每日一学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));
    }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值