Building Robust and Flexible Event System in Unity3D
Posted hehao98
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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");
}
}
以上是关于Building Robust and Flexible Event System in Unity3D的主要内容,如果未能解决你的问题,请参考以下文章
手势跟踪论文学习:Realtime and Robust Hand Tracking from DepthCost Function
视觉SLAMDXSLAM: A Robust and Efficient Visual SLAM System with Deep Features
视觉SLAMDXSLAM: A Robust and Efficient Visual SLAM System with Deep Features
SMART: Robust and Efficient Fine-Tuning for Pre-trainedNatural Language Models
论文学习9Prio: Private, Robust, and Scalable Computation of Aggregate Statistics
论文翻译:2020_A Robust and Cascaded Acoustic Echo Cancellation Based on Deep Learning