Question by
mmvlad · Aug 23, 2018 at 10:43 AM ·
compilation
RuntimeInitializeOnLoadMethod inside define not working
Hi, I found today in my project that RuntimeInitializeOnLoadMethod inside #if UNITY_IOS && !UNITY_EDITOR on IOS was not working when.
Platform IOS XCode 9.4 Unity 2017.4.6f1 LTS
So this code showed nothing:
public static class MyStaticClass
{
#if UNITY_IOS && !UNITY_EDITOR
[RuntimeInitializeOnLoadMethod]
private static void Initialize()
{
Debug.Log("Init on load");
}
#endif
}
And this printed message as it was supposed to
public static class MyStaticClass
{
[RuntimeInitializeOnLoadMethod]
private static void Initialize()
{
#if UNITY_IOS && !UNITY_EDITOR
Debug.Log("Init on load");
#endif
}
}
I believe this is not expected behavior. Am I missing something?
Comment
Answer by Curious-George · Feb 19, 2019 at 05:50 PM
This one just hit me too. I doubt it's expected behaviour. Maybe there's some souce code preprocessing step that's out of order or something.
In any case, I opted to just do this:
#if UNITY_IOS && !UNITY_EDITOR
private static void InitializeImpl() {
Debug.Log("Init on load");
}
#else
private static void InitializeImpl() {
/* empty */
}
#endif
[RuntimeInitializeOnLoadMethod]
private static void Initialize() {
InitializeImpl();
}