原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2018/1122/206.html
事件分发机制也被称之为事件监听派发系统,在每个游戏框架中都是不可或缺的一个模块。起作用主要是用来解耦,监听网络消息,或者做一些异步的操作等,下面要和大家介绍的是使用C#在Unity游戏开发中的事件分发处理机制的实现,我们常州微信小程序开发专家-幻天网络希望能帮到大家。
实现类似代码能,金和能量的加减操作
using UnityEngine;
using System.Collections;
public class WindowCtr : MonoBehaviour {
public CtrEnergy energyCtr;
public CtrMoney moneyCtr;
public Money money;
public Energy energy;
// Use this for initialization
void Start () {
//UIButton[] energyCtrArr = energyCtr.btnArr;
// UIButton[] moneyCtrArr = moneyCtr.btnArr;
money = GetComponentInChildren<Money>();
energy = GetComponentInChildren<Energy>();
moneyCtr.clickCall += Click2;
energyCtr.clickCall += Click1;
}
// Update is called once per frame
//体力
void Click1 (GameObject go) {
Debug.Log("----体力-----");
energy.SetEnergy(go.name);
}
//金钱
void Click2(GameObject go)
{
Debug.Log("金钱");
money.SetMoney(go.name);
}
}
using UnityEngine;
using System.Collections;
public class CtrMoney : MonoBehaviour {
public UIButton[] btnArr;
//UIEventListener.VoidDelegate
//声明一个委托类型
public delegate void VoidCall(GameObject go);
//声明一个事件
public event VoidCall clickCall;
// Use this for initialization
void Awake () {
btnArr = GetComponentsInChildren<UIButton>();
for (int index = 0; index < btnArr.Length; index++)
{
UIEventListener.Get(btnArr[index].gameObject).onClick = Click;
}
}
void Click(GameObject go)
{
if (clickCall == null) return;
clickCall(go);//事件分发
}
}
using UnityEngine;
using System.Collections;
public class CtrEnergy : MonoBehaviour {
public UIButton[] btnArr;
public event CtrMoney.VoidCall clickCall;
// public event UIEventListener.VoidDelegate clickEvent;
// Use this for initialization
void Awake()
{
btnArr = GetComponentsInChildren<UIButton>();
for (int index = 0; index < btnArr.Length; index++)
{
UIEventListener.Get(btnArr[index].gameObject).onClick = Click;
}
}
void Click(GameObject go)
{
if (clickCall != null)
clickCall(go);
}
}
上篇:上一篇:C#简单的xml操作
下篇:下一篇:Unity3d中Material与ShareMaterial引用的区别