目录
1.Action
2.Func,Predicate
3.UnityAction,UnityEvent ,Delegate(常用)
1.Actionusing System;
using System.Linq;
using UnityEngine;
public class ActionTest : MonoBehaviour
{
Action action;
Action action1;
Action action2;
Action 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();
public delegate TResult Func(T arg);
public delegate TResult Func(T1 arg1, T2 arg2);
public class FuncTest : MonoBehaviour
{
Func func;
Func func1;
Predicate predicate;
void Start()
{
func = XXX;
Debug.Log(func());
func1 = CallStringLength;
Debug.Log(func1("sadasdads"));
///bool Predicate的用法
///输入一个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();//无意义= HsjDel_1
public delegate void HsjDel_4(T obj);
public delegate void HsjDel_5(T1 o1,T2 o2);
public class Hsj : MonoBehaviour
{
public HsjDel_1 d1;
public HsjDel_2 d2;
public HsjDel_3 d3;
public HsjDel_4 d4;
public HsjDel_5 d5;
//UnityEvent本质上是继承自UnityEventBase的类,它的AddListener()方法能够注册UnityAction,RemoveListener能够取消注册UnityAction,还有Invoke()方法能够一次性调用所有注册了的UnityAction
public UnityEvent unityEvent1;
public UnityEvent unityEvent2;
//UnityAction本质上是delegate,且有数个泛型版本(参数最多是4个),一个UnityAction可以添加多个函数(多播委托)
public UnityAction unityAction1;//UnityAction 就是delegate的无参数的一种形式 =同与HsjDel_1
public UnityAction unityAction2;//UnityAction就是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 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);
}
}