UpdatePump

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Usage)
(GlobalUpdatePump.cs)
Line 21: Line 21:
  
 
/// <summary>
 
/// <summary>
/// A way of executing Unity's update messages on non-Monobehaviour objects, such as static
+
/// A way of executing Unity's update messages on non-Monobehaviour objects.
/// objects or ScriptableObjects.
+
 
/// </summary>
 
/// </summary>
 
public class GlobalUpdatePump : MonoBehaviour {
 
public class GlobalUpdatePump : MonoBehaviour {
 
          
 
          
public static bool hideGameObject = true;
+
    public static bool hideGameObject = true;
 
 
private List<Action> fixedUpdateTarget = new List<Action> ();
+
private event Action fixedUpdateTarget;
private List<Action> updateTarget = new List<Action> ();
+
private event Action updateTarget;
private List<Action> lateUpdateTarget = new List<Action> ();
+
private event Action lateUpdateTarget;
+
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)
 
public void Register (Action action, UpdateType updateType)
Line 60: Line 36:
 
{
 
{
 
case UpdateType.FixedUpdate:
 
case UpdateType.FixedUpdate:
fixedUpdateTarget.Add (action);
+
fixedUpdateTarget += action;
 
return;
 
return;
 
case UpdateType.Update:
 
case UpdateType.Update:
updateTarget.Add (action);
+
updateTarget += action;
 
return;
 
return;
 
case UpdateType.LateUpdate:
 
case UpdateType.LateUpdate:
lateUpdateTarget.Add (action);
+
lateUpdateTarget += action;
 
return;
 
return;
 
}
 
}
Line 73: Line 49:
 
public void UnRegister (Action action, UpdateType updateType)
 
public void UnRegister (Action action, UpdateType updateType)
 
{
 
{
switch (updateType) {
+
switch (updateType)  
 +
{
 
case UpdateType.FixedUpdate:
 
case UpdateType.FixedUpdate:
fixedUpdateTarget.Remove (action);
+
fixedUpdateTarget -= action;
 
return;
 
return;
 
case UpdateType.Update:
 
case UpdateType.Update:
updateTarget.Remove (action);
+
updateTarget -= action;
 
return;
 
return;
 
case UpdateType.LateUpdate:
 
case UpdateType.LateUpdate:
lateUpdateTarget.Remove (action);
+
lateUpdateTarget -= action;
 
return;
 
return;
 
}
 
}
Line 101: Line 78:
 
}
 
}
 
 
void DoUpdating (List<Action> listToUpdate)
+
void DoUpdating (Action currentEvent)
 +
{
 +
if (currentEvent != null)
 +
{
 +
currentEvent ();
 +
}
 +
}
 +
 +
void OnDestroy ()
 +
{
 +
RemoveAll (fixedUpdateTarget);
 +
RemoveAll (updateTarget);
 +
RemoveAll (lateUpdateTarget);
 +
}
 +
 +
void RemoveAll (Action deadEvent)
 
{
 
{
int actionsToDo = listToUpdate.Count;
+
if (deadEvent != null)  
for (int i = 0; i < actionsToDo; i++)
+
 
{
 
{
Action actionToDo = listToUpdate[i];
+
Delegate[] clientList = deadEvent.GetInvocationList ();
if (actionToDo == null)
+
foreach (Delegate d in clientList) {
{
+
deadEvent -= (d as Action);
listToUpdate.Remove (actionToDo);
+
}
+
else
+
{
+
actionToDo ();
+
 
}
 
}
 
}
 
}

Revision as of 23:32, 15 August 2011

Author: Brandon Edmark

Description

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.

Usage

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 presence of the GlobalUpdatePump should not need to be managed. The class will put its sole instance on a new 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.

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

GlobalUpdatePump.cs

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

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

   public static bool hideGameObject = true;

private event Action fixedUpdateTarget; private event Action updateTarget; private event Action lateUpdateTarget;

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

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

void FixedUpdate () { DoUpdating(fixedUpdateTarget); }

void Update () { DoUpdating (updateTarget); }

void LateUpdate () { DoUpdating (lateUpdateTarget); }

void DoUpdating (Action currentEvent) { if (currentEvent != null) { currentEvent (); } }

void OnDestroy () { RemoveAll (fixedUpdateTarget); RemoveAll (updateTarget); RemoveAll (lateUpdateTarget); }

void RemoveAll (Action deadEvent) { if (deadEvent != null) { Delegate[] clientList = deadEvent.GetInvocationList (); foreach (Delegate d in clientList) { deadEvent -= (d as Action); } } } }

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

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox