Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using UnityEngine.UI;
- namespace Antnomoly
- {
- /// <summary>
- /// This is used to send Event Arguments to all of the classes that are listening
- /// to the "StateChange" event when it is fired.
- /// </summary>
- public class StateChangeArgs : EventArgs
- {
- public StateValues State;
- }
- public class AntnomolyGame : MonoBehaviour
- {
- public event EventHandler<StateChangeArgs> StateChange;
- public Text GameText;
- int breath;
- int strength;
- int timer;
- private StateValues state;
- public StateValues State
- {
- get { return state; }
- set
- {
- state = value;
- // This method will now take care of the code based on your current State.
- StateMethod ( );
- // Fire the StateChange event so that all listeners can then react.
- OnStateChange ( );
- }
- }
- void Awake ( )
- {
- // When you call the lower case "state", your referencing the local, private variable.
- // If you referenced the uppercase "State", you'd be calling the State property method,
- // and firing the "StateChange" event.
- state = StateValues.None;
- }
- /// <summary>
- /// When the state changes, it calls this method. This method is used to keep all of your
- /// game code seperate from your "State" property method, nothing more.
- /// </summary>
- private void StateMethod ( )
- {
- // Do something based on your current state
- switch ( state )
- {
- case StateValues.beginning:
- // Do your beginning code...
- break;
- }
- }
- /// <summary>
- /// This is the Event method that will notify all listeners that the state has been changed.
- /// </summary>
- private void OnStateChange ( )
- {
- StateChange?.Invoke ( this, new StateChangeArgs ( ) { State = this.state } );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment