1. 程式人生 > >EventSyste(Unity3D)事件系統

EventSyste(Unity3D)事件系統

eric destroy game ons 模式 string bsp using 全局

當遊戲的某一對象屬性改變時,全局需要用到或已經註冊該屬性的地方都要即時改變,EventSystem使用通過事件監聽的方法,實現了即時更新屬性的功能

基本框架圖解如下:

技術分享圖片

實現該功能的步驟如下:

1. 需要引用以下三個腳本,(來源於GameFrameWork的GameMain\Scripts\Event文件夾)

技術分享圖片

2.在EventKey中寫入需要註冊事件系統的屬性(作為事件監聽的索引鍵值,單一類),例如:

技術分享圖片

3.創建一個玩家屬性腳本(即圖解中的屬性管理區);例如

 1 using System.Collections;
 2 using
System.Collections.Generic; 3 using UnityEngine; 4 5 public class PlayerInfoManagerTest 6 { 7 #region 單例模式 8 private static PlayerInfoManagerTest instance; 9 10 public static PlayerInfoManagerTest Instance 11 { 12 get 13 { 14 if (instance == null)
15 { 16 instance = new PlayerInfoManagerTest(); 17 } 18 return instance; 19 } 20 } 21 private PlayerInfoManagerTest() { } 22 #endregion 23 24 25 private int playerGold; 26 27 private string playerName; 28 29 public
int PlayerGold 30 { 31 get { return playerGold; } 32 set 33 { 34 //之前玩家金幣數值 != 設置過來的數值 35 if (playerGold != value) 36 { 37 playerGold = value; 38 //數值發生變化 通知註冊當前 金幣發生變化的 界面 39 EventDispatcher.TriggerEvent<int>(EventKeyTest.OnPlayerGoldChange, playerGold); 40 41 } 42 } 43 } 44 public string PlayerName { 45 get { return playerName; } 46 set { 47 //if (!playerName.Equals(true)) 48 if (playerName!=value) 49 { 50 playerName = value; 51 EventDispatcher.TriggerEvent<string>(EventKeyTest.OnPlayerNameChage, playerName); 52 } 53 } 54 } 55 }

4.創建事件管理腳本,用於註冊事件,並寫入事件發生時執行的方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class EventTest : MonoBehaviour {

    public Text goldText;
    public Text playerNameText;

    private string[] names = {"張三","李四","王五","趙柳","二哈","狗子" };
    // Use this for initialization
    void Start()
    {
        EventDispatcher.AddEventListener<int>(EventKeyTest.OnPlayerGoldChange, OnPlayerGoldValueChange);
        EventDispatcher.AddEventListener<string>(EventKeyTest.OnPlayerNameChage, OnPlayerNameChange);
    }

    void OnPlayerGoldValueChange(int gold)
    {
        goldText.text = gold.ToString();
    }
    void OnPlayerNameChange(string name)
    {
        playerNameText.text = name;
    }
    // Update is called once per frame
    void Update() {

    }
    private void OnDestroy()
    {
        EventDispatcher.RemoveEventListener<int>(EventKeyTest.OnPlayerGoldChange, OnPlayerGoldValueChange);
        EventDispatcher.RemoveEventListener<string>(EventKeyTest.OnPlayerNameChage, OnPlayerNameChange);
    }  
    //unityBtn事件
    public void OnClickToAddGold()
    {
        PlayerInfoManagerTest.Instance.PlayerGold += 100;
    }
    public void ClickReduceBtn() {
        PlayerInfoManagerTest.Instance.PlayerGold -= 50;
    }

    public void ClickChangeNameBtn() { 
        PlayerInfoManagerTest.Instance.PlayerName=names[Random.Range(0,names.Length)];
    }
}

5.創建需要註冊監聽玩家屬性改變的界面的腳本;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TextPlayerGold : MonoBehaviour {

    // Use this for initialization
    void Start()
    {

        EventDispatcher.AddEventListener<int>(EventKeyTest.OnPlayerGoldChange, OnPlayerGoldChange);
    }

    private void OnPlayerGoldChange(int aaa)
    {
        gameObject.GetComponent<Text>().text = aaa.ToString();
    }
    // Update is called once per frame
    void Update () {
        
    }
}

完成.

EventSyste(Unity3D)事件系統