Alert icon
We're changing our privacy policy. This stuff matters.  Learn more  Dismiss

Unity Coding Tips Part 1: Events and Delegates

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
7,921
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Feb 6, 2011

The first tutorial in a series aimed at showing some of the more esoteric features available to Unity developers. In this tutorial, we cover the basics of events and delegates. You will learn some simple techniques to make your code a lot more coherent and easier to manage.

This directly relates to our plugins (http://prime31.com/unity), many of which employ events for dispatching asynchronous operations.

Category:

Howto & Style

Tags:

License:

Standard YouTube License

  • likes, 0 dislikes

Link to this comment:

Share to:

Uploader Comments (prime31studios)

  • This seems similar to "CSharpMessenger Extended" (google it). Other than performance is there any reason it's better? I notice for one thing you will get AutoComplete for the event name, instead of having to type a string.

  • @St4rdog its similar but the key difference is that native events are associated with an object whereas the Messenger Extended is a string based publisher/subscriber system. Events will be faster due to skipping the initial string match.

  • Great tutorial, practical and to the point. Thanks!

    I found some generic information about this topic online but your example did it for me, will be using events/delegates from today :) - please do more :) c#/Unity :)

    QUESTION: In term of performance on iOS, are events/delegates (with maybe quite a few events and listeners in a scene...) good to use or is there some performance issue compared to use some other solution to do the same things?

  • @HokutoTC performance will be excellent on mobile. Events are WAY faster than SendMessage and will beat out any alternatives (like subscriber/message solutions).

  • any tips for learning from scratch, i cant seem to find a good tutorial for someone who doesnt know how any of it works

  • @lucidskater a quick web search will get you literally thousands of C# tutorials for beginners. There are also tons of books as well.

see all

All Comments (30)

Sign In or Sign Up now to post a comment!
  • @chemicalvamp actually it isn't - once your C#, JS or Boo scripts are compiled they all run the same - Unity would never have used it otherwise.

  • This is a wonderful tutorial! I went from a modest understanding of listeners and subscriptions to feeling ready to begin using them in complex applications in just 11 minutes. I found it especially helpful that you took time to explain each line and its role using both proper terminology and newbie-friendly explanations. Thanks a bunch!

  • @johnny3734 I used to think the same thing. (I started out with unityscript for unity) but what it comes down to is.. there aren't any resources available for unityscript (unity does not use pure javascript!) and there are myriads of resources available for c#. If you're smart, you'll drop unityscript and use c# asap. You'll thank me later.

  • There's nothing wrong with JavaScript, I think it's the funnest to code out of any.

  • Thanks for this tutorial - I've been hacking around in Unity for a while but have decided to grow up a bit and learn to do some things correctly. Enjoyed the first tutorial - looking forward to following along with the rest :-)

  • to get around having to check for null everytime you fire an event you can give it a single empty delegate so you technically never have zero listeners.

    eg

    static event DelegateThing OnThingEvent = delegate {};

    It's messy but it works well :)

  • @HokutoTC A common method is to put events in a seperate postoffice style class. Each event then has a method to invoke it. So this solves the 'must be called from the same class' problem PLUS allows you to intercept event calls and do sanity checks and debugging easier. In fact ReSharper for visual studio will write the invoker function for you (maybe plain VS does too).

    The other way around it is to NOT use events, just delegates alone which don't have those restrictions, but it's more risky

  • You forgot to mention C# is 20 times faster than java script.

  • Top stuff!

  • Top stuff!

    

Loading...

Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more