Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class DelegateExample : MonoBehaviour {
- //the "method templates" that are used
- public delegate void enterCombatEvent();
- public delegate void exitCombatEvent();
- //the listeners for the events
- public static enterCombatEvent OnEnterCombat;
- public static exitCombatEvent OnExitCombat;
- private bool isInCombat;
- //Call this method when the player encounters an enemy
- public void EnterCombat() {
- isInCombat = true;
- //call the method, all scripts that have a listener will be called
- OnEnterCombat?.Invoke();
- }
- //Call this method when there are no more enemies
- public void ExitCombat() {
- isInCombat = false;
- //call the method, all scripts that have a listener will be called
- OnExitCombat?.Invoke();
- }
- }
- public class OtherScript : MonoBehaviour {
- // Use this for initialization
- void Start () {
- DelegateExample.OnEnterCombat += CombatStart;
- DelegateExample.OnExitCombat += CombatEnd;
- }
- void OnDestroy() {
- DelegateExample.OnEnterCombat -= CombatStart;
- DelegateExample.OnExitCombat -= CombatEnd;
- }
- //this is called when the player enteres combat
- void CombatStart () {
- //start combat music
- }
- //this is called when the player leaves combat
- void CombatEnd () {
- //start casual music
- }
- }
Add Comment
Please, Sign In to add comment