当前位置:网站首页>Unity 委托 (Delegate) 的简单理解以及实现
Unity 委托 (Delegate) 的简单理解以及实现
2022-07-24 14:08:00 【HoFunGames】
委托相当于把某一个方法当成参数,当执行委托的时候就相当于执行了方法,所以这个方法必须和委托具有相同的参数类型.
委托的简单实现
using UnityEngine;
//委托(代理) 是存有对某个方法的引用的一种引用类型变量。
//委托语法:delegate <return type> <delegate-name> <parameter list>
public class DelegateTest : MonoBehaviour
{
//声明一个没有返回值的委托,委托可以写在类中也可以写在类名的外面
public delegate void PrintString(string value);
void Start()
{
DebugString("普通的方法");
//声明一个委托并且将和委托具有相同参数类型的方法DebugString传入
PrintString p1 = new PrintString(DebugString);
//此时这个委托具有和 DebugString方法相同的功能
p1("委托测试");
}
//普通的方法,输出你传入的字符串
public void DebugString(string s)
{
Debug.Log(s);
}
}输出的结果

多播:委托对象可使用 "+" 运算符进行合并。一个合并委托调用它所合并的两个委托。只有相同类型的委托可被合并。"-" 运算符可用于从合并的委托中移除组件委托。
简单来说就是委托执行的时候可以执行多个方法
using UnityEngine;
//多播(组播),委托对象可使用 "+" 运算符进行合并。一个合并委托调用它所合并的两个委托。只有相同类型的委托可被合并。"-" 运算符可用于从合并的委托中移除组件委托。
public class DelegateTest : MonoBehaviour
{
//声明一个没有返回值的委托,委托可以写在类中也可以写在类名的外面
public delegate void PrintString(string value);
void Start()
{
//声明一个委托并且将和委托具有相同参数类型的方法DebugString传入
PrintString p ;
PrintString p1 = new PrintString(DebugStringOne);
PrintString p2 = new PrintString(DebugStringTwo);
p = p1;
p += p2;
//调用多播
p("委托测试");
}
//普通的方法,输出你传入的字符串
public void DebugStringOne(string s)
{
Debug.Log("DebugStringOne:" + s);
}
public void DebugStringTwo(string s)
{
Debug.Log("DebugStringTwo:" + s);
}
}
输出结果

边栏推荐
- The KAP function of epidisplay package in R language calculates the value of kappa statistics (total consistency, expected consistency), analyzes the consistency of the results of multiple scoring obj
- Ansible installation and deployment of automated operation and maintenance
- 2022.7.22 simulation match
- Lazy loading of pictures
- 【无标题】
- Add an element to the object array with unshift
- Soft link, hard link
- C language -- program environment and preprocessing
- R language uses the sum function of epidisplay package to calculate the descriptive statistical summary information of the specified variables in dataframe under different grouping variables, visualiz
- Detailed analysis of common command modules of ansible service
猜你喜欢

Source code analysis of ArrayList

Network security - file upload competitive conditions bypass

Network security -- Service Vulnerability scanning and utilization

Network security - filtering bypass injection

Concurrent programming ----------- set

How to quickly wrap lines in Excel table

【无标题】rhcsa第一次作业

正则表达和绕过案例

对话框管理器第二章:创建框架窗口

OWASP zap security testing tool tutorial (Advanced)
随机推荐
Flinktable & SQL (VI)
sql server语法—创建数据库
C# 多线程锁整理记录
Introduction to the separation of front and rear platforms of predecessors
Wechat applet todo case
Mmdrawercontroller first loading sidebar height problem
Flink容错机制(五)
Ansible服务常用命令模块详细解析
软链接、硬链接
Don't lose heart. The famous research on the explosive influence of Yolo and PageRank has been rejected by the CS summit
Network security - war backdoor deployment
正则表达和绕过案例
MySQL community download address
Nessus安全测试工具使用教程
SQL server startup and shutdown job script
字符串——28. 实现 strStr()
栈与队列——232. 用栈实现队列
threw exception [Circular view path [index]: would dispatch back to the current handler URL [/index]
C language -- program environment and preprocessing
R language test sample proportion: use the prop.test function to perform a single sample proportion test to calculate the confidence interval of the p value of the successful sample proportion in the