EventsSystems(Unity3D)事件系统
当游戏的某一对象属性改变时,全局需要用到或已经注册该属性的地方都要即时改变,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 () { } }
完成.