Hello Guest

Author Topic: Sprite button event firing  (Read 3606 times)

dakeese

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Sprite button event firing
« on: August 25, 2012, 03:31:30 am »
Unikron, this isn't so much a usage question as it is me trying to understand a bit better how Unity works, and your tk2dButton is a great example.

I noticed that in tk2dButton, events are called like so:

Code: [Select]
if (ButtonDownEvent != null)
ButtonDownEvent(this);

However, I have read (I'm relatively new to C#), that it is good practice to assign your delegate to a temporary variable and then calling that, in case your last subscriber unsubscribes between checking for null (first line above) and calling the event (second line above).

But in Unity is that an unnecessary precaution?  I'm not sure if co-routines are actually called asynchronously as part of the Update() sequence or if they are happening synchronously in another thread.  Also, I'm thinking maybe it doesn't matter because when you destroy MonoBehaviours, the MonoBehaviours will actually still exist until the Update() step is complete. So in that case you would never have a situation where the last subscriber would disappear right before you try to call an event.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite button event firing
« Reply #1 on: August 25, 2012, 11:59:38 am »
This is mostly an unnecessary precaution. Unity coroutines aren't threads and don't run simultaneously with other functions - each coroutine runs until it yields, one after another. This happens after all the update functions are called, so there's nothing to worry about there.