【Unity开发小技巧】UnityEvent,UnityAction, Delegate,Action,Func,Predicate的区别及用法

目录

1.Action

2.Func,Predicate

3.UnityAction,UnityEvent ,Delegate(常用)


1.Action

using System;
using System.Linq;
using UnityEngine;

public class ActionTest : MonoBehaviour
{
    Action action;
    Action<string> action1;
    Action<string, int> action2;
    Action<string[]> action3;
    void Start()
    {
        action += test1;//无参数
        action();

        action1 += test2;//一个参数
        action1("Test2");

        action2 += test3;//两个参数
        action2("Test3", 99);

        action3 += test4;//集合参数
        action3(new string[] { "charlies", "nancy", "alex", "jimmy", "selina" });
    }

    void test1()
    {
        Debug.Log("test1");
    }
    void test2(string str)
    {
        Debug.Log(str);
    }
    void test3(string str, int num)
    {
        Debug.Log(string.Format("{0}  {1}", str, num));
    }

    void test4(string[] x)
    {
        var result = from o in x where o.Contains("s") select o;
        foreach (string s in result.ToList())
        {
            Debug.Log(s);
        }
    }
}

2.Func,Predicate

Func可以传入多个参数,默认最后一个为返回值
Predicate只能接受一个传入参数,返回值为bool类型
#region 模块信息
// **********************************************************************
// Copyright (C) 2018 Blazors
// Please contact me if you have any questions
// File Name:             GameDefine
// Author:                romantic123fly
// WeChat||QQ:            at853394528 || 853394528 
// **********************************************************************
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

//有返回值的委托
public delegate TResult Func<TResult>();
public delegate TResult Func<T, TResult>(T arg);
public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);

public class FuncTest : MonoBehaviour
{
    Func<int> func;
    Func<string, int> func1;
    Predicate<string[]> predicate;
    void Start()
    {
        func = XXX;
        Debug.Log(func());
        func1 = CallStringLength;
        Debug.Log(func1("sadasdads"));

        ///bool Predicate<T>的用法
        ///输入一个T类型的参数,返回值为bool类型
        predicate = func2;


        string[] _value = { "charlies", "nancy", "alex", "jimmy", "selina" };
        Debug.Log(predicate(_value));
    }

    private bool func2(string[] obj)
    {
        var result = from p in obj
                     where p.Contains("s")
                     select p;
        if (result.ToList().Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    int XXX()
    {
        return 10;
    }
    int CallStringLength(string str)
    {
        return str.Length;
    }
}

3.UnityAction,UnityEvent ,Delegate(常用)

using UnityEngine;
using UnityEngine.Events;

public delegate void HsjDel_1();
public delegate void HsjDel_2(string name);
public delegate void HsjDel_3<T>();//无意义= HsjDel_1
public delegate void HsjDel_4<T>(T obj);
public delegate void HsjDel_5<T1,T2>(T1 o1,T2 o2);

public class Hsj : MonoBehaviour
{
    public HsjDel_1 d1;
    public HsjDel_2 d2;
    public HsjDel_3<UserData> d3;
    public HsjDel_4<UserData> d4;
    public HsjDel_5<string ,int> d5;
    //UnityEvent本质上是继承自UnityEventBase的类,它的AddListener()方法能够注册UnityAction,RemoveListener能够取消注册UnityAction,还有Invoke()方法能够一次性调用所有注册了的UnityAction
    public UnityEvent unityEvent1;
    public UnityEvent<string > unityEvent2;
    //UnityAction本质上是delegate,且有数个泛型版本(参数最多是4个),一个UnityAction可以添加多个函数(多播委托)
    public UnityAction unityAction1;//UnityAction 就是delegate的无参数的一种形式 =同与HsjDel_1
    public UnityAction<int> unityAction2;//UnityAction<T>就是delegate一个无参数的一种形式 =同于HsjDel_2
    // Start is called before the first frame update
    void Start()
    {
        d1?.Invoke();
        d2?.Invoke("HSJ");
        d3?.Invoke();
        d4?.Invoke(new UserData("aaa",5));
        d5?.Invoke("aaa", 1);

        unityEvent1?.Invoke();
        unityEvent2?.Invoke(name);

        unityAction1?.Invoke();
        unityAction2?.Invoke(5);
    }

    public void F1( int a, UnityAction<int> action)
    {
        Debug.Log(a);
        action?.Invoke(2);
    }
}
public class UserData
{
    public string name;
    public int age;

    public UserData(string name, int age)
    {
        this.name = name;
        this.age = age;
    }
}
using UnityEngine;

public class Obj : MonoBehaviour
{
    public Hsj hsj;
    // Start is called before the first frame update
    void Awake()
    {
        hsj.d1 += Del_1;
        hsj.d2 += Del_2;
        hsj.d3 += Del_3;
        hsj.d4 += Del_4;
        hsj.d5 += Del_5;
        hsj.F1(2,(a)=> {
            Debug.Log(a);
        });
        hsj.unityEvent1.AddListener(UnityEvent);
        hsj. unityEvent2.AddListener(UnityEvent1);
      
        hsj.unityAction1+= Action1;
        hsj.unityAction2+= Action2;
    }

    private void Action2(int arg0)
    {
        Debug.Log("Action2:"+arg0);
    }

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

    private void UnityEvent1(string arg0)
    {
        Debug.Log("UnityEvent1:" + arg0);
    }

    private void UnityEvent()
    {
        Debug.Log("UnityEvent");
    }
    public void Del_1()
    {
        Debug.Log("Del_1");
    }
    public void Del_2(string n)
    {
        Debug.Log("Del_2:"+n);
    }
    private void Del_3()
    {
        Debug.Log("Del_3");
    }
    public void Del_4(UserData data)
    {
        Debug.Log("Del_4:"+data.name);
    }
    public void Del_5(string s,int a)
    {
        Debug.Log("Del_5:"+s);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幻世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值