using UnityEngine;
using System.Collections;
public class DJH_Delegate : MonoBehaviour
{
public delegate void Delegate1();
public Delegate1 OutTest1;
public delegate void Delegate2(int index);
public Delegate2 OutTest2;
int index = 12345;
void Start()
{
OutTest1 = DebugLog1;
OutTest2 = DebugLog2;
}
void OnGUI()
{
//开始按钮
if (GUI.Button(new Rect(0, 10, 100, 30), "Button1"))
{
if (OutTest1 != null)//无参数
{
DebugLog1();
}
}
if (GUI.Button(new Rect(0, 60, 100, 30), "Button2"))
{
if (OutTest2 != null)//有参数
{
DebugLog2(index);
}
}
}
void DebugLog1()
{
Debug.Log("Button1");
}
void DebugLog2(int index)
{
Debug.Log("Button2参数:" + index);
}
}【Unity3D自学记录】Unity3D代理委托模式
Unity中委托的应用实例
最新推荐文章于 2025-08-07 11:26:07 发布
本文通过Unity游戏开发环境中的一个具体实例介绍了如何使用C#中的委托。实例展示了两种类型的委托:一种不带参数,另一种带有整型参数。通过GUI按钮触发委托调用,并在控制台打印出相应的消息。

3736

被折叠的 条评论
为什么被折叠?



