unity3D中Action与Func的简单运用

Action与Func是.NEt中的内置委托类型,可以直接用来定义委托而无需先声明,非常的方便

Action的简单运用

//使用Action,Func等.Net自带的内置委托Action和Func
//须引用名称为System的命名空间
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*//无参
public delegate void Action();
//1个参数
public delegate void Action<in T>(T obj);
//2个参数
public delegate void Action<in T1, in T2>(T1 Obj1, T2 obj2);*/


public class ActionTest : MonoBehaviour
{
    //无参
    Action SayGoodBayAction;
    //一个参数
    Action<string> SayHelloAction;
    //两个参数
    Action<string, string> strTestAction;
    // Start is called before the first frame update
    void Start()
    {
        SayHelloClass mySayHello = new SayHelloClass();
        mySayHello.OnHello("Tom1");

        SayHelloAction = myTest;
        SayHelloAction += myTest;
        SayHelloAction += mySayHello.OnHello;


        SayGoodBayAction = myTest2;

        strTestAction = myTest3;

        SayHelloAction.Invoke("Tom");
        SayGoodBayAction.Invoke();

        strTestAction.Invoke("汤姆" , "杰瑞");

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private static void myTest( string name)
    {
        Debug.Log($"This is a Test,{name}");
    }

    private static void myTest2()
    {
        Debug.Log("This is myTest2");
    }

    private static void myTest3(string str1, string str2)
    {
        Debug.Log($"{str1} + {str2}");
    }
}


public class SayHelloClass : ISayHello
{
    public void OnHello(string message)
    {
        Debug.Log($"Hello,my name is {message}");
    }
}

public interface ISayHello
{
    void OnHello(string message);
}

Func的简单运用

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

public class Func : MonoBehaviour
{
    Func<int,int,int> myFunc = null;
    // Start is called before the first frame update
    void Start()
    {

        myFunc = Add;

        if(myFunc != null)
        {
            int result = myFunc.Invoke(2, 3);
            Debug.Log(result);
        }

        myFunc -= Add;

        //检查是否有误
        try
        {
            myFunc.Invoke(5, 2);
        }
        catch
        {
            Debug.Log("委托可能为空");
        }
        finally
        {
             //重新指向Add函数
            myFunc = Add;
            Debug.Log($"result = {myFunc.Invoke(10,8)}");

            //移出Add
            myFunc -= Add;

            //Lambda表达式
            //指向匿名函数
            myFunc = (x, y) =>
            {
                return x + y;
            };
            Debug.Log($"result = {myFunc.Invoke(9, 6)}");
        }
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    
    //加法
    int Add(int x, int y)
    {
        int z = x + y;
        return z;
    }
    //减法
    int Mul(int x, int y)
    {
        int z = x - y;
        return z;
    }
    
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#ActionFunc是两种常用的委托类型。Action是一个没有返回值的委托,而Func是一个有返回值的委托。\[1\] 在代码示例,使用了FuncAction来定义委托并执行相应的方法。Func可以定义带有不同参数和返回值类型的委托,而Action只能定义带有参数但没有返回值的委托。\[1\] 在示例,MyFun1是一个无参的Func委托,返回一个string值。MyFun2是一个带有一个string参数的Func委托,返回一个string值。MyFun3是一个带有两个string参数的Func委托,返回一个bool值。\[1\] 另外,示例还展示了使用Action来定义委托的写法。在Start方法,使用Action定义了一个带有一个string参数的委托act,并执行了Init方法。\[2\] 此外,还可以使用Lambda表达式来创建委托。Lambda表达式可以简化委托的定义和使用。在示例,使用Lambda表达式创建了一个带有一个string参数的Action委托。\[3\] 总结来说,ActionFuncC#常用的委托类型,用于定义不同参数和返回值类型的委托。Action用于定义没有返回值的委托,而Func用于定义有返回值的委托。可以使用Lambda表达式来简化委托的定义和使用。 #### 引用[.reference_title] - *1* *2* [c#ActionFunc简单用法)](https://blog.csdn.net/qq_39984000/article/details/115245134)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [C#语法:C# 简述Actionfunction内置委托用法、Lambda 表达式创建委托最为方便](https://blog.csdn.net/qq_37271216/article/details/102801792)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值