zoukankan      html  css  js  c++  java
  • Building Robust and Flexible Event System in Unity3D

    Building Robust and Flexible Event System in Unity3D

    1. Prerequisites

    1.1 Observer Pattern

    According to Wikipedia, the observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. (I omitted the definition in the Gang of Four book since it's not easy to understand)

    Observer Pattern is often used in game when you need to build a event system. A event system is capable of notifying other classes about some specific events (player died, boss slaughtered, key item found, ...).

    1.2 Observer Pattern in C# Language

    Observer pattern is so commonly used that C# supports observer pattern at language level, which saves us a lot of labor and potential trouble in implementing it.

    1.2.1 The delegate keyword

    delegate is a special data type in C# representing functions. It's similar to function pointer in C, but has the advantage of multicasting, which allows combining different functions into a single variable.

    delegate void MyDelegate(int num);
    public void UseDelegate() {
        MyDelegate myDelegate = f;
        myDelegate += g;
        myDelegate();
    }
    public void f(int a) {...}
    public void g(int b) {...}
    

    1.2.2 The event keyword

    The event data type is just similar to the delegate data type, but it doesn't allow any modification other than adding and removing observers to/from the event variable.

    public delegate void ClickAction();
    public static event ClickAction OnClicked;
    OnClicked += button1.ProcessClicked;
    OnClicked += scene.ProcessClicked;
    OnClicked -= button1.ProcessClicked;
    OnClicked -= scene.ProcessClicked;
    

    An important thing to notice is that a function MUST be removed from the event variable if the object it belongs to has been disabled or garbage collected, otherwise erroneous behaviors may occur.

    2. Example Usage

    Suppose we have a boss in game that we can slaughter it to win the level. When the boss died three class need to response to it. The player should cheer, the UI should display message and the game should end a few seconds later. Then we can arrange our code as follows:

    public class GameManager : MonoBehaviour {
    	// Manage Events
    	public delegate void BossSlaughteredAction();
    	public static event BossSlaughteredAction bossSlaugheredAction;
    	public static void OnBossSlaughtered() {
    		if (bossSlaugheredAction != null) bossSlaugheredAction();
    	}
    	void OnEnable() {
    		bossSlaugheredAction += HandleBossSlaughtered;
    	}
    	void OnDisable() {
    		bossSlaugheredAction -= HandleBossSlaughtered;
    	}
    	public void HandleBossSlaughtered() {
    		//Debug.Log("Boss Slaughtered!");
    		DisplayTextAtScreen("猎得传奇猎物,游戏结束!", 5.0f);
    		Invoke("ProcessGameEnding", 5.0f);
    	}
        void ProcessGameEnding() {
    		UnityEngine.SceneManagement.SceneManager.LoadScene("StartMenu");
    	}
    }
    
    public class BeerAttributes : MonoBehaviour {
    	[SerializeField] float health = 100.0f;
    	void TakeDamage(float amount) {
    		health -= amount;
    		if (health <= 0) {
    			this.enabled = false;
    			//Debug.Log("Die");
    			GameManager.OnBossSlaughtered();
    		}
    	}
    }
    
    public class WolfEventHandler : MonoBehaviour {
    	void OnEnable() {
    		GameManager.bossSlaugheredAction += HandleBossSlaughtered;
    	}
    	void OnDisable() {
    		GameManager.bossSlaugheredAction -= HandleBossSlaughtered;
    	}
    	void HandleBossSlaughtered() {
    		Animator animator = GetComponent<Animator>();
    		animator.SetTrigger("Cheer");
    	}
    }
    
  • 相关阅读:
    java中native的用法
    用uWSGI和Nginx部署Flask项目
    elasticsearch之使用Python批量写入数据
    mysql 远程访问
    Chrome扩展及应用开发
    jQuery ajax
    Chrome扩展及应用开发-储存数据
    Chrome扩展及应用开发-扩展页面间的通信
    Python3 将本地时间转换成指定时区时间
    python如何编译py文件生成pyc、pyo、pyd以及如何和C语言结合使用
  • 原文地址:https://www.cnblogs.com/hehao98/p/9244912.html
Copyright © 2011-2022 走看看