美文网首页
unity 委托事件

unity 委托事件

作者: GB_speak | 来源:发表于2018-10-16 11:06 被阅读20次

    定义委托及事件

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    
    public delegate void KaiShiEventHandler(string name);//定义委托
    
    public class AppMain : MonoBehaviour {
    
       
        public static event KaiShiEventHandler kaishi;//事件
      
        public Button but;
    
        public int i = 0;
        // Use this for initialization
        void Start () {
    
            but.onClick.AddListener(OnClick);
    
        }
    
        public void OnClick()
        {
            
            kaishi("ddddddddd~");
           
        }
        
        // Update is called once per frame
        void Update () {
            
        }
    }
    

    实现事件监听+=
    (事件取消监听为-=)

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CubeApp : MonoBehaviour {
    
        // Use this for initialization
        void Start () {
            
            AppMain.kaishi += kaishi;
    
        }
    
        public void kaishi(string name)
        {
            Debug.Log("开始~~~~"+name);
        }
       
        
        // Update is called once per frame
        void Update () {
            
        }
    }
    
    image.png

    相关文章

      网友评论

          本文标题:unity 委托事件

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