Startup Manager

From Unify Community Wiki
Revision as of 10:17, 10 December 2009 by Ryan scott (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

There is currently no built-in way to manage the order in which scripts start up. To a certain extent you can get around this by putting more important or prerequisite code into Awake(), but this doesn't solve the problem and if you're entirely likely to run into the same problem again.

The startup manager is a singleton class that calls an initialization function on participating objects created in the last frame (or at startup) in the order dictated by their priority variable. Participating classes must be derived from the provided BaseBehaviour class (which in turn derives from MonoBehaviour as per usual). This class registers with the startup manager singleton in Start() which then calls Initialize() on it at the begining of the next frame (again, in the appropriate order).

The BaseBehaviour.priority member is a float. This was a design decision to allow for plenty of tweaking.

BaseBehaviour derived objects which do not find the StartManager instance (e.g. because it is not present in the scene) will call Initialize themselves at the next LateUpdate().

C# - StartupManager.cs

<csharp> using UnityEngine; using System.Collections;

public class StartupManager : MonoBehaviour {

   public static StartupManager Instance;
   
   public bool debug = false;
   ArrayList componentList = new ArrayList();
   void Awake() {
       Instance = this;
   }

void Update () {

       if( componentList.Count <= 0 )
           return;
       if( debug )
           Debug.Log( "StartupManager: Update()" );
       // Call Initialize() on everything
       componentList.Sort();
       foreach( BaseBehaviour behaviour in componentList ) {
           if( behaviour != null && behaviour._initialized == false )
               initializeObject( behaviour );
       }
       componentList.Clear();

}

   public virtual void registerStartup( BaseBehaviour behaviour ) {
       if( debug )
           Debug.Log( "Registering " + behaviour + " @ priority " + behaviour._StartupPriority );
       
       componentList.Add( behaviour );
   }
   public void initializeObject( BaseBehaviour behaviour ) {
       behaviour.Initialize();
       behaviour._initialized = true;
   }

} </csharp>

C# - BaseBehaviour.cs

<csharp> using UnityEngine; using System.Collections;

public class BaseBehaviour : MonoBehaviour, System.IComparable {

   public float _StartupPriority = 0f;
   public int CompareTo( object obj ) {
       if( obj is BaseBehaviour ) {
           BaseBehaviour thisObj = (BaseBehaviour) obj;
           return _StartupPriority.CompareTo( thisObj._StartupPriority );
       }
       return 0;
   }
   
   [HideInInspector]
       public bool _initialized = false;
   public virtual void Initialize() {
       //Debug.Log( _StartupPriority + ": In Inititialize()" );
       _initialized = true;
   }

public virtual void Start () {

       if( StartupManager.Instance != null )
           StartupManager.Instance.registerStartup( this );
       else
           Initialize();

}

   public virtual void LateUpdate() {
       //if( !_initialized ) {
       if( StartupManager.Instance == null ) {
           //Debug.Log( name + ": LateUpdate() and not initialized; assuming no StartupManager present, doing Init()" );
           _initialized = true;
       }
   }
   public virtual void OnSerializeNetworkView( BitStream stream, NetworkMessageInfo info ) {
   }

} </csharp>

Here is an example of a BaseBehaviour derived class.

C# - TestClass.cs

<csharp>

</csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox