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");
    	}
    }
    
  • 相关阅读:
    Struts2的%,#,$的区别,UI标签及其表单radio,checkbox,select回显数据(七)
    Struts2的控制标签库和数据标签库(六)
    Struts2从后端向前端传递数据和OGNL访问用户自定义静态方法(五)
    两个小例子登录和显示全部用户信息的模块(四)
    Struts2的ServletAPI的获取和各种类型的数据获取(三)
    Action的三种实现方式,struts.xml配置的详细解释及其简单执行过程(二)
    Struts2的 两个蝴蝶飞,你好 (一)
    虚拟机安装Centos7系统后优化操作
    Java进程故障排查(CPU资源占用高,接口响应超时,功能接口停滞等)
    zabbix企业微信报警实现
  • 原文地址:https://www.cnblogs.com/hehao98/p/9244912.html
Copyright © 2011-2022 走看看