ALWAYS assign enum values (at least, if you serialize them)
I'm using a mobile integration library, let's call it Unicorn library, for our Bug-A-Ball game. I like the Unicorn library because it simplifies accessing native iOS and Android APIs. They do pretty good work and the support is good as well.
But in their last update, they did a simple mistake. In their code, they have an enum with all supported advertising networks looking like this (I've removed some values for better readability):
public enum AdNetwork
{
None,
IronSource,
MoPub,
UnityAds,
Vungle,
}
Nothing wrong here so far you may think.
Wrong! :-) I've updated the Unicorn library from 2.18.1 to 2.19.0 - so a minor version. Looking through the diff, I've seen that they've updated their enum:
public enum AdNetwork
{
None,
IronSource,
- MoPub,
UnityAds,
Vungle,
}
They've removed MoPub.
Now, we have a big issue. This enum is serialized into an asset file (ScriptableObject), so every value has a specific number. If you don't specify it, the compiler will do that for you, like this:
public enum AdNetwork
{
None = 0,
IronSource = 1,
MoPub = 2,
UnityAds = 3,
Vungle = 4,
}
If you now remove MoPub, the compiler will count again:
public enum AdNetwork
{
None = 0,
IronSource = 1,
UnityAds = 2,
Vungle = 3,
}
See, how UnityAds
value changed from 3 to 2 and Vungle from 4 to 3?
Ok, how is that an issue now?
Well, in version 2.18.1 I've set Bug-A-Ball to use UnityAds
. The value that has been serialized to the asset file was 3
. Now, after the update to version 2.19.0, the asset file was loaded from the Unicorn library and is now set to Vungle
instead of UnityAds
.
With that little change, they've broken the whole UnityAds
integration. No ads will show up anymore, because it uses the wrong AdNetwork that has not been set up. Bummer! Basically, all AdNetworks after MoPub are changed.
How to prevent this?
You should always manually set enum values and not let the compiler count for you. Then, you can simply remove in-between values:
public enum AdNetwork
{
None = 0,
IronSource = 1,
- MoPub = 2,
UnityAds = 3,
Vungle = 4
}
Now, it will only break for users that have used MoPub, but not all the others.
Cheers!