For all Unity developers and developers-to-be, both beginners and professionals!
Here I demonstrate how to create delegates, events and singletons to work in conjunction. This tutorial is written for Unity3D, However, similar code can be used for any C# or .NET application.
Posted by vicestudios on Dec 10th, 2012
Intermediate Client Side Coding.
Why would I want to know this?
As a young self-taught programmer, I often found myself writing tons and tons of boolean statements to determine if some event or action has happened. I listen to those events through Coroutines and other methods to return values. If you find yourself doing this as well, STOP IT!
Welcome to Events…
Lately, I’ve been trying to improve on my C# programming skills and found myself lacking knowledge of the basic understand for Events. So, while looking through numerous tutorials on MSDN and other blogs, I found most tutorials to be over complicated and lush with convoluted code not pertinent to the core concept. I don’t want this to happen to you!
With that said I will try to explain the basics of Events and how they are used in a project…
If you don’t know what a Singleton is, you probably should. A Singleton is a script that cannot be an Instanced or ‘Duplicated’. It is, well… Single.
I recommend using Singletons for things that do not need to be copied multiple times during a game. Such as an Inventory System. Typically, the player only needs one Inventory, so we don’t want to call Instances of it, we only want one. And when we call it, we want to make sure it doesn’t get duplicated.
There are many ways to create Singletons, but this method is often used because it’s simple…
Here, I have a class ‘Clicker’ that is attached to my Camera. This class handles all clicks that I send into 3D Space with a Raycast.
To access my ‘DoSomething’ method from another script, I simply call…
This eliminates the need to use a bunch of Static Method and Variable calls, plus gives us one instance only!
Delegate and Event?
A Delegate can be thought of as a reference pointer to an object/method. When it gets called, it notifies all methods that reference the delegate.
So, first things first…
Define a Delegate and the method that gets called when it fires…
The delgate called ‘OnClickEvent’ passes a ‘GameObject’ that we can use to define what game object it came from. Then, we defined an ‘event’ OnClick that gets called when the delegate is called.
Now, in the same script, we need to call the delegate and pass it our GameObject. I’ve done this through a Raycast…
As you can see, if the Ray has contact an Object in the scene and we Left-Mouse-Click, we call the event and pass the GameObject.
The last thing we must do is reference the delegate from our other scripts that are listening to the call. For this I’ve created a class called ‘GoldPile’.
In our Awake() method, we’ve defined our listening Event and assigned a local method that gets called ‘OnClick’. ‘OnClick’ does not need to be the same as our delegate method, but it can be.
Note: In our previous post we added a Singleton to our Clicker class. This allows us to use Clicker.Instance
As you can see, we’ve also created the OnClick() method that passes our GameObject we clicked on.
Note: You must use if (g == gameObject), otherwise it will hide other instances of that method in the scene as well… This is why we pass the GameObject for reference!
Now you are free to add this method to any other script in your game if needed. Don’t forget to define the method and delegate in your Awake().
You can download the complete project for your reference HERE. Enjoy! :)