Dalegate委托执行,调用,对象保持一致

***********************************************挂在场景中*****************************************

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System;


public class Test : MonoBehaviour {


    private  TestDelegate delagete;
    private void Update()
    {
        
        if (Input.GetMouseButtonDown(1))
        {
            //执行方法
            Mathod(1,(seq)=> {
                Debug.Log(seq);
                LogBug(seq);
            });
        }
        if (delagete !=null )
        {
            //委托对象内开启走帧检测
            delagete.Update(Time.deltaTime);
        }
    }
    /// <summary>
    /// 定义一个执行方法
    /// </summary>
    /// <param name="time"></param>
    /// <param name="obj"></param>
    /// <returns></returns>
    TestDelegate Mathod(int time,TestDelegate.Debugint obj)
    {
         delagete= new TestDelegate(time,obj);
         delagete.Runing();
         return delagete;
    }
    /// <summary>
    /// 回调执行方法
    /// </summary>
    /// <param name="x"></param>
    void LogBug(float x)
    {
        Debug.Log("日志打印输出参数值:"+x);
    }
   
}

************************************************************************************************

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class TestDelegate 
{
    
    //定义一个委托,用来指向我们某个函数。
    //param参数是名字
    public delegate void Debugint(float param);
    //定义一个委托的变量事件
    private  Debugint handlerDebugint;
    //获取赋值的时间
    private float timer;
    //可执行开关
    private bool isBegin = false;
    /// <summary>
    /// 构造体赋值
    /// </summary>
    /// <param name="timer"></param>
    /// <param name="intstr"></param>
    public TestDelegate(int timer,Debugint intstr)
    {
        this.timer = timer;
        handlerDebugint = intstr;
    }
    /// <summary>
    /// 运用参数,调用可执行走帧
    /// </summary>
    /// <param name="time"></param>
    public void Update(float time)
    {
        if (isBegin)
        {
            timer += time;
            if (timer>2)
            {
                //调用委托,回调执行
                handlerDebugint(timer);
                isBegin = false;
            }
        }
    }
    /// <summary>
    /// 开启开关
    /// </summary>
    public void Runing()
    {
        isBegin = true;
    }
    
}

posted @ 2018-07-04 18:20  低小调  阅读(73)  评论(0编辑  收藏  举报