美文网首页
(unity和C#)Event

(unity和C#)Event

作者: 叫我颜先生 | 来源:发表于2018-09-18 18:41 被阅读0次

    简介

    实例

    • 简单调用

      using UnityEngine;
      
      public class DelegateExample : MonoBehaviour {
          //Step1. 定义类型----为Delegate定义一种函数原型
          public delegate void MyDelegate(int args);
          //Step2. 声明变量----声明一个Delegate变量
          public MyDelegate myDelegate;
      
          private void Start()
          {
              //Step3. 变量赋值----引用Delegate变量实现相应的函数
              myDelegate = PrintNum;
              myDelegate(5);
          }
      
          public void PrintNum(int num)
          {
              print("Print number: " + num);
          }
      }
      
    • C# Event

      把Event想象成一个视频作者,并且他还具有一大堆热情的粉丝,每天都在等待新视频的发布。为了在第一时间收看到新发布的视频,粉丝们大多会选择订阅视频作者,这样作者更新时你就会收到一条即时消息。event只负责告诉每个函数什么时候被调用,这些函数到底干了什么,event并不关心

      //Idol.cs 广播消息 定义的event相当于源头,只要调用了该事件,都会引起注册该事件的人调用相应的注册方法
      public class Idol : MonoBehaviour {
          public delegate void IdolBehaviour(string behaviour);
          public static event IdolBehaviour IdolDoSomethingHandler;
      
          private void Start()
          {
              //Idol 决定搞事了, 如果他还有粉丝的话, 就必须全部都通知到
              if (IdolDoSomethingHandler != null)
              {
                  IdolDoSomethingHandler("Idol give up writing.");
              }
          }
      }
      
      //SubscriberA.cs  订阅消息 很早注册订阅了,当event发送了,那么就会调用对应方法
      public class SubscriberA : MonoBehaviour {
          private void OnEnable()
          {
              //注册事件 相当于偶像突然有什么事件了,就会立即知道
              Idol.IdolDoSomethingHandler += LikeIdol;
          }
      
          //取消注册 
          private void OnDisable()
          {
              Idol.IdolDoSomethingHandler -= LikeIdol;
          }
      
          /// 粉丝A是一个脑残粉
          public void LikeIdol(string idolAction)
          {
              print(idolAction + " I will support you forever!");
          }
      }
      
    • Unity Event

      主要是可以拖拽很方便,用Serializable序列化IdolEvent,这样可以在Editor中显示

      //Idol.cs 需要的event直接继承自UnityEvent
      using UnityEngine;
      using UnityEngine.Events;
      
      //使用Serializable序列化IdolEvent, 否则无法在Editor中显示
      [System.Serializable]
      public class IdolEvent : UnityEvent<string> {
      
      }
      
      public class Idol : MonoBehaviour {
          public IdolEvent idolEvent;
          private void Start()
          {
              //Idol 广播消息
              if (idolEvent == null)
              {
                  idolEvent = new IdolEvent();
              }
              idolEvent.Invoke("Idol give up writing.");
          }
      }
      
      //SubscriberA.cs  如果通过拖拽,只需要写个方法即可
      //不通过拖拽 也可以注册 Addlistener
      using UnityEngine;
      public class SubscriberB : MonoBehaviour {
          public Idol myIdol;
          private void OnEnable()
          {
              //注册事件
              myIdol.idolEvent.AddListener(HateIdol);
          }
      
          private void OnDisable()
          {
              myIdol.idolEvent.RemoveListener(HateIdol);
          }
      
          public void HateIdol(string idolAction)
          {
              print(idolAction + " I will hate you forever!");
          }
      }
      
    • C# Action+Unity Action

      其实是封装好的委托

      //Data.cs 定义委托,值改变给委托
      using UnityEngine;
      using System.Collections;
      
      public class Data {
        public System.Action<int, int> onDataChange;
          public UnityAction<int, int> onDataChange1;
      
          public int blood { get; set; }
          public int blue { get; set; }
      
          public Data()
          {
      
          }
      
          public void SetData(int blood, int blue)
          {
              this.blood = blood;
              this.blue = blue;
              onDataChange(this.blood, this.blue);
              onDataChange1(this.blood, this.blue);
          }
      }
      
      //Test.cs 可以UI显示  
      public class Test : MonoBehaviour
      {
          private Data _Data;
          private void Start()
          {
              _Data = new Data();
              _Data.onDataChange += ChangeView;
              _Data.onDataChange1 += ChangeView;
          }
      
          private void ChangeView(int a,int b)
          {
              Debug.LogError(a);
          }
      
          void Update()
          {
              _Data.SetData(1, 2);
          }
      }
      
    • Unity Event+Action

      • action是委托,可以注册很多方法
      • event是事件,能够注册方法,也能action
      • event的Invoke()方法可以一次性调用所有UnityAction
      //不带参数UnityEvent不是抽象类,可以直接实例化
      using UnityEngine;
      using System.Collections;
      using UnityEngine.Events;
       
      public class UnityActionAndEvent : MonoBehaviour
      {
          public UnityAction action;
          public UnityEvent myEvent = new UnityEvent();
       
          void Start()
          {
              action = new UnityAction(MyFunction);
              action += MyFunction2;
              myEvent.AddListener(action);
          }
       
          void Update()
          {
              if (Input.GetKeyDown(KeyCode.P))
              {
                  myEvent.Invoke();
              }
          }
       
          public void MyFunction()
          {
              print("Hello: ");
          }
       
          public void MyFunction2()
          {
              print("Hello2: ");
          }
      }
      
      //带参数UnityEvent是抽象类,需要自己声明一个类
      public class MyEvent:UnityEvent<int>{}
      
      public class UnityActionWithParameter : MonoBehaviour {
          public MyEvent myEvent = new MyEvent();
          public UnityAction<int> action;
       
          void Start () {
              action= new UnityAction<int>(MyFunction);
              action += MyFunction2;
              myEvent.AddListener(action);
          }  
       
          void Update () {
              if(Input.GetKeyDown(KeyCode.A))
              {
                  myEvent.Invoke(10);
              }
          }
       
          public void MyFunction(int i)
          {
              print (i);
          }
          public void MyFunction2(int i)
          {
              print(i*2);
          }
      }
      
    • 其他事件(button.onClick等,通过Addlistener添加Action,Action可以注册多个方法)

      using UnityEngine;
      using UnityEngine.UI;
      using UnityEngine.Events;
      
      public class UnityActionExample : MonoBehaviour
      {
          Button m_AddButton;
          Renderer m_Renderer;
      
          private UnityAction m_MyFirstAction;
          float m_MyNumber;
      
          void Start()
          {
              m_AddButton = GetComponent<Button>();
              m_Renderer = GetComponent<Renderer>();
      
              m_MyFirstAction += MyFunction;
              m_MyFirstAction += MySecondFunction;
      
              m_AddButton.onClick.AddListener(m_MyFirstAction);
          }
      
          void MyFunction()
          {
              m_MyNumber++;
              Debug.Log("First Added : " + m_MyNumber);
          }
      
          void MySecondFunction()
          {
              m_Renderer.material.color = Color.blue;
              Debug.Log("Second Added");
          }
      }
      

    参考

    https://blog.csdn.net/qq_28849871/article/details/78366236

    https://docs.unity3d.com/ScriptReference/Events.UnityAction.html

    相关文章

      网友评论

          本文标题:(unity和C#)Event

          本文链接:https://www.haomeiwen.com/subject/wajmnftx.html