From Unify Community Wiki
Revision as of 17:27, 2 August 2011 by Bcedmark (Talk | contribs)

Jump to: navigation, search

Author: Brandon Edmark


This is a singleton class that allows non-MonoBehaviour objects to hook into Unity's Update, FixedUpdate, and LateUpdate loops. It is useful if you have objects outside the Unity MonoBehaviour/GameObject system that need to update like MonoBehaviours can. It can also be used to boost performance vs Unity's automatic Update calls if there are hundreds of MonoBehaviours updating in a scene.


Call GlobalUpdatePump.Instance.Register, inputting the name of the method you want to run and the type of Update you want to run it on. The update type, fittingly, is specified by the UpdateType enum. The method must have no parameters and must return void.

You can call GlobalUpdatePump.Instance.UnRegister if you want to stop calling a method. However, if the object owning the method was destroyed, GlobalUpdatePump will automatically stop caring about it, so you don't have to worry about deregistering just for the sake of null safety.

The class will put its sole instance on a GameObject when it is first called. if hideGameObject = true, it will hide the GameObject in the hierarchy. The GameObject is not destroyed on scene load; the presence of the GlobalUpdatePump should not need to be managed.

As GlobalUpdatePump is just a MonoBehaviour, you can use its "enabled" property to pause the updating of all the objects registered to it.


<csharp> using UnityEngine; using System; using System.Collections.Generic;

/// <summary> /// A way of executing Unity's update messages on non-Monobehaviour objects, such as static /// objects or ScriptableObjects. /// </summary> public class GlobalUpdatePump : MonoBehaviour {

public static bool hideGameObject = true;

private List<Action> fixedUpdateTarget = new List<Action> (); private List<Action> updateTarget = new List<Action> (); private List<Action> lateUpdateTarget = new List<Action> ();

private static GlobalUpdatePump instance;

public static GlobalUpdatePump Instance { get { if (instance == null) { GameObject holder = new GameObject ("GlobalUpdatePump"); instance = holder.AddComponent<GlobalUpdatePump> ();

                               if (hideGameObject) { holder.hideFlags = HideFlags.HideInHierarchy; }

DontDestroyOnLoad(holder); }

return instance; } }

void OnApplicationQuit () { instance = null; }

public void Register (Action action, UpdateType updateType) { switch (updateType) { case UpdateType.FixedUpdate: fixedUpdateTarget.Add (action); return; case UpdateType.Update: updateTarget.Add (action); return; case UpdateType.LateUpdate: lateUpdateTarget.Add (action); return; } }

public void UnRegister (Action action, UpdateType updateType) { switch (updateType) { case UpdateType.FixedUpdate: fixedUpdateTarget.Remove (action); return; case UpdateType.Update: updateTarget.Remove (action); return; case UpdateType.LateUpdate: lateUpdateTarget.Remove (action); return; } }

void FixedUpdate () { DoUpdating(fixedUpdateTarget); }

void Update () { DoUpdating (updateTarget); }

void LateUpdate () { DoUpdating (lateUpdateTarget); }

void DoUpdating (List<Action> listToUpdate) { int actionsToDo = listToUpdate.Count; for (int i = 0; i < actionsToDo; i++) { Action actionToDo = listToUpdate[i]; if (actionToDo == null) { listToUpdate.Remove (actionToDo); } else { actionToDo (); } } } }

public enum UpdateType { FixedUpdate, Update, LateUpdate } </csharp>

Personal tools