国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Unity常用命令模式詳解

2019-10-29 19:32:16
字體:
來源:轉載
供稿:網友

在調用一些簡單的方法實現一系列的動作時,回退的問題比較重要。作為一款用戶體驗良好的產品而言,有回退功能將顯得比較人性化,想想如果我們常用的window,在刪除一個文件后無法恢復將變得多么的糟糕。更為直觀的例子是在玩一些小游戲時,比如象棋、推箱子,提供了悔棋的功能,用戶有了更多選擇的余地。

本文主要將的將是在Unity中實現一個以常聽說的命令模式為設計原理,實現一個可以撤銷移動、旋轉、顏色和文字信息的小Demo。

命令模式,主要成員有提出要求的客戶、設置命令的收集者、執行命令的接收者。客戶要求很簡單,點擊按扭就要實現一項目具體的效果,設置命令的收集者無需要知道命令如何執行,只需要為執行者做好配制。用命令的執行者將執行一個方法,所有的命令者是繼承于有這個方法的接口的類。

抽象到程序代碼中,這三類成員分別對應于界面上的用戶,RemoteControl (這里是隨便命名的),RemoteLoader

Unity,命令模式

先制作如上的界面,方便你比較直觀的認識,其中左邊兩個是用于切換選擇不同的命令。下面第一個按扭可以執行選中的命令,第二個按扭可以進行撤銷操作。

程序,UGUI面局如下,在Canvas下分別設置了執行者和配制者。

Unity,命令模式

制作好界面之后就可以來實現具體的腳本編輯了,分別創建好接口ICommand,配制腳本RemoteLoader和執行腳本RemoteControl,結構如下:

Unity,命令模式

在Commonds中,分別編寫了用于移動,旋轉,顏色,文字的腳本

Unity,命令模式

這樣一來,就可以實現一個可撤銷的命令模式了,效果如下所示:

Unity,命令模式

其中用于保存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();  }}

僅供參考,謝謝閱讀。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 呼和浩特市| 龙胜| 海阳市| 昌邑市| 长治市| 玉山县| 喀什市| 灵丘县| 凤翔县| 随州市| 大竹县| 岫岩| 长泰县| 黄骅市| 连城县| 垫江县| 辉南县| 萍乡市| 利津县| 五河县| 缙云县| 广安市| 封丘县| 临沂市| 哈巴河县| 禄劝| 乐清市| 通河县| 河南省| 林芝县| 仁布县| 伊宁县| 本溪市| 洪洞县| 满洲里市| 永年县| 县级市| 沂南县| 宁都县| 武山县| 方山县|