Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

General Question about Performance of Code Snippet

Discussion in 'Scripting' started by AdamGMain, Dec 19, 2021.

  1. AdamGMain

    AdamGMain

    Joined:
    Jun 5, 2019
    Posts:
    8
    Hey all,

    I just had a question about the way I've accomplished something. I am making a simple inventory system, and when an object is picked up I have a little flyout that animates off the screen after a few seconds. This is accomplished with a coroutine and a tween. Because I am instantiating a new instance of the flyout prefab, I wasn't able to define my IEnumerator outside the Add method, so I moved it inside the Add method and it works great.

    My question is, is this ok to do? Performance wise, this isn't a per frame operation. I thought about making a timer for the tween instead of using a coroutine but if this makes sense I'd like to keep it.

    Here is the code:

    Code (CSharp):
    1.    public void Add (Item item)
    2.     {
    3.         var newPanel = Instantiate(panel, inventoryUI);
    4.         var panelText = newPanel.GetComponentInChildren<Text>();
    5.         var panelToMove = newPanel.GetComponent<RectTransform>();
    6.  
    7.         panelText.text = item.displayName;
    8.  
    9.         IEnumerator FlyoutAnimation()
    10.         {
    11.             yield return new WaitForSeconds(3);
    12.  
    13.             panelToMove.DOAnchorPosY(-800f, 1f);
    14.  
    15.             //Wait for Tween and then destroy the object
    16.             yield return new WaitForSeconds(3);
    17.  
    18.             Destroy(newPanel.gameObject);
    19.  
    20.             yield return null;
    21.         }
    22.  
    23.         StartCoroutine(FlyoutAnimation());
    24.  
    25.         items.Add(item);
    26.  
    27.         if (OnItemAddedCallback != null)
    28.         {
    29.             OnItemAddedCallback.Invoke();
    30.         }
    31.     }
    Any input is welcome and I appreciate your time!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    DO NOT OPTIMIZE CODE JUST BECAUSE... If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413
     
  3. AdamGMain

    AdamGMain

    Joined:
    Jun 5, 2019
    Posts:
    8
    Maybe performance wasn't the right angle to come at this from. Let me rephrase:

    In the time I've been learning Unity and C#, I've not come across defining (and redefining every call) an IEnumerator inside of a method. Maybe I just haven't come across it and it's a common thing to do, but I wanted to make sure and see if anyone had any insight regarding potential issues doing it this way.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    I've never seen an issue from coroutines because they're really identical in performance to any other function.

    Here is when Unity calls them, all from the same exact thread that it does everything in your scripts with:

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    Here's some more interesting stuff about coroutines:

    Coroutines in a nutshell:

    https://forum.unity.com/threads/des...en-restarting-the-scene.1044247/#post-6758470

    https://forum.unity.com/threads/proper-way-to-stop-coroutine.704210/#post-4707821

    Splitting up larger tasks in coroutines:

    https://forum.unity.com/threads/bes...ion-so-its-non-blocking.1108901/#post-7135529

    Coroutines are NOT always an appropriate solution: know when to use them!

    https://forum.unity.com/threads/things-to-check-before-starting-a-coroutine.1177055/#post-7538972

    https://forum.unity.com/threads/heartbeat-courutine-fx.1146062/#post-7358312