CodingJar

Test Unity Initialization Method Order

Aug 2nd, 2015
5,408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.88 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Linq;
  3. using Exception = System.Exception;
  4.  
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8.  
  9. /// <summary>
  10. /// Testing the order of initialization calls available to us from the Unity Engine.
  11. /// </summary>
  12. public class TestInitOrder : MonoBehaviour
  13. {
  14. #pragma warning disable 0414 // unused variables
  15.     [SerializeField]    Renderer    _renderer;
  16.     [SerializeField]    string      _staticConstructor;
  17.     [SerializeField]    string      _playModeStateChanged;
  18.     [SerializeField]    string      _onValidate;
  19.     [SerializeField]    string      _didReloadScripts;
  20.     [SerializeField]    string      _initOnLoadMethod;
  21.     [SerializeField]    string      _runtimeInitOnLoadMethod;
  22.     [SerializeField]    string      _postProcessScene;
  23.     [SerializeField]    string      _awake;
  24.  
  25.     /// <summary>
  26.     /// This is the most basic log functionality we're going to use in our test.  It's just going to tell us what function is called and what state the Unity Engine was in.
  27.     /// </summary>
  28.     /// <param name="instance">The instance of TestOrderInit if there is one</param>
  29.     /// <param name="baseString">The function name that called DoLogging</param>
  30.     /// <returns></returns>
  31.     private static string DoLogging( TestInitOrder instance, string baseString )
  32.     {
  33. #if UNITY_EDITOR
  34.         string logString = string.Format( "{0} - IsPlaying: {1} Will Play: {2} IsStaticBatch: {3}", baseString, EditorApplication.isPlaying, EditorApplication.isPlayingOrWillChangePlaymode, instance && instance._renderer ? instance._renderer.isPartOfStaticBatch.ToString() : "No Instance" );
  35. #else
  36.         string logString = string.Format( "{0} - IsPlaying: {1}", baseString, Application.isPlaying );
  37. #endif
  38.         Debug.Log( logString );
  39.  
  40.         return logString;
  41.     }
  42.  
  43.     /// <summary>
  44.     /// Let us know when Reset() is called, also used to setup the _renderer variable in a nice, Unity 5.x-friendly way
  45.     /// </summary>
  46.     void Reset()
  47.     {
  48.         _renderer = GetComponent<MeshRenderer>();
  49.         Debug.Log( "Reset was called (and the _renderer variable is set to the Renderer attached to the GameObject, if there was one)" );
  50.     }
  51.  
  52.     /// <summary>
  53.     /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html">Awake</see> gets called.
  54.     /// Additionally, tell us what the current values are since we're most likely to get this at runtime.
  55.     /// </summary>
  56.     void Awake()
  57.     {
  58.         // Just print out the values before we change anything...
  59.         var allFields = GetType().GetFields( System.Reflection.BindingFlags.NonPublic| System.Reflection.BindingFlags.Instance );
  60.         foreach( var field in allFields )
  61.             Debug.Log( string.Format("During Awake, Field {0} = {1}", field.Name, field.GetValue(this)) );
  62.  
  63.         // Now tell us when this was actually executed
  64.         _awake = DoLogging( this, "Awake" );
  65.     }
  66.  
  67.     /// <summary>
  68.     /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnEnable.html">OnEnable</see> gets called.
  69.     /// </summary>
  70.     void OnEnable()
  71.     {
  72.         DoLogging( this, "OnEnable" );
  73.     }
  74.  
  75.     /// <summary>
  76.     /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html">Start</see> gets called.
  77.     /// </summary>
  78.     void Start()
  79.     {
  80.         DoLogging( this, "Start" );
  81.     }
  82.  
  83.     /// <summary>
  84.     /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html">OnLevelWasLoaded</see> gets called
  85.     /// </summary>
  86.     /// <param name="level">The level number that was configured in the build settings</param>
  87.     void OnLevelWasLoaded(int level)
  88.     {
  89.         DoLogging( this, "OnLevelWasLoaded" );
  90.     }
  91.  
  92.     /// <summary>
  93.     /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnValidate.html">OnValidate</see> gets called
  94.     /// </summary>
  95.     void OnValidate()
  96.     {
  97.         _onValidate = DoLogging( this, "OnValidate" );
  98.     }
  99.  
  100.     /// <summary>
  101.     /// Tell us whenever the static constructor gets called.  We subscribe to playmodeStateChanged here.
  102.     /// </summary>
  103.     static TestInitOrder()
  104.     {
  105. #if UNITY_EDITOR
  106.         EditorApplication.playmodeStateChanged += PlaymodeStateChanged;
  107. #endif
  108.  
  109.         TestInitOrder existingInstance = null;
  110.  
  111. #if false
  112.         // Try...catch because we can be on a loading thread which isn't thread-safe with Unity API
  113.         try
  114.         {
  115.             // Need to find the object because we're not an actual instance in the static constructor
  116.             existingInstance = Resources.FindObjectsOfTypeAll<TestInitOrder>().FirstOrDefault();
  117.         }
  118.         catch ( Exception ex )
  119.         {
  120.             Debug.LogException( ex );
  121.         }
  122. #endif
  123.  
  124.         if ( existingInstance )
  125.             existingInstance._staticConstructor = DoLogging( existingInstance, "StaticConstructor" );
  126.         else
  127.             DoLogging( null, "StaticConstructor" );
  128.     }
  129.  
  130. #if UNITY_EDITOR
  131.     /// <summary>
  132.     /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/Callbacks.PostProcessSceneAttribute.html">PostProcessScene</see> gets called
  133.     /// </summary>
  134.     [UnityEditor.Callbacks.PostProcessScene]
  135.     static void PostProcessScene()
  136.     {
  137.         TestInitOrder existingInstance = null;
  138.  
  139.         try
  140.         {
  141.             existingInstance = Resources.FindObjectsOfTypeAll<TestInitOrder>().FirstOrDefault();
  142.         }
  143.         catch ( Exception ex )
  144.         {
  145.             Debug.LogException( ex );
  146.         }
  147.  
  148.         if ( existingInstance )
  149.             existingInstance._postProcessScene = DoLogging( existingInstance, "PostProcessScene" );
  150.         else
  151.             DoLogging( null, "PostProcessScene" );
  152.     }
  153.  
  154.     /// <summary>
  155.     /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/EditorApplication-playmodeStateChanged.html">Application.PlaymodeStateChanged</see> is triggered
  156.     /// </summary>
  157.     static void PlaymodeStateChanged()
  158.     {
  159.         TestInitOrder existingInstance = null;
  160.  
  161.         try
  162.         {
  163.             existingInstance = Resources.FindObjectsOfTypeAll<TestInitOrder>().FirstOrDefault();
  164.         }
  165.         catch ( Exception ex )
  166.         {
  167.             Debug.LogException( ex );
  168.         }
  169.  
  170.         if ( existingInstance )
  171.             existingInstance._playModeStateChanged = DoLogging( existingInstance, "PlaymodeStateChanged" );
  172.         else
  173.             DoLogging( null, "PlaymodeStateChanged" );
  174.     }
  175.  
  176.     /// <summary>
  177.     /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/Callbacks.DidReloadScripts.html">DidReloadScripts</see> is triggered
  178.     /// </summary>
  179.     [UnityEditor.Callbacks.DidReloadScripts]
  180.     static void DidReloadScripts()
  181.     {
  182.         TestInitOrder existingInstance = null;
  183.  
  184.         try
  185.         {
  186.             existingInstance = Resources.FindObjectsOfTypeAll<TestInitOrder>().FirstOrDefault();
  187.         }
  188.         catch ( Exception ex )
  189.         {
  190.             Debug.LogException( ex );
  191.         }
  192.  
  193.         if ( existingInstance )
  194.             existingInstance._didReloadScripts = DoLogging( existingInstance, "DidReloadScripts" );
  195.         else
  196.             DoLogging( null, "DidReloadScripts" );
  197.     }
  198.  
  199. #if UNITY_5
  200.     /// <summary>
  201.     /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/InitializeOnLoadMethodAttribute.html">InitializeOnLoadMethod</see> is triggered
  202.     /// </summary>
  203.     [UnityEditor.InitializeOnLoadMethod]
  204.     static void InitializeOnLoadMethod()
  205.     {
  206.         TestInitOrder existingInstance = null;
  207.  
  208.         try
  209.         {
  210.             existingInstance = Resources.FindObjectsOfTypeAll<TestInitOrder>().FirstOrDefault();
  211.         }
  212.         catch ( Exception ex )
  213.         {
  214.             Debug.LogException( ex );
  215.         }
  216.  
  217.         if ( existingInstance )
  218.             existingInstance._initOnLoadMethod = DoLogging( existingInstance, "InitializeOnLoadMethod" );
  219.         else
  220.             DoLogging( null, "InitializeOnLoadMethod" );
  221.     }
  222. #endif
  223. #endif
  224.  
  225. #if UNITY_5
  226.     /// <summary>
  227.     /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute.html">RuntimeInitializeOnLoadMethod</see> gets called
  228.     /// </summary>
  229.     [UnityEngine.RuntimeInitializeOnLoadMethod]
  230.     static void RuntimeInitializeOnLoadMethod()
  231.     {
  232.         TestInitOrder existingInstance = null;
  233.  
  234.         try
  235.         {
  236.             existingInstance = Resources.FindObjectsOfTypeAll<TestInitOrder>().FirstOrDefault();
  237.         }
  238.         catch ( Exception ex )
  239.         {
  240.             Debug.LogException( ex );
  241.         }
  242.  
  243.         if ( existingInstance )
  244.             existingInstance._initOnLoadMethod = DoLogging( existingInstance, "RuntimeInitializeOnLoadMethod" );
  245.         else
  246.             DoLogging( null, "RuntimeInitializeOnLoadMethod" );
  247.     }
  248. #endif
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment