unity3DAction和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;
-
}
-
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhghfchc
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13