unity委托

委托是什么?举一个例子来讲就是一件本该由自己来做的事情不由自己来做而是叫别人来做

Lambda和中心事件会广泛用到委托,因此搞清楚委托概念非常重要

 

委托是一种类,他是一种引用类型的数据类型,它可以指向一个自定义类型的参数列表和自定义返回值类型的方法

委托类似于c语言中的函数指针的概念,函数指针的间接调用  函数指针及其定义和用法,C语言函数指针详解 (biancheng.net)

C语言的函数指针:int(*p)(int a, int b); 

C#中的委托:public delegate int p(int a, int b);

可以看到两者的相似程度

 

简单给出一个使用自定义委托的例子。单播委托和多播委托的使用,用一个委托一次调用三个函数的功能

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

public class Delegate1 : MonoBehaviour
{
    //如果参数列表为空 可以指向任何一个一个没有返回值、参数列表为空的方法
    //但不能指向有返回值有参数列表的方法
    public delegate void MyDelegate();
    MyDelegate myDelegate;

    private void OnEnable()
    {

        //函数指针!

        //单播委托
        myDelegate = Log1;
        //or
        //myDelegate = new MyDelegate(Log1);

        //多播委托
        myDelegate += Log2;
        //myDelegate += new MyDelegate(Log2);
        myDelegate += Log3;
        //myDelegate += new MyDelegate(Log3);

    }

    private void Log1()
    {
        Debug.Log("Log1");
    }

    private void Log2()
    {
        Debug.Log("Log1");
    }

    private void Log3()
    {
        Debug.Log("Log1");
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //调用委托
            myDelegate();
            //or
            //myDelegate.Invoke();
        }
    }
}

 

当然我们也可以不自定义,直接使用C#为我们做好的泛型委托Action 或 Func

Action:无返回值(void),但可以有参数列表

Func<>:有返回值,也有参数列表

public class Delegate2 : MonoBehaviour
{
    //C#为我们封装好的delegate
    Action action01;
    Func<string> func01;
    Func<double, double, double> func02;
}

以上内容参考自:【委托•语法篇】委托类型的声明和实例以及Action委托和Func委托(附:委托的重要性,函数指针,多播委托,委托的缺点)_哔哩哔哩_bilibili

 

Unity也提供了其泛型委托

下面是官方给出的例子

//Attach this script to a GameObject. Attach a Renderer and Button component to the same GameObject for this example.
//This script will change the Color of the GameObject as well as output messages to the Console saying which function was run by the UnityAction.

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

public class UnityActionExample : MonoBehaviour
{
    //This is the Button you attach to the GameObject in the Inspector
    Button m_AddButton;
    Renderer m_Renderer;

    private UnityAction m_MyFirstAction;
    //This is the number that the script updates
    float m_MyNumber;

    void Start()
    {
        //Fetch the Button and Renderer components from the GameObject
        m_AddButton = GetComponent<Button>();
        m_Renderer = GetComponent<Renderer>();

        //Make a Unity Action that calls your function
        m_MyFirstAction += MyFunction;
        //Make the Unity Action also call your second function
        m_MyFirstAction += MySecondFunction;
        //Register the Button to detect clicks and call your Unity Action
        m_AddButton.onClick.AddListener(m_MyFirstAction);
    }

    void MyFunction()
    {
        //Add to the number
        m_MyNumber++;
        //Display the number so far with the message
        Debug.Log("First Added : " + m_MyNumber);
    }

    void MySecondFunction()
    {
        //Change the Color of the GameObject
        m_Renderer.material.color = Color.blue;
        //Ouput the message that the second function was played
        Debug.Log("Second Added");
    }
}

 

posted @   fjnloo  阅读(514)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示