- Home /
Is there any performance differences between Coroutine methods ?
Hello everyone, I want to ask a real simple question. I am basically writing a script for zoom in & out according to the mouse input. I could just put all the code into the Update and lerp the object through boolean checks, but the problem is I need to be extra careful about script optimization in this project. So I avoid using update a lot, which means I use coroutines in order to lerp the object for zooming in & out. My problem is, I want the user to be able to have quick responses on mouse click. For example, right click starts zooming, but if I click it again in the middle of zooming in, I want it to zoom out, I don't want it to wait for the first coroutine to finish zooming. So I need to use StopCoroutine. For example :
if(Input.GetButtonDown("Fire2"))
{
pressedZoomButton = !pressedZoomButton;
if(pressedZoomButton)
{
StopCoroutine("ZoomOut");
StartCoroutine("ZoomIn");
}
else{
StopCoroutine("ZoomIn");
StartCoroutine("ZoomOut");
}
}
It works fine, no problems, but at first I tried to do it by using the method name, then I learned in order the StopCoroutine function to work it needs to be used with string method, also it needs to be used on a method which is also called with string method. I did it, it works, but I got a little bit suspicious, is using these coroutine string methods costy ? Will it cause any performance problems, because as far as I know strings are the things that we need to approach carefully.
Also really what is the difference between calling it by method name and by string, I mean technically ?
Historically, the call with string allowed to stop the coroutine so that would have been the reason to use it. Now that the latest updates include the StopCoroutine(IEnumerator), the use of string may become obsolete pretty soon.
Answer by BenouKat · Feb 26, 2015 at 08:00 PM
If you check the documentation : http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
It specifies that the Coroutine with a string gets a higher overhead runtime when you start it.
Despite this, no, there's not really a technical difference. It's just 2 ways to manage them, one is more costly at start.
Answer by DanSuperGP · Feb 26, 2015 at 08:20 PM
It's worth noting that, in Unity 5 you can store a reference to a running coroutine and stop it using the reference rather than a string. This will be more efficient than using strings.
This code throws an error in 4.6 and works perfectly in Unity 5
using UnityEngine;
using System.Collections;
public class CoroutineTest : MonoBehaviour {
public bool doOnce;
public float TimeFromStart = 5;
Coroutine coLoop;
// Use this for initialization
void Start () {
coLoop = StartCoroutine(CoroutineLoop());
}
// Update is called once per frame
void Update () {
if(Time.time > TimeFromStart && ! doOnce)
{
Debug.Log ("StoppingCoroutine");
StopCoroutine(coLoop);
doOnce = true;
}
}
IEnumerator CoroutineLoop()
{
while(true)
{
yield return null;
Debug.Log("Still Running");
}
}
}
Didn't know it (didn't check Unity 5 yet).
Good they finally did it.
Hmm.. It was throwing an error for me in 4.6.2 but not in 5...
Answer by tomhendriks1102 · Sep 28, 2017 at 03:35 PM
might not totaly be what you need in this case, but you could use a number or boolean to change with mouseclicks in your update and have that coroutine check the value to know if it should zoom in/out or not zoom. this way u dont have to start/stop coroutines and just have one running all the time basically "listening" for a new command to zoom again.