Unity Coding Tips Part 1: Events and Delegates
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 (prime31.com many of which employ events for dispatching asynchronous operations.
Video Tutorial Rating: 5 / 5
Don’t forget to check out our other video tutorials or share this video with a friend.
19 responses to “Unity Coding Tips Part 1: Events and Delegates”
Leave a Reply Cancel reply
Video Tutorials
Bringing you the best video tutorials for Photoshop, Illustrator, Fireworks, WordPress, CSS and others.
Video tutorial posted 10/11/12
Category: C# Tutorials
Pages
Random Videos
Video Categories
- 3DS Max Tutorials (150)
- After Effects Tutorials (160)
- C# Tutorials (121)
- Colour (6)
- Crazy Effects (1)
- CSS Tutorials (120)
- Dreamweaver Tutorials (139)
- Excel Tutorials (127)
- Featured (10)
- Fireworks Tutorials (131)
- General Effects (9)
- HTML Tutorials (143)
- Illustration and Vector (1)
- Illustrator Tutorials (174)
- IMove Tutorials (119)
- Lightroom Tutorials (145)
- People and Faces (3)
- Photoshop Tutorials (169)
- Text Effects (7)
- Uncategorized (32)
- WordPress Tutorials (140)
Tags
VideoTutorials.co.uk
-
Videotutorials.co.uk offers the web's best Photoshop tutorials, Illustrator video guides, CSS and HTML tutorials and much more all in one place. With new videos being added every day, you can learn how to master software and code to give your images and website some great effects.
Our site has beginner, intermediate and advanced video tutorials of varying lengths so you can learn a new skill at your own speed and level. So whether you're a web designer, print designer or just wanting to edit some family photos, you can get to it quickly and easily using these free video guides. (more)
I just stumbled on this from the iTween website, and I wanted to thank you guys for putting this out here. Events are something I’ve struggled with in other languages and being new to C# having a clear discussion of what is a very elegant solution is great. Thank you!
Thanks ;D
I really want to thank you for this video - first time i really understood delegates and Events, thanks a lot!!
I want to create a sound effects manager that manages all the audio in my scene.
However, instead of having multiple listeners and one manager firing off the events to all of them, like in your video, the way I am doing it will yield one listener listening to multiple eventManagers in a scene. It’s basically backwards.
While it seems to work, is this an incorrect usage of Events and Delegates?
Observer pattern! Isn’t this the most important game development pattern? Surprised there aren’t more Unity videos about it.
Hi, great tuto, but I have got 2 questions :
Why use the “event” keyword since it works without it ?
Wen you click on the buttons several times, it seems from the console output that the methods are called only once ? Why is that ?
Thanks.
Your post is filled with so much “WTF”, I don’t even know where to start with a response.
Thank you, I’m just learning and this was just what I needed to get buttons from your UIToolkit to do something. I have my eye on a few of your plugins too. Thanks again. 🙂
Many thanks for including your opinion just as we did in the video. We are making tutorial videos and thus would like those watching to be using the best tool. The advantages of C# over JS are concrete and there are many of them but that is a discussion too large for here. Some advantages are:
- TONS of C# learning/documentation resources
– language features that are not available in JS
– no sneaky compiled code (look at compiled output of JS transform.position.x = 3 as an example)
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.
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 🙂
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!
So what I am doing for now is having a – public static bool varName – that I can set to true or false from anywhere/any script, then if this static var is true my event is set to true (triggered) in the class that declared the event.
Is this ok, or maybe I am overusing static variables and it would be better/safer to trigger events in a different way? Really happy about this video tutorial, I started using events/delegates only after watching this. Thanks