Auto-Install void Update() optimization fast system by Night Train Code
The system caches all components, that need an void Update() into one list, then a single void Update() calls them all from the list
https://www.youtube.com/watch?v=7Dvir9Bf8X4
-
Install NightCache components into your Unity project (it will calls all void Run, FixedRun and LateRun in scene)
-
Add component «NightCacheEntry» on any GameObject in scene
-
Inherit any class, which contains void Update / FixedUpdate / LateUpdate from «NightCache»
-
Implement the required interfaces: «INightInit», «INightRun», «INightFixedRun» or «INightLateRun»
-
Move the code from the old method to the new one
-
System will automatically add a new component «NightCacheInstallMachine» on GameObject, which contains a «NightCache» by RequireComponent. If for some reason this did not happen, then add it manually.
-
Everything is ready to use :)
UPD: See below documentation for MonoInstallable & NightCacheInstallable
public class UnitMovement : MonoBehaviour
{
private void Start()
{
//OnStart
}
private void MoveUnit()
{
//Movement
}
private void Update()
{
MoveUnit();
}
}
public class UnitMovement : NightCache, INightInit, INightRun
{
public void Init()
{
//OnStart
}
private void MoveUnit()
{
//Movement
}
public void Run()
{
MoveUnit();
}
}
Interface | Identical |
---|---|
INightRun : void Init() | void Start() |
INightRun : void Run() | void Update() |
INightFixedRun : void FixedRun() | void FixedUpdate() |
INightLateRun : void LateRun() | void LateUpdate() |
Installables contains virtual void OnFirstEnable() This method is called once the first time the object is enabled
MonoInstallable for MonoBehaviour, NightCacheInstallable for NightCache
Old:
public class UnitMovement : MonoBehaviour
{
private bool installed;
private void OnEnable()
{
if (installed) return;
//DoSomething
installed = true;
}
}
New:
public class UnitMovement : MonoInstallable
{
protected override void OnFirstEnable()
{
//DoSomething
}
}
public class UnitMovement : NightCacheInstallable, INightRun
{
protected override void OnFirstEnable()
{
//DoSomething
}
public void Run()
{
//Movement
}
}