Unity 委托Delegate的理解以及用法案例

一、什么是委托

*委托顾名思义就是委托别人做一些事情,一般在做项目时经常遇到需要把函数作为参数去传递,或者去写一些回调方法,这个时候就可以用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;
        }
    }
}

总结

在我们点击对应的按钮时把声明的委托放进事件中,至于想执行什么方法就去用委托注册就行,很简单易懂吧!

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值