Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Linq;
- using Exception = System.Exception;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- /// <summary>
- /// Testing the order of initialization calls available to us from the Unity Engine.
- /// </summary>
- public class TestInitOrder : MonoBehaviour
- {
- #pragma warning disable 0414 // unused variables
- [SerializeField] Renderer _renderer;
- [SerializeField] string _staticConstructor;
- [SerializeField] string _playModeStateChanged;
- [SerializeField] string _onValidate;
- [SerializeField] string _didReloadScripts;
- [SerializeField] string _initOnLoadMethod;
- [SerializeField] string _runtimeInitOnLoadMethod;
- [SerializeField] string _postProcessScene;
- [SerializeField] string _awake;
- /// <summary>
- /// 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.
- /// </summary>
- /// <param name="instance">The instance of TestOrderInit if there is one</param>
- /// <param name="baseString">The function name that called DoLogging</param>
- /// <returns></returns>
- private static string DoLogging( TestInitOrder instance, string baseString )
- {
- #if UNITY_EDITOR
- 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" );
- #else
- string logString = string.Format( "{0} - IsPlaying: {1}", baseString, Application.isPlaying );
- #endif
- Debug.Log( logString );
- return logString;
- }
- /// <summary>
- /// Let us know when Reset() is called, also used to setup the _renderer variable in a nice, Unity 5.x-friendly way
- /// </summary>
- void Reset()
- {
- _renderer = GetComponent<MeshRenderer>();
- Debug.Log( "Reset was called (and the _renderer variable is set to the Renderer attached to the GameObject, if there was one)" );
- }
- /// <summary>
- /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html">Awake</see> gets called.
- /// Additionally, tell us what the current values are since we're most likely to get this at runtime.
- /// </summary>
- void Awake()
- {
- // Just print out the values before we change anything...
- var allFields = GetType().GetFields( System.Reflection.BindingFlags.NonPublic| System.Reflection.BindingFlags.Instance );
- foreach( var field in allFields )
- Debug.Log( string.Format("During Awake, Field {0} = {1}", field.Name, field.GetValue(this)) );
- // Now tell us when this was actually executed
- _awake = DoLogging( this, "Awake" );
- }
- /// <summary>
- /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnEnable.html">OnEnable</see> gets called.
- /// </summary>
- void OnEnable()
- {
- DoLogging( this, "OnEnable" );
- }
- /// <summary>
- /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html">Start</see> gets called.
- /// </summary>
- void Start()
- {
- DoLogging( this, "Start" );
- }
- /// <summary>
- /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html">OnLevelWasLoaded</see> gets called
- /// </summary>
- /// <param name="level">The level number that was configured in the build settings</param>
- void OnLevelWasLoaded(int level)
- {
- DoLogging( this, "OnLevelWasLoaded" );
- }
- /// <summary>
- /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnValidate.html">OnValidate</see> gets called
- /// </summary>
- void OnValidate()
- {
- _onValidate = DoLogging( this, "OnValidate" );
- }
- /// <summary>
- /// Tell us whenever the static constructor gets called. We subscribe to playmodeStateChanged here.
- /// </summary>
- static TestInitOrder()
- {
- #if UNITY_EDITOR
- EditorApplication.playmodeStateChanged += PlaymodeStateChanged;
- #endif
- TestInitOrder existingInstance = null;
- #if false
- // Try...catch because we can be on a loading thread which isn't thread-safe with Unity API
- try
- {
- // Need to find the object because we're not an actual instance in the static constructor
- existingInstance = Resources.FindObjectsOfTypeAll<TestInitOrder>().FirstOrDefault();
- }
- catch ( Exception ex )
- {
- Debug.LogException( ex );
- }
- #endif
- if ( existingInstance )
- existingInstance._staticConstructor = DoLogging( existingInstance, "StaticConstructor" );
- else
- DoLogging( null, "StaticConstructor" );
- }
- #if UNITY_EDITOR
- /// <summary>
- /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/Callbacks.PostProcessSceneAttribute.html">PostProcessScene</see> gets called
- /// </summary>
- [UnityEditor.Callbacks.PostProcessScene]
- static void PostProcessScene()
- {
- TestInitOrder existingInstance = null;
- try
- {
- existingInstance = Resources.FindObjectsOfTypeAll<TestInitOrder>().FirstOrDefault();
- }
- catch ( Exception ex )
- {
- Debug.LogException( ex );
- }
- if ( existingInstance )
- existingInstance._postProcessScene = DoLogging( existingInstance, "PostProcessScene" );
- else
- DoLogging( null, "PostProcessScene" );
- }
- /// <summary>
- /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/EditorApplication-playmodeStateChanged.html">Application.PlaymodeStateChanged</see> is triggered
- /// </summary>
- static void PlaymodeStateChanged()
- {
- TestInitOrder existingInstance = null;
- try
- {
- existingInstance = Resources.FindObjectsOfTypeAll<TestInitOrder>().FirstOrDefault();
- }
- catch ( Exception ex )
- {
- Debug.LogException( ex );
- }
- if ( existingInstance )
- existingInstance._playModeStateChanged = DoLogging( existingInstance, "PlaymodeStateChanged" );
- else
- DoLogging( null, "PlaymodeStateChanged" );
- }
- /// <summary>
- /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/Callbacks.DidReloadScripts.html">DidReloadScripts</see> is triggered
- /// </summary>
- [UnityEditor.Callbacks.DidReloadScripts]
- static void DidReloadScripts()
- {
- TestInitOrder existingInstance = null;
- try
- {
- existingInstance = Resources.FindObjectsOfTypeAll<TestInitOrder>().FirstOrDefault();
- }
- catch ( Exception ex )
- {
- Debug.LogException( ex );
- }
- if ( existingInstance )
- existingInstance._didReloadScripts = DoLogging( existingInstance, "DidReloadScripts" );
- else
- DoLogging( null, "DidReloadScripts" );
- }
- #if UNITY_5
- /// <summary>
- /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/InitializeOnLoadMethodAttribute.html">InitializeOnLoadMethod</see> is triggered
- /// </summary>
- [UnityEditor.InitializeOnLoadMethod]
- static void InitializeOnLoadMethod()
- {
- TestInitOrder existingInstance = null;
- try
- {
- existingInstance = Resources.FindObjectsOfTypeAll<TestInitOrder>().FirstOrDefault();
- }
- catch ( Exception ex )
- {
- Debug.LogException( ex );
- }
- if ( existingInstance )
- existingInstance._initOnLoadMethod = DoLogging( existingInstance, "InitializeOnLoadMethod" );
- else
- DoLogging( null, "InitializeOnLoadMethod" );
- }
- #endif
- #endif
- #if UNITY_5
- /// <summary>
- /// Tell us whenever <see cref="http://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute.html">RuntimeInitializeOnLoadMethod</see> gets called
- /// </summary>
- [UnityEngine.RuntimeInitializeOnLoadMethod]
- static void RuntimeInitializeOnLoadMethod()
- {
- TestInitOrder existingInstance = null;
- try
- {
- existingInstance = Resources.FindObjectsOfTypeAll<TestInitOrder>().FirstOrDefault();
- }
- catch ( Exception ex )
- {
- Debug.LogException( ex );
- }
- if ( existingInstance )
- existingInstance._initOnLoadMethod = DoLogging( existingInstance, "RuntimeInitializeOnLoadMethod" );
- else
- DoLogging( null, "RuntimeInitializeOnLoadMethod" );
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment