每日一学5——Unity C# 委托

学习来源:https://unity.cn/projects/shi-yao-shi-wei-tuo-c-ji-chu-zhi-shi-jiang-jie-lambdabiao-da-shi-action-funchan-shu

Code Monkey的视频质量的确非常高,近期以刷他的视频为主(电脑上不知道为啥视频老是打不开,有兴趣的小伙伴可以自己去Unity Connect软件上自己刷,速度还不错)。一直认为的委托就是把函数打包起来,一次性调用多个函数。事实上本质好像就是这样。但是一直以来也想不起来用委托,只会呆呆的调用多次完事了,也可能是大项目的经历还不够。现在来以这篇视频好好学习一下委托吧,本篇整理一下各种委托的写法~

  • 普通委托
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    //前面需要声明公有还是私有,类似函数
    //增加关键词delegate,设定返回值类型,设置参数,此处以最简写法测试
    //参数与返回值类型需与委托函数相同
    public delegate void testDelegate();

    private testDelegate TestDelegate;

    void Start()
    {
        //TestDelegate = new testDelegate(TestFunction);
        //TestDelegate += TestFunction;
        TestDelegate = TestFunction;//赋值,这句话与上两句话是等价的,可互相替代,一般建议使用第二种
        TestDelegate += TestFunction2;
        TestDelegate();//直接调用
    }

    void TestFunction()
    {
        Debug.Log("TestFunction");
    }

    void TestFunction2()
    {
        Debug.Log("TestFunction2");
    }
}
  • 匿名方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    public delegate void testDelegate();

    private testDelegate TestDelegate;

    void Start()
    {
        //委托调用匿名方法
        //缺点是无法移除委托中的匿名方法
        TestDelegate=delegate () {
            Debug.Log("Test");
        };

        //此写法省略()
        //TestDelegate = delegate {
        //    Debug.Log("Test");
        //};
        TestDelegate();
    }
}
  • 无参数的Lambda表达式
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    public delegate void testDelegate();

    private testDelegate TestDelegate;

    void Start()
    {
        //Lambda表达式+匿名方法
        TestDelegate += () => { Debug.Log("Test"); };
        //Lambda表达式+匿名方法调用普通方法
        TestDelegate += () =>{
             TestFunction();
         };
        TestDelegate();
    }

    void TestFunction() {
        Debug.Log("TestFunction");
    }
}
  • 有参数和返回值的Lambda表达式
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    public delegate bool testDelegate(int i);

    private testDelegate TestDelegate;

    void Start()
    {
        //Lambda表达式+匿名方法
        TestDelegate += (int i) => { return i<10; };
        //Lambda表达式+匿名方法简写
        TestDelegate += (int i) => i < 10;
        //Lambda表达式+匿名方法调用普通方法简写
        TestDelegate += (int i) => TestFunction(i);
        //因为上述实际过程是匿名函数调用了TestFunction,因此这里-=TestFunction是无效的
        TestDelegate -= TestFunction;
        //委托的返回值会以最后一个函数的返回值为结果
        Debug.Log(TestDelegate(7));//输出False
    }

    bool TestFunction(int i) {
        Debug.Log("TestFunction");
        return i < 5;
    }
}
  • 特殊委托Action
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class NewBehaviourScript : MonoBehaviour
{
    //Action是一个返回值为空,不接收参数的委托
    public Action testAction;
    //泛型Action,<>中设定传入值,返回值为空
    public Action<NewBehaviourScript> testActionT;

    void Start()
    {
        //与之前委托一样()中写参数,后面写实体
        testActionT += (NewBehaviourScript m) =>Debug.Log("testActionT");
        testActionT(this);
    }
}
  • 特殊委托Func单参数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class NewBehaviourScript : MonoBehaviour
{
    //Func与Action<T>相反,是一个设定返回值,不接收参数的委托
    public Func<int> testFunc;

    void Start()
    {
        //确保()中无参数,返回值类型与设定相同
        testFunc += () => { return 5; } ;
        Debug.Log(testFunc());
    }
}
  • 特殊委托Func多参数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class NewBehaviourScript : MonoBehaviour
{
    //如果同时需要参数与返回值,可以用Func多参数形式
    //<>中设定的最后一个参数类型即为返回值类型,之前的为参数类型
    public Func<bool,int> testFunc;

    void Start()
    {
        //()中对应设定好的bool类型
        testFunc += (bool b) => { return b ? 5 : 4; } ;
        Debug.Log(testFunc(false));
    }
}

委托的全部写法就这些了,后面再学习实际场景中的用途~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • [Unity]技术学习路线图(长期更新) 很多同学都会问到我说,代理设计模式到底会怎么用,哪些地方会用到,把自己对...
    肖浩呗阅读 9,423评论 1 14
  • C# 委托 委托是类型安全的类,它定义了返回类型和参数的类型,委托类可以包含一个或多个方法的引用。可以使用lamb...
    OctOcean阅读 5,186评论 0 4
  • 委托Delegate C#中的Delegate对应于C中的指针,但是又有所不同C中的指针既可以指向方法,又可以指向...
    沉麟阅读 3,968评论 0 0
  • 引用方法 委托是寻址方法的.NET版本。在C++中,函数指针只不过是一个指向内存位置的指针,它不是类型安全的。我们...
    余百777阅读 3,237评论 0 0
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 128,268评论 2 7

友情链接更多精彩内容