[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
委托相当于把某一个方法当成参数,当执行委托的时候就相当于执行了方法,所以这个方法必须和委托具有相同的参数类型.
委托的简单实现
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); } }
输出结果