GameFreamWork framework -- Application of event system

Event system is widely used, which is very helpful for processing player data (player gold, experience, level), making data call many times, reducing ...

Event system is widely used, which is very helpful for processing player data (player gold, experience, level), making data call many times, reducing coupling

Apply in unity (demonstrate by changing player's gold coins);

1) register to listen

2). Remove monitoring

3) when the gold coin changes, inform each interface

Operation:

1. Import the three scripts of Event into the project;

2. write a script, PlayerInforManagerTest. The main function of script is to store user data. Other scripts need to be invoked in the script when they need data.

1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class PlayerInfoManagerTest { 6 7 #region Singleton mode 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 22 private PlayerInfoManagerTest() { } 23 #endregion 24 25 26 private int playerGold; 27 28 public int PlayerGold { 29 30 get { return playerGold; } 31 32 set { 33 //Previous player gold value != Set the value 34 if (playerGold != value) 35 { 36 playerGold = value; 37 //Interface to register the change of current gold coin when the value changes 38 EventDispatcher.TriggerEvent<int>(EventKey.OnPlayerGoldChange, playerGold); 39 40 } 41 42 43 44 } 45 } 46 47 48 49 }

3). Add the Key to change the data in the EventKey script of the event system

4). Write a script EventTest, which is used to call the event system as a change of data. It is equivalent to that when a store purchases (sells) equipment, the gold coin will be reduced (increased), and the player informanagertest data center will be informed to update the data, so that the data will be consistent when other scripts (for example, the player backpack displays gold coin) call the player informanagertest

1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 6 7 public class EventTest : MonoBehaviour { 8 9 public Text goldText; 10 11 // Use this for initialization 12 void Start() 13 { 14 EventDispatcher.AddEventListener<int>(EventKey.OnPlayerGoldChange, OnPlayerGoldValueChange); 15 } 16 17 void OnPlayerGoldValueChange(int gold) 18 { 19 goldText.text = gold.ToString(); 20 } 21 22 // Update is called once per frame 23 void Update() { 24 25 } 26 private void OnDestroy() 27 { 28 EventDispatcher.RemoveEventListener<int>(EventKey.OnPlayerGoldChange, OnPlayerGoldValueChange); 29 30 } 31 32 public void OnClickToAddGold() 33 { 34 PlayerInfoManagerTest.Instance.PlayerGold += 100; 35 } 36 }

5). Add button and gold Text in the unit, and mount the script

4 December 2019, 06:09 | Views: 1356

Add new comment

For adding a comment, please log in
or create account

0 comments