- Home /
Is "delegates" in "Update", efficient or worst
I have 100 objects in the game, I need to move every cube. I know 100 "Updates" in 100 objects will cause performance issue.
So I design like only one class have "Update". The same class contains "event" using "delegate"; This event is calling in "Update".
using UnityEngine;
namespace com.Test.One
{
public class Controller1 : MonoBehaviour
{
public delegate void OtherUpdateDelegate();
public static event OtherUpdateDelegate OtherUpdate;
private static Controller1 instance;
public static Controller1 Instance
{
get { return instance;}
}
private void Awake()
{
if(instance != null && instance != this)
{
Destroy(this.gameObject);
}
else
{
instance = this;
}
}
/// <summary>
/// events for calling custom Update fuctions of other scripts
/// </summary>
private void Update()
{
if(OtherUpdate != null)
{
OtherUpdate();
}
}
}
}
Other classes are adding " custom update " into the event, and performing their movement or other actions
using UnityEngine;
namespace com.Test.One
{
public class MovingObjects : MonoBehaviour
{
[SerializeField] private float speed = 5;
[SerializeField] private float yMax = 3, yMin = -3;
[SerializeField]
private float currentSpeed;
private void Start()
{
currentSpeed = speed;
if (Controller1.Instance != null)
{
Controller1.OtherUpdate += MyUpdate;
}
}
private void OnDisable()
{
if (Controller1.Instance != null)
{
Controller1.OtherUpdate -= MyUpdate;
}
}
private void MyUpdate() // custom update
{
transform.Translate(transform.up * currentSpeed * Time.deltaTime);
Debug.Log("" + transform.position.y);
if (transform.position.y >= yMax)
{
currentSpeed = -(speed);
}
if (transform.position.y <= yMin)
{
currentSpeed = (speed);
}
}
}
}
Is this efficient way or worst. If worst any alternative solution there?
The best way to know is ... to profile but I believe you will have some improvements. Valentin Simonov has made a test of 10 000 $$anonymous$$onoBehaviours calling Update each frame vs 1 $$anonymous$$anager calling a custom function on 10 000 $$anonymous$$onoBehaviour every update and obviously, the manager was better. I think it will be similar in your case with the delegate-way (not sure tough, the profiler will be your friend here)
Like Helium says, try it and test it. It feels like an improvement but the impact might depend on, for example, how often these things are getting enabled and disabled.
And note that using delegates isn't the only possible way of doing this. It's a neat way but may not be the most efficient (see Bunny83's answer for more on this)
Some other things you might want to do include cacheing the transform in $$anonymous$$ovingObjects, and adding an "else" at line 34.
Answer by Bunny83 · Jun 04, 2019 at 08:51 AM
If you plan to register many methods to the same multicast delegate, it will not be very efficient because the way the multicast delegate handles. A multicast delegate is essentially a doubly linked list of multicast delegate instances. So each delegate instance has next and a previous reference to the next and previous instance in the chain. Linked lists would actually be a great and flexible way to handle adding and removing instances. However the way "combining" of delegates work all instances are cloned whenever you add or remove an instance. So if you have 1000 delegates combined in a single multicast delegate you will clone all 1000 instances when you add another one or when you remove one.
It would be more efficient when you simply use a List<Action>
where you simply add all the method you want to invoke and just iterate through that List in Update and invoke them one by one. (that's what a multicast delegate does anyways). Since the order of the registered methods usually don't matter when you want to remove a method you could use the "replace with last, delete last" approach. This avoids the down shifting of the internal array elements when you remove the first element (or an element close to the start).
Though instead of storing delegates it often makes more sense to implement a custom interface and store the object references in a list. Delegates can be quite demanding especially when you often add / remove instances.
Note that you currently used Start to register and OnDisable to unregister the delegate. This is the wrong pairing. The partner of Start is OnDestroy. Or if you like to use OnDisable you would use the pair OnEnable / OnDisable. Start is only called once in the lifetime of an object. OnDisable is called each time the object is disabled.
Answer by liju_juego · Jun 05, 2019 at 07:22 AM
I followed the @Bunny83's instructions. It's work awesome.
Here is the code
public class Controller3 : MonoBehaviour
{
private static Controller3 instance;
public static Controller3 Instance
{
get { return instance; }
}
private List<Action> movingObjectList = new List<Action>();
private void Awake()
{
if(instance != null && instance != this)
{
Destroy(gameObject);
}
else
{
instance = this;
}
}
private void Update()
{
//Debug.Log("movingObjectList.Count:" + movingObjectList.Count);
for (int i = 0; i < movingObjectList.Count; i++)
{
if (movingObjectList.Count > i)
{
//Debug.Log("" + i);
if (movingObjectList[i] != null )
{
movingObjectList[i].Invoke();
}
}
}
}
public void AddAction(Action action)
{
Debug.Log("AddAction " );
if (!movingObjectList.Contains(action))
{
movingObjectList.Add(action);
}
}
public void RemoveAction(Action action)
{
Debug.Log("RemoveAction ");
if (movingObjectList.Contains(action))
{
movingObjectList.Remove(action);
}
}
}
Controller3 script attached with one empty game object
public class MovingObject3 : MonoBehaviour {
[SerializeField] private float speed = 5;
[SerializeField] private float yMax = 3, yMin = -3;
[SerializeField]
private float currentSpeed;
private void OnEnable()
{
StartCoroutine(I_DelayedActionAdd());
}
private void AddToAction()
{
Debug.Log("OnEnable" + Controller3.Instance.ToString());
currentSpeed = speed;
if (Controller3.Instance != null)
{
Controller3.Instance.AddAction(MyUpdate);
}
}
private IEnumerator I_DelayedActionAdd()
{
if (gameObject.activeInHierarchy)
{
yield return new WaitForSeconds(1);
if (gameObject.activeInHierarchy)
{
AddToAction();
}
}
}
private void OnDisable()
{
if (Controller3.Instance != null)
{
Controller3.Instance.RemoveAction(MyUpdate);
}
}
private void MyUpdate() // custom update
{
transform.Translate(transform.up * currentSpeed * Time.deltaTime);
//Debug.Log("" + transform.position.y);
if (transform.position.y >= yMax)
{
currentSpeed = -(speed);
}
if (transform.position.y <= yMin)
{
currentSpeed = (speed);
}
}
}
MovingObject3 attached with 1000 of object
working smoothly in the Unity editor without any lag
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer
(Game Build) Validating files and updating to latest version 0 Answers
How to trigger udpate in editor mode only when I modify component parameters 1 Answer