一、什么是委托
*委托顾名思义就是委托别人做一些事情,一般在做项目时经常遇到需要把函数作为参数去传递,或者去写一些回调方法,这个时候就可以用Delegate来解决
二、案例
三、脚本说明
首先是全局的一个控制脚本,挂在了Canvas下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
注册委托时的方法需要注意的点:
1.返回值类型要一致,下面我的返回值类型都是void,所以我注册的方法也必须是void返回值类型
2.参数的个数以及参数类型要保持一致
*/
//控制所有物体旋转的委托
public delegate void CallRotate();
//控制所有物体缩放的委托
public delegate void CallScale();
//控制所有物体停止旋转的委托
public delegate void CallStopRotate();
//控制单独物体行为的委托
public delegate void HowName(string name);
public class DelegateController : MonoBehaviour
{
public CallRotate cllRotate;
public CallScale callScale;
public CallStopRotate callStopRotate;
public HowName CallhowName;
void Start()
{
/*
以下是ui按钮的点击事件,在组件中对应绑定
*/
}
/// <summary>
/// 旋转的点击事件
/// </summary>
public void OnRotateClick()
{
cllRotate();
}
/// <summary>
/// 放大的点击事件
/// </summary>
public void OnScaleClick()
{
callScale();
}
/// <summary>
/// 停止旋转的点击事件
/// </summary>
public void OnStopRotateClick()
{
callStopRotate();
}
/// <summary>
/// Sphere的点击事件
/// </summary>
public void OnSphereClick()
{
CallhowName("Sphere");
}
/// <summary>
/// Cube的点击事件
/// </summary>
public void OnCubeClick()
{
CallhowName("Cube");
}
/// <summary>
/// Capsule的点击事件
/// </summary>
public void OnCapsuleClick()
{
CallhowName("Capsule");
}
}
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System.Collections;
/*
cube本身的脚本
*/
public class CubeMyClass : MonoBehaviour
{
public DelegateController delMainScript;
private bool isRotate = false;
Vector3 startPos;
Vector3 startScale;
Quaternion startRotation;
void Awake()
{
startPos = transform.position;
startScale = transform.localScale;
startRotation = transform.rotation;
}
void Start()
{
/*
对委托的方法进行注册,在点击对应按钮时就会调用你所有注册的方法
*/
delMainScript.cllRotate += MySelfRotate;
delMainScript.callStopRotate += MySelfStopRotate;
delMainScript.callScale += MySelfScale;
delMainScript.CallhowName += CubeTest;
}
void Update()
{
if (isRotate)
{
transform.Rotate(new Vector3(0, 4, 0), Space.Self);
}
}
void MySelfRotate()
{
isRotate = true;
}
void MySelfStopRotate()
{
isRotate = false;
transform.DOMove(startPos, 1);
transform.DOScale(startScale, 1);
transform.DORotateQuaternion(startRotation, 1);
}
void MySelfScale()
{
StopCoroutine(Doscale());
StartCoroutine(Doscale());
}
IEnumerator Doscale()
{
transform.DOScale(2, 1);
yield return new WaitForSeconds(2);
transform.DOScale(1, 1);
}
void CubeTest(string name)
{
if (name == transform.name)
{
transform.DOMove(Vector3.zero, 1);
transform.DOScale(2, 1);
isRotate = true;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
/*
圆本身的脚本
*/
public class SphereMyClass : MonoBehaviour
{
public DelegateController delMainScript;
private bool isRotate = false;
Vector3 startPos;
Vector3 startScale;
Quaternion startRotation;
private void Awake()
{
startPos = transform.position;
startScale = transform.localScale;
startRotation = transform.rotation;
}
void Start()
{
delMainScript.cllRotate += MySelfRotate;
delMainScript.callStopRotate += MySelfStopRotate;
delMainScript.callScale += MySelfScale;
delMainScript.CallhowName += SphereTest;
}
void Update()
{
if (isRotate)
{
transform.Rotate(new Vector3(0, 10, 0), Space.Self);
}
}
void MySelfRotate()
{
isRotate = true;
}
void MySelfStopRotate()
{
isRotate = false;
transform.DOMove(startPos, 1);
transform.DOScale(startScale, 1);
transform.DORotateQuaternion(startRotation, 1);
}
void MySelfScale()
{
StopCoroutine(Doscale());
StartCoroutine(Doscale());
}
IEnumerator Doscale()
{
transform.DOScale(2, 1);
yield return new WaitForSeconds(2);
transform.DOScale(1, 1);
}
void SphereTest(string name)
{
if (name == transform.name)
{
transform.DOMove(Vector3.zero, 1);
transform.DOScale(2, 1);
isRotate = true;
}
}
}
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
胶囊本身的脚本
*/
public class CapsuleMyClass : MonoBehaviour
{
public DelegateController delMainScript;
private bool isRotate = false;
Vector3 startPos;
Vector3 startScale;
Quaternion startRotation;
void Awake()
{
startPos = transform.position;
startScale = transform.localScale;
startRotation = transform.rotation;
}
void Start()
{
delMainScript.cllRotate += MySelfRotate;
delMainScript.callStopRotate += MySelfStopRotate;
delMainScript.callScale += MySelfScale;
delMainScript.CallhowName += CapsuleTest;
}
void Update()
{
if (isRotate)
{
transform.Rotate(new Vector3(0, 1, 0), Space.Self);
}
}
void MySelfRotate()
{
isRotate = true;
}
void MySelfStopRotate()
{
isRotate = false;
transform.DOMove(startPos, 1);
transform.DOScale(startScale, 1);
transform.DORotateQuaternion(startRotation, 1);
}
void MySelfScale()
{
StopCoroutine(Doscale());
StartCoroutine(Doscale());
}
IEnumerator Doscale()
{
transform.DOScale(2, 1);
yield return new WaitForSeconds(2);
transform.DOScale(1, 1);
}
void CapsuleTest(string name)
{
if (name == transform.name)
{
transform.DOMove(Vector3.zero, 1);
transform.DOScale(2, 1);
isRotate = true;
}
}
}
总结
在我们点击对应的按钮时把声明的委托放进事件中,至于想执行什么方法就去用委托注册就行,很简单易懂吧!