EventSyste(Unity3D)事件系统
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了EventSyste(Unity3D)事件系统,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3059字,纯文字阅读大概需要5分钟。
内容图文
当游戏的某一对象属性改变时,全局需要用到或已经注册该属性的地方都要即时改变,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 单例模式 8privatestatic PlayerInfoManagerTest instance; 910publicstatic PlayerInfoManagerTest Instance 11 { 12get13 { 14if (instance == null) 15 { 16 instance = new PlayerInfoManagerTest(); 17 } 18return instance; 19 } 20 } 21private PlayerInfoManagerTest() { } 22#endregion232425privateint playerGold; 2627privatestring playerName; 2829publicint PlayerGold 30 { 31get { return playerGold; } 32set33 { 34//之前玩家金币数值 != 设置过来的数值35if (playerGold != value) 36 { 37 playerGold = value; 38//数值发生变化 通知注册当前 金币发生变化的 界面39 EventDispatcher.TriggerEvent<int>(EventKeyTest.OnPlayerGoldChange, playerGold); 4041 } 42 } 43 } 44publicstring PlayerName { 45get { return playerName; } 46set { 47//if (!playerName.Equals(true)) 48if (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 initializationvoid 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 framevoid Update() { } privatevoid OnDestroy() { EventDispatcher.RemoveEventListener<int>(EventKeyTest.OnPlayerGoldChange, OnPlayerGoldValueChange); EventDispatcher.RemoveEventListener<string>(EventKeyTest.OnPlayerNameChage, OnPlayerNameChange); } //unityBtn事件publicvoid OnClickToAddGold() { PlayerInfoManagerTest.Instance.PlayerGold += 100; } publicvoid ClickReduceBtn() { PlayerInfoManagerTest.Instance.PlayerGold -= 50; } publicvoid 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); } privatevoid OnPlayerGoldChange(int aaa) { gameObject.GetComponent<Text>().text = aaa.ToString(); } // Update is called once per framevoid Update () { } }
完成.
原文:https://www.cnblogs.com/RainPaint/p/10124051.html
内容总结
以上是互联网集市为您收集整理的EventSyste(Unity3D)事件系统全部内容,希望文章能够帮你解决EventSyste(Unity3D)事件系统所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。