- Do not make every class a MonoBehaviour. Static classes with initializers or Singletons are also perfectly reasonable too! Just ask yourself if the given class needs an update every frame.
- GetComponent is not a quick lookup. Try to limit its use. (Script Optimizations)
- If you do have non-UI MonoBehaviour objects (and you probably do) useGUILayout should be false. Learn about it.
- If you make a class a MonoBehaviour solely to be capable of running Coroutines consider this alternative: Have a centralized MonoBehaviour be responsible for starting Coroutines in your non-MonoBehaviours.
public delegate IEnumerator CoroutineMethod();
IEnumerator RunCoroutine(CoroutineMethod coroutineMethod)
{
return coroutineMethod();
}
public void StartCoroutineDelegate(CoroutineMethod coroutineMethod)
{
StartCoroutine("RunCoroutine", coroutineMethod);
}
Be sure to read about StartCoroutine and StopCoroutine to learn more!
sweet little post, didnt know about the useguilayout, they should really default that to false (how many objects really have gui on them)
ReplyDeleteI have a suggestion for an improvement, StartCoroutineDelegate should return Coroutine instead of void, this allows you to yield return when calling it, good for chaining.
ReplyDeletepublic Coroutine startCoroutine(CoroutineMethod coroutineMethod){
return StartCoroutine("executeCoroutine",coroutineMethod);
}
Thanks for the above. It seems to be exactly what I need. It's just that I'm a noob to Unity and C#, so I have no idea how to declare the coroutinemethod that gets passed into these functions. Any hints?
ReplyDeletehi.
ReplyDeleteand about stopCoroutine!!!