Hello All:
In this post, I am going to explain how to create a simple game event manager in Unity3d.
For this post you need basic idea of Events and Delegates in C#. You can refer to this tutorial to have an idea of events and delegates in C#.
Let's start..
First we will create a class which will serve as argument to our events.
Let us call it GameEventArgs
This class will have Two event GameStart and GameOver to notify start of the game and end of the game respectively.
In this post, I am going to explain how to create a simple game event manager in Unity3d.
For this post you need basic idea of Events and Delegates in C#. You can refer to this tutorial to have an idea of events and delegates in C#.
Let's start..
First we will create a class which will serve as argument to our events.
Let us call it GameEventArgs
public class GameEventArgs : EventArgs { public float CurrentTime { get; set; } }Then we will create a static class which will have a delegate which accepts GameEventArgs as parameter.
This class will have Two event GameStart and GameOver to notify start of the game and end of the game respectively.
public static class GameEventManager { public delegate void GameEvent(GameEventArgs eventArgs); public static event GameEvent GameStart, GameOver; public static void TriggerGameStart(GameEventArgs eventArgs = null) { if (GameStart != null) { GameStart(eventArgs); } } public static void TriggerGameOver(GameEventArgs eventArgs = null) { if (GameOver != null) { GameOver(eventArgs); } } }Now we will create a Player which subscribes to our event manager.
public class Player : MonoBehaviour { bool isStarted; public float rotationSpeed = .1f; private float nextChange; // Use this for initialization void Start() { GameEventManager.GameStart += new GameEventManager.GameEvent(GameEventManager_GameStart); GameEventManager.GameOver += new GameEventManager.GameEvent(GameEventManager_GameOver); } void GameEventManager_GameOver(GameEventArgs eventArgs) { isStarted = false; Debug.Log("Game over at time: " + eventArgs.CurrentTime); } void GameEventManager_GameStart(GameEventArgs eventArgs) { Debug.Log("Game started at time: " + eventArgs.CurrentTime); isStarted = true; } // Update is called once per frame void Update() { if (isStarted) { if (Time.time > nextChange) { { transform.Rotate(Vector3.up * Random.Range(-180, 180), Space.Self); nextChange = Time.time + rotationSpeed; } } } } }Now we just need to trigger game start and game play, which we can do this simple script.
public class GamePlay : MonoBehaviour { // Use this for initialization void Start() { StartCoroutine(GamePlayer()); } private IEnumerator GamePlayer() { ///Waiting for 1 second before staring the game yield return new WaitForSeconds(1); GameEventManager.TriggerGameStart(new GameEventArgs { CurrentTime = Time.time }); ///Waiting for 5 second before staring the game yield return new WaitForSeconds(5); GameEventManager.TriggerGameOver(new GameEventArgs { CurrentTime = Time.time }); } }Complete working application for this tutorial can be downloaded from here. Do let me know if this was helpful.
Thanks for printing this post. Hope you liked it.
Keep visiting and sharing.
Thanks,
Ashwani.
Keep visiting and sharing.
Thanks,
Ashwani.
Very good, thank you!!
ReplyDelete