很多小伙伴知道委托 但是可能从来没有用过
今天我做一个小小的demo给大家大致介绍一下委托的使用方法
首先给大家demo
从上边我们看到我们实现的demo 其实也不难 但是我们用委托来实现可能会更简洁 操作起来更简单
实现过程
场景的搭建我就不多说了 这里边也没有用什么模型那些 就是单纯的3D物体的拼接
其中的UI我们可以修改模式 也可以使用3Dtext 但我这里用到时UI
这里我就不很详细的说了
首先我们先写出我们服务员(售货员)的脚本
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
//委托传递的是方法/函数
public delegate void CallNumber(); //使用时,必须要声明委托对象;返回值类型,要与将要注册到委托对象中的函数,返回值类型保持一致!
public delegate int GetOuntHandler(); /*也就是:委托方法和被委托方法的类型必须保持一致*/
public delegate bool SomeFood(string food);
/*以上是委托函数的声明方式*/
public class ChinarDelegate : MonoBehaviour
{
//方法是引用类型的对象
//委托将一个函数 转换成了一个函数对象
//可以将 这个函数 以 对象 的形式进行传递
public CallNumber callNumber; //定义委托对象 这里定义的是一个委托对象
public SomeFood someFood; //定义一个有参数有返回值的委托
public Text WaiterSpeak; //大屏幕内容文本
public Text BigScreen; //服务员说话内容文本
/// <summary>
/// 叫号方法 —— 绑定按钮
/// </summary>
public void OnClickCallNumber()
{
callNumber();
Content( "叫号啦!!!", "请100号顾客取餐!");
}
/// <summary>
/// 汉堡包 —— 绑定按钮
/// </summary>
public void OnClickHamburger()
{
if (!GameObject.Find("Hamburger(Clone)")) Instantiate(Resources.Load("Hamburger"));
someFood("汉堡");
Content("谁的汉堡?", "请B号顾客取餐!");
}
/// <summary>
/// 可乐 —— 绑定按钮
/// </summary>
public void OnClickCola()
{
someFood("可乐");
Content( "谁点的可乐?", "请C号顾客取餐!");
}
/// <summary>
/// 封装内容简化代码
/// </summary>
/// <param name="color"></param>
/// <param name="speak"></param>
/// <param name="bigScreen"></param>
private void Content( string speak, string bigScreen)
{
WaiterSpeak.text = speak;
BigScreen.text = bigScreen;
StopCoroutine(ClearText());
StartCoroutine(ClearText());
}
/// <summary>
/// 清理文本内容
/// </summary>
/// <returns></returns>
IEnumerator ClearText()
{
yield return new WaitForSeconds(3);
WaiterSpeak.text = "";
BigScreen.text = "";
}
}
然后对于顾客 我是用了继承的方法 避免代码的重复 提升我们开发的效率
首先给出父类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class User : MonoBehaviour
{
//此类为所有顾客的父类
public ChinarDelegate Cd; //委托对象
protected Text temporaryText; //临时文本,设置公有静态是为了 方便调用颜色属性,去改变服务员文本颜色
private Vector3 startpos;//开始位置
private Transform TalkFoodPosi;//取货位置
protected float timer;//插值的进度
protected bool ismove;//是否开始移动
public virtual void Start()
{
startpos = transform.position;
Cd = GameObject.Find("Shopper").GetComponent<ChinarDelegate>(); //获取对象
Cd.callNumber += LookScreen;
TalkFoodPosi = GameObject.Find("talkPosi").transform;
}
/// <summary>
/// 抬头看大屏幕
/// </summary>
public virtual void LookScreen()
{
//temporaryText.text = "顾客C看向大屏幕";
StopCoroutine(ClearText());
StartCoroutine(ClearText());
}
/// <summary>
/// 清理文本内容
/// </summary>
/// <returns></returns>
protected IEnumerator ClearText()
{
yield return new WaitForSeconds(3);
temporaryText.text = "";
}
private void Update()
{
if (ismove)
{
timer += Time.deltaTime * 0.5f;
}
else
{
timer -= Time.deltaTime * 0.5f;
}
if (timer > 1f)
{
ismove = false;
}
if (timer <= 0)
{
timer = 0;
}
gameObject.transform.position = Vector3.Lerp(startpos, TalkFoodPosi.position, timer);
}
}
其中用到了Find的方法 如果不懂 自行查找
然后是三个不同的顾客的脚本
顾客一
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UserOne : User
{
public override void Start()//重写实例化
{
base.Start();
temporaryText = GameObject.Find("customtalk1").GetComponent<Text>();
}
public override void LookScreen()//重写看大屏幕
{
base.LookScreen();
temporaryText.text = "顾客A看向大屏幕";
}
}
顾客二
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UserTwo : User
{
public override void Start()
{
base.Start();
Cd.someFood += WaitHamburger;
temporaryText = GameObject.Find("customtalk2").GetComponent<Text>();
}
/// <summary>
/// 服务员叫汉堡时,委托事件才会触发
/// </summary>
public bool WaitHamburger(string food)
{
if (food == "汉堡")
{
temporaryText.text = "这里,我要汉堡";
ismove = true;
StopCoroutine(ClearText());
StartCoroutine(ClearText());
return true;
}
return false;
}
public override void LookScreen()
{
base.LookScreen();
temporaryText.text = "顾客B看向大屏幕";
}
}
顾客三
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UserThree : User
{
public override void Start()//重写实例化
{
base.Start();
temporaryText = GameObject.Find("customtalk3 ").GetComponent<Text>();
//当 叫号的委托对象中,有多个方法调用时,需要用 += 添加
Cd.someFood += WaitCola;
//委托对象添加 顾客3的可乐
}
public override void LookScreen()//重写看大屏幕
{
base.LookScreen();
temporaryText.text = "顾客C看向大屏幕";
}
/// <summary>
/// 服务员叫可乐时,委托事件才会触发
/// </summary>
public bool WaitCola(string food)
{
if (food == "可乐")
{
temporaryText.text = "这里,我要的可乐";
ismove = true;
StopCoroutine(ClearText());
StartCoroutine(ClearText());
return true;
}
return false;
}
}
大家可以先把我的代码贴到项目上 然后根据注释慢慢理解
希望这篇博文对大家有所帮助