Upload

Unity Coding Tips Part 1: Events and Delegates

prime31studios prime31studios·56 videos
1,116

Subscription preferences

Loading...

Loading icon Loading...

Working...
24,491
Like     Dislike 0

Sign in to YouTube

Sign in with your Google Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to like prime31studios's video.

Sign in to YouTube

Sign in with your Google Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to dislike prime31studios's video.

Sign in to YouTube

Sign in with your Google Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to add prime31studios's video to your playlist.

Uploaded 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.

Loading icon Loading...

Loading icon Loading...

Loading icon Loading...

The interactive transcript could not be loaded.

Loading icon Loading...

Loading icon Loading...

Ratings have been disabled for this video.
Rating is available when the video has been rented.
This feature is not available right now. Please try again later.

Uploader Comments (prime31studios)

  • St4rdog

    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.

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate St4rdog's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate St4rdog's comment.
  • prime31studios

    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.

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate prime31studios's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate prime31studios's comment.
    in reply to St4rdog (Show the comment)
  • Tiziano Cirillo

    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?

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Tiziano Cirillo's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Tiziano Cirillo's comment.
  • prime31studios

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

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate prime31studios's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate prime31studios's comment.
    in reply to Tiziano Cirillo (Show the comment)
  • lucidskater

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

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate lucidskater's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate lucidskater's comment.
  • prime31studios

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

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate prime31studios's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate prime31studios's comment.
    in reply to lucidskater (Show the comment)

Top Comments

  • prime31studios

    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)

    · 5

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate prime31studios's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate prime31studios's comment.
    in reply to juicymangoze (Show the comment)
  • Sinister Rainbow

    This is by far the best tut i've seen on the topic i've seen in looking around for the last hour. For someone coming to Unity/C# from other game engines this satisfies..

    · 2

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Sinister Rainbow's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Sinister Rainbow's comment.

All Comments (51)

Sign in now to post a comment!
  • dylophosaurus

    would you mine clarifying on "abstracting input" please? thanks :)

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate dylophosaurus's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate dylophosaurus's comment.
    in reply to Mike Desaro (Show the comment)
  • Jamie Lowes

    Thanks for this! Totally didn't know about events until I saw them in the Prime31 GameCenter plugin for Unity this morning. Now it's all working. SuperInternetHighFiveâ„¢!! :D

    Keep up the good work!! :D

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Jamie Lowes's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Jamie Lowes's comment.
  • Mike Desaro

    It isn't too performance heavy but Input is already global so it doesn't make much sense. If you are abstracting input (which is required for mobile + desktop) then it makes more sense to use an event.

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Mike Desaro's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Mike Desaro's comment.
    in reply to n3v4k (Show the comment)
  • n3v4k

    Would it be a good practice to notify other GameObjects about the value of (for example) Input.GetAxis() using an event inside the Update() funciton? or it is too performance heavy to fire events at every frame?

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate n3v4k's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate n3v4k's comment.
  • Mike Desaro

    A static variable is absolutely not a replacement for an event.

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Mike Desaro's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Mike Desaro's comment.
    in reply to deltamish (Show the comment)
  • deltamish

    Nice tutorial.but you can achieve same effect by just declaring a static variable right or am i missing something here(Did i sound rude there if yes i ma really sorry)

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate deltamish's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate deltamish's comment.
  • oioichuck

    Thanks for this tutorial. Extremely useful.

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate oioichuck's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate oioichuck's comment.
  • kang hoo

    Thanks you very much.i hope it have more example ....

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate kang hoo's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate kang hoo's comment.
  • Patrick Davis

    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!

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Patrick Davis's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Patrick Davis's comment.
  • Loading comment...
Loading...
Advertisement
Loading...
Working...
Sign in to add this to Watch Later