当前位置:网站首页>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);
}
}
输出结果

边栏推荐
- CAS atomic type
- Unity行人随机行走不碰撞
- 自动化运维之Ansible安装部署
- Unity pedestrians walk randomly without collision
- Where can Huatai Securities open an account? Is it safe to use a mobile phone
- 2.4. properties of special profile
- 北京一卡通以35288.8529万元挂牌出让68.45%股权,溢价率为84%
- Mmdrawercontroller first loading sidebar height problem
- R语言检验样本比例:使用prop.test函数执行单样本比例检验计算总体中成功样本比例p值的置信区间
- Data Lake series articles
猜你喜欢

Error importing header file to PCH

After five years of contact with nearly 100 bosses, as a headhunter, I found that the secret of promotion was only four words

Remove the treasure box app with the green logo that cannot be deleted from iPhone

Network security - error injection

RHCE first operation

天然气潮流计算matlab程序

rhce第一次作业

Nessus安全测试工具使用教程

Sringboot plugin framework implements pluggable plug-in services

Sringboot-plugin-framework 实现可插拔插件服务
随机推荐
On the number of solutions of indefinite equations
CSP2021 T3 回文
Statistical table of competition time and host school information of 2022 national vocational college skills competition (the second batch)
SQL subquery
How to build and run WordPress on raspberry pie
R language uses the tablestack function of epidisplay package to make statistical summary tables (descriptive statistics based on the grouping of target variables, hypothesis testing, etc.), set the b
R语言使用sort函数排序向量数据实战、返回实际排序后的数据(默认升序)
2022.7.22 模拟赛
自动化运维之Ansible安装部署
sql server语法—创建数据库
NOIP2021 T2 数列
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
Nmap security testing tool tutorial
Rhcsa sixth note
Noip2021 T2 series
Cocoapod installation problems
Network security - file upload competitive conditions bypass
北京一卡通以35288.8529万元挂牌出让68.45%股权,溢价率为84%
Mini examination - examination system
Network security - file upload content check bypass