Unity常用命令模式详解
作者:探求虚无 发布时间:2021-12-30 16:58:08
在调用一些简单的方法实现一系列的动作时,回退的问题比较重要。作为一款用户体验良好的产品而言,有回退功能将显得比较人性化,想想如果我们常用的window,在删除一个文件后无法恢复将变得多么的糟糕。更为直观的例子是在玩一些小游戏时,比如象棋、推箱子,提供了悔棋的功能,用户有了更多选择的余地。
本文主要将的将是在Unity中实现一个以常听说的命令模式为设计原理,实现一个可以撤销移动、旋转、颜色和文字信息的小Demo。
命令模式,主要成员有提出要求的客户、设置命令的收集者、执行命令的接收者。客户要求很简单,点击按扭就要实现一项目具体的效果,设置命令的收集者无需要知道命令如何执行,只需要为执行者做好配制。用命令的执行者将执行一个方法,所有的命令者是继承于有这个方法的接口的类。
抽象到程序代码中,这三类成员分别对应于界面上的用户,RemoteControl (这里是随便命名的),RemoteLoader
先制作如上的界面,方便你比较直观的认识,其中左边两个是用于切换选择不同的命令。下面第一个按扭可以执行选中的命令,第二个按扭可以进行撤销操作。
程序,UGUI面局如下,在Canvas下分别设置了执行者和配制者。
制作好界面之后就可以来实现具体的脚本编辑了,分别创建好接口ICommand,配制脚本RemoteLoader和执行脚本RemoteControl,结构如下:
在Commonds中,分别编写了用于移动,旋转,颜色,文字的脚本
这样一来,就可以实现一个可撤销的命令模式了,效果如下所示:
其中用于保存undo方法和具体怎么undo都是使用Stack来实现的,下面分别是部分代码实现 :
一、接口
public interface ICommand
{
void Execute();
void UnDo();
}
二、执行器
public class RemoteControl : MonoBehaviour {
public Button ctrlBtn;
public Button undoBtn;
public Text ctrlName;
private ICommand icommand;
public Stack<UnityAction> undoFunctions = new Stack<UnityAction>();
void Awake(){
ctrlBtn.onClick.AddListener(OnCtrlBtnClicked);
undoBtn.onClick.AddListener(OnUnDoBtnClicked);
}
public void SetText(string textinfo)
{
ctrlName.text = textinfo;
}
public void SetCommond(ICommand icommand)
{
this.icommand = icommand;
}
/// <summary>
/// 执行
/// </summary>
public void OnCtrlBtnClicked()
{
if (icommand != null)
{
icommand.Execute();
undoFunctions.Push(icommand.UnDo);
}
}
/// <summary>
/// 撤销
/// </summary>
private void OnUnDoBtnClicked()
{
if (undoFunctions.Count > 0)
{
undoFunctions.Pop().Invoke();
}
}
}
三、配制加载器
public class RemoteLoader : MonoBehaviour
{
public Button lastBtn;
public Button nextBtn;
private int index;
private const int NUM_COMMAND = 10;
private ICommand[] commands;
private string[] textinfos;
private MoveCommand movexCmd;
private MoveCommand moveyCmd;
private MoveCommand movezCmd;
private RotateCommand rotxCmd;
private RotateCommand rotyCmd;
private RotateCommand rotzCmd;
private ColorChangeCommand redColorCmd;
private ColorChangeCommand greenColorCmd;
private ColorChangeCommand blueColorCmd;
private TextChangeCommand textCmd;
private string[] infos = { "A","B", "C", "D", "E", "F" };
public RemoteControl remoteCtrl;
public GameObject cube;
void Awake()
{
lastBtn.onClick.AddListener(OnLastBtnClicked);
nextBtn.onClick.AddListener(OnNextBtnClicked);
}
void Start()
{
commands = new ICommand[NUM_COMMAND];
textinfos = new string[NUM_COMMAND];
textinfos[0] = "x方向移动";
commands[0] = new MoveCommand(cube.transform, Vector3.right);
textinfos[1] = "y方向移动";
commands[1] = new MoveCommand(cube.transform, Vector3.up);
textinfos[2] = "z方向移动";
commands[2] = new MoveCommand(cube.transform, Vector3.forward);
textinfos[3] = "x轴旋转10度";
commands[3] = new RotateCommand(cube.transform, Vector3.right * 10);
textinfos[4] = "y轴旋转10度";
commands[4] = new RotateCommand(cube.transform, Vector3.up * 10);
textinfos[5] = "z轴旋转10度";
commands[5] = new RotateCommand(cube.transform, Vector3.forward * 10);
textinfos[6] = "变红";
commands[6] = new ColorChangeCommand(Color.red, cube.GetComponent<Renderer>().material);
textinfos[7] = "变绿";
commands[7] = new ColorChangeCommand(Color.green, cube.GetComponent<Renderer>().material);
textinfos[8] = "变蓝";
commands[8] = new ColorChangeCommand(Color.blue, cube.GetComponent<Renderer>().material);
textinfos[9] = "换信息";
commands[9] = new TextChangeCommand(cube.GetComponentInChildren<TextMesh>(), infos);
}
private void OnNextBtnClicked()
{
if (index == NUM_COMMAND || index == -1)
{
index = 0;
}
remoteCtrl.SetCommond(commands[index]);
remoteCtrl.SetText(textinfos[index]);
index++;
}
private void OnLastBtnClicked()
{
if (index == NUM_COMMAND || index == -1)
{
index = NUM_COMMAND - 1;
}
remoteCtrl.SetCommond(commands[index]);
remoteCtrl.SetText(textinfos[index]);
index--;
}
}
四、颜色转换命令脚本
public class ColorChangeCommand : ICommand
{
private Stack<Color> m_OriginColor = new Stack<Color>();
private Color m_Color;
private Material m_Material;
public ColorChangeCommand(Color color, Material material)
{
m_Color = color;
m_Material = material;
}
public void Execute()
{
m_OriginColor.Push(m_Material.color);
m_Material.color = m_Color;
}
public void UnDo()
{
m_Material.color = m_OriginColor.Pop();
}
}
五、移动命令脚本
public class MoveCommand : ICommand
{
private Vector3 m_Offset;
private Transform m_Object;
public MoveCommand(Transform obj, Vector3 offset)
{
this.m_Object = obj;
this.m_Offset = offset;
}
public void Execute()
{
m_Object.transform.position += m_Offset;
}
public void UnDo()
{
m_Object.transform.position -= m_Offset;
}
}
六、转换命令脚本
public class RemoteControl : MonoBehaviour {
public Button ctrlBtn;
public Button undoBtn;
public Text ctrlName;
private ICommand icommand;
public Stack<UnityAction> undoFunctions = new Stack<UnityAction>();
void Awake(){
ctrlBtn.onClick.AddListener(OnCtrlBtnClicked);
undoBtn.onClick.AddListener(OnUnDoBtnClicked);
}
public void SetText(string textinfo)
{
ctrlName.text = textinfo;
}
public void SetCommond(ICommand icommand)
{
this.icommand = icommand;
}
/// <summary>
/// 执行
/// </summary>
public void OnCtrlBtnClicked()
{
if (icommand != null)
{
icommand.Execute();
undoFunctions.Push(icommand.UnDo);
}
}
/// <summary>
/// 撤销
/// </summary>
private void OnUnDoBtnClicked()
{
if (undoFunctions.Count > 0)
{
undoFunctions.Pop().Invoke();
}
}
}
七、文字加载脚本
public class TextChangeCommand : ICommand
{
private Stack<string> lastInfos = new Stack<string>();
private IEnumerator<string> datas;
private TextMesh m_Textmesh;
public TextChangeCommand(TextMesh textMesh,ICollection<string> texts)
{
datas = texts.GetEnumerator();
m_Textmesh = textMesh;
}
public void Execute()
{
if (!datas.MoveNext())
{
datas.Reset();
datas.MoveNext();
}
lastInfos.Push(m_Textmesh.text);
m_Textmesh.text = datas.Current;
}
public void UnDo()
{
m_Textmesh.text = lastInfos.Pop();
}
}
仅供参考,谢谢阅读。
来源:https://blog.csdn.net/tankerhunter/article/details/52058054
猜你喜欢
- 前章知识: 点此跳转HTML简介:超文本是一种组织信息的方式,它通过超级链接方法将文本中的文字、图表与其他信息媒体相关联。这些相互关联的信息
- 本文实例讲述了Android编程之绘制文本(FontMetrics)实现方法。分享给大家供大家参考,具体如下:Canvas 作为绘制文本时,
- 在项目中如果涉及到用Excel开发的报表模版来导出报表数据的话,一般都是在Excel报表中使用VBA做成宏来进行调用。即先使用Excel自带
- SEATA概要seata 是alibaba 出的一款分布式事务管理器,他有侵入性小,实现简单等特点。我们能够使用seata 实现分布式事务管
- 一般文本文件我们以日志文件.log文件为例:import java.io.BufferedReader; import java.io.Fi
- 一,“==”与equals()运行以下代码,如何解释其输出结果?public class StringPool { public
- 前言对于字符串的操作,我们常用的就是trim()去除前后空格、subString()截取子字符串,其他的用的不多。下表中是字符串常用的方法。
- 本文我们要谈的七大原则,即:单一职责,里氏替换,迪米特法则,依赖倒转,接口隔离,合成/聚合原则,开放-封闭 。1.
- List<T>是怎么存放元素?我们扒一段List<T>的一段源码来一窥究竟。using System;using S
- 从Microsoft .Net 2.0版本以后,就默认提供了System.IO.Ports.SerialPort类,用户可以非常简单地编写少
- 一般学过C#的都知道,Array 一旦定义好,比如四个长度,当需要再往里面添加元素的时候,需要Array.Resize一下才可以。有鉴于此,
- 方法一:Hashtable ht = new Hashtable();  
- 前言在Java 8之前,默认情况下,接口中的所有方法都是公共的和抽象的。但是这一限制在Java 8中被打破了,Java 8允许开发人员在接口
- 一、前往 jetbrains 官网下载 IDEA Ultimate版本https://www.jetbrains.com/idea/down
- 一、什么是封装?封装就是将属性私有化,提供公有的方法访问私有属性。做法就是:修改属性的可见性来限制对属性的访问,并为每个属性创建一对取值(g
- 本文实例讲述了Java实现的计时器【秒表】功能。分享给大家供大家参考,具体如下:应用名称:Java计时器用到的知识:Java GUI编程开发
- 对象POJO和JSON互转public class JsonUtil { /** * JSON 转 POJO &n
- 在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析。一、
- 在web应用中,常常会遇见点击某个链接会弹出一个新的窗口,或者是相互关联的web应用 ,这样要去操作新窗口中的元素,就需要主机切换到新窗口进
- 本篇是基于TornadoFx框架对Tooltip组件进行讲解,使用Kotlin语言,和传统Java使用有所区别,仅供参考介绍鼠标悬浮在某个控