Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Does setting a change which is already in place actually do anything?

Discussion in 'Scripting' started by Tarodev, Aug 20, 2019.

  1. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    190
    Let me explain:

    Say we have two update loops. In one of the loops I have this:

    Code (CSharp):
    1.  _image.color = Input.GetMouseButton(0) ? Color.grey : Color.white;
    And in the other one we have:

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0)) _image.color = Color.grey;
    2.  
    3. if (Input.GetMouseButtonUp(0)) _image.color = Color.white;
    Now, I'm not saying either one is how you should handle this, but I've always wondered. The real question here is if Unity will be setting the color every frame even if it's the same, or alternatively, it could be a transform position or a sprite assignment on an image.

    EDIT: Mistake picked up by Antistone.
     
    Last edited: Aug 20, 2019
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I suspect you are confusing GetMouseButtonDown and GetMouseButton. GetMouseButton tells you whether the button is down right now. GetMouseButtonDown tells you whether the button just switched from being up to being down during the last frame.

    Consequently, your two code examples have different behavior; the first one makes the button gray for one frame when the mouse is clicked, while the second makes it gray until the mouse is released.

    Assuming that you changed the first example to use GetMouseButton instead, then they'd have similar net results, but since the second example is only looking for changes, it might fail if the script is somehow not running when the mouse changes. For instance, if your component were disabled and then re-enabled, but the mouse was pressed/released while it was turned off, then your first example would change to the correct color as soon as it was turned back on, while the second would only change the next time the mouse was pressed/released.

    The first example does cause the color to be set every frame, but that doesn't necessarily mean it's more expensive. You could use the profiler to try to compare the two, but my guess is that you'd find there is no significant difference. Unity redraws the entire screen every frame anyway, so drawing a new color isn't necessarily any more expensive than drawing the same color. Additionally, your second example calls two functions on the Input class instead of one, and has two code branches instead of one--those things have (small) performance costs, too.
     
    Tarodev likes this.
  3. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    190
    Ahhhh, yes the code above was meant to be GetButton. I'll change it for clarity.

    So to summarize, Unity draws every frame regardless, so changing the color is effectively no different performance-wise and the first example would be faster simply because the second one checks Input twice.

    Thank for that!
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I don't know that the first one is faster. This stuff can be really hard to guess. If it's important, you should test it.

    But I think it's very unlikely that you will notice a performance difference either way.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It would depend on what code Unity runs whenever _image.color is set, if any. Unity might do a bunch of work whenever it is set, or Unity might every once in a while just check if _image.color has changed to a new value and if so do some work. I have no idea.

    If you are really concerned, I would put setting _image.color in a loop setting it hundreds of times per frame. First try setting it to the same value each time, then rerun the test flipping it between different values. Profile both and compare.

    But if you were just setting a bool or an int to the same value each update, instead of _image.color, I'd say the performance hit would be so trivial you probably couldn't even see it on the profiler vs just setting when the value should actually change.
     
  6. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    190
    I'm not really concerned, it's just something which gets done so often I'd just like to know which method is more performant. But you're both right, it'd be the difference between doing it 500,000 times a second as opposed to 550,000 times a second... Nothing to even think about, I suppose.