10
2015
Simple Event & Delegate Script
Just a quick test on events & delegates (taken from this live tutorial)
– New scene
– Add Sphere object to the scene (and move it into Main Camera view, for example 0,0,0)
– Create new script: FireEvent.cs
using UnityEngine; using System.Collections; public class FireEvent : MonoBehaviour { public delegate void Clicked(); public event Clicked WasClicked; void OnMouseDown() { if (WasClicked!=null) // we have someone listening { Debug.Log("We have a listener, send event"); WasClicked(); }else{ Debug.Log("Nobody is listening!!!! noooooo"); } } }
– Attach “FireEvent.cs” script into Sphere gameobject
– Create new script: Listener.cs
using UnityEngine; using System.Collections; public class Listener : MonoBehaviour { public GameObject EventManager; // reference to event firing object void Start() { // subscribe to the event (attach listener) EventManager.GetComponent<FireEvent>().WasClicked+=SomethingWasClickedOutside; // call this function if event is fired } void SomethingWasClickedOutside() { Debug.Log ("Listener.cs: Event was fired!"); } }
– Create new Empty Gameobject
– Attach “Listener.cs” to that empty gameobject
– Assign Sphere into the “EventManager” field in the inspector
– Hit play and then click the Sphere with mouse, event should be fired.
More info:
http://unity3d.com/learn/tutorials/modules/intermediate/scripting/events
http://unity3d.com/learn/tutorials/modules/intermediate/scripting/delegates
Related Posts
4 Comments + Add Comment
Leave a comment
Recent posts
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
- Custom Unity Hub Project Template Preview Image/Video (using HTML+CSS in package description)
Recent Comments
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- ffmpeg stream raw video into Unity Texture2D on
How to use Delegates in Unity:
http://unitydojo.blogspot.fi/2015/03/how-to-use-delegates-in-unity-like-boss.html
C# Events and Delegates Made Simple
using UnityEngine.Events, UnityEvent:
http://forum.unity3d.com/threads/unityevent-where-have-you-been-all-my-life.321103/
Delegates and Events in Unity
http://www.unitygeek.com/delegates-events-unity/