usingUnityEngine;publicclassCoinCollectedListener:MonoBehaviour{publicCoinCollectedEvent coinCollectedEvent;privatevoidOnEnable(){
coinCollectedEvent.OnCoinCollected.AddListener(RespondToCoinCollected);}privatevoidOnDisable(){
coinCollectedEvent.OnCoinCollected.RemoveListener(RespondToCoinCollected);}privatevoidRespondToCoinCollected(){
Debug.Log("Coin collected!");// Increase player's score, update UI, etc.}}publicclassHealthChangedListener:MonoBehaviour{publicHealthChangedEvent healthChangedEvent;privatevoidOnEnable(){
healthChangedEvent.OnHealthChanged.AddListener(RespondToHealthChanged);}privatevoidOnDisable(){
healthChangedEvent.OnHealthChanged.RemoveListener(RespondToHealthChanged);}privatevoidRespondToHealthChanged(int newHealth){
Debug.Log("Health changed to: "+ newHealth);// Update UI, check for game over conditions, etc.}}
3. 触发事件:
现在,让我们创建将触发这些事件的组件。
usingUnityEngine;publicclassPlayerController:MonoBehaviour{publicCoinCollectedEvent coinCollectedEvent;publicHealthChangedEvent healthChangedEvent;privateint health =100;privateint score =0;// Called when the player collects a coinpublicvoidCollectCoin(){
score +=10;
coinCollectedEvent.TriggerEvent();}// Called when the player's health changespublicvoidChangeHealth(int amount){
health += amount;
healthChangedEvent.TriggerEvent(health);}}
usingUnityEngine;usingUnityEngine.Events;publicclassPlayerController:MonoBehaviour{// Define UnityEvents for coin collected and health changedpublicUnityEvent onCoinCollected;publicUnityEvent<int> onHealthChanged;privateint health =100;privateint score =0;// Called when the player collects a coinpublicvoidCollectCoin(){
score +=10;
onCoinCollected.Invoke();// Invoke the coin collected event}// Called when the player's health changespublicvoidChangeHealth(int amount){
health += amount;
onHealthChanged.Invoke(health);// Invoke the health changed event with the new health value}}
2. 创建事件监听器:
创建将响应这些事件的事件侦听器组件。
usingUnityEngine;publicclassGameController:MonoBehaviour{privatevoidOnEnable(){// Subscribe to the coin collected eventFindObjectOfType<PlayerController>().onCoinCollected.AddListener(RespondToCoinCollected);// Subscribe to the healthchanged eventFindObjectOfType<PlayerController>().onHealthChanged.AddListener(RespondToHealthChanged);}privatevoidOnDisable(){// Unsubscribe from the coin collected eventFindObjectOfType<PlayerController>().onCoinCollected.RemoveListener(RespondToCoinCollected);// Unsubscribe to the healthchanged eventFindObjectOfType<PlayerController>().onHealthChanged.RemoveListener(RespondToHealthChanged);}privatevoidRespondToCoinCollected(){
Debug.Log("Coin collected!");// Increase player's score, update UI, etc.}privatevoidRespondToHealthChanged(int newHealth){
Debug.Log("Health changed to: "+ newHealth);// Update UI, check for game over conditions, etc.}}
usingUnityEngine;publicclassCoin:MonoBehaviour{privatevoidOnTriggerEnter(Collider other){if(other.CompareTag("Player")){// Call the CollectCoin method of the PlayerController
other.GetComponent<PlayerController>().CollectCoin();Destroy(gameObject);// Destroy the coin object}}}publicclassEnemy:MonoBehaviour{privatevoidOnTriggerEnter(Collider other){if(other.CompareTag("Player")){// Call the ChangeHealth method of the PlayerController
other.GetComponent<PlayerController>().ChangeHealth(-10);// Decrease health by 10Destroy(gameObject);// Destroy the enemy object}}}