亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Unity常用命令模式詳解

 更新時(shí)間:2019年03月07日 08:45:17   作者:探求虛無(wú)  
這篇文章主要為大家詳細(xì)介紹了Unity常用命令模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

本文主要將的將是在Unity中實(shí)現(xiàn)一個(gè)以常聽說(shuō)的命令模式為設(shè)計(jì)原理,實(shí)現(xiàn)一個(gè)可以撤銷移動(dòng)、旋轉(zhuǎn)、顏色和文字信息的小Demo。

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

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

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

程序,UGUI面局如下,在Canvas下分別設(shè)置了執(zhí)行者和配制者。

制作好界面之后就可以來(lái)實(shí)現(xiàn)具體的腳本編輯了,分別創(chuàng)建好接口ICommand,配制腳本RemoteLoader和執(zhí)行腳本RemoteControl,結(jié)構(gòu)如下:

在Commonds中,分別編寫了用于移動(dòng),旋轉(zhuǎn),顏色,文字的腳本

這樣一來(lái),就可以實(shí)現(xiàn)一個(gè)可撤銷的命令模式了,效果如下所示:

其中用于保存undo方法和具體怎么undo都是使用Stack來(lái)實(shí)現(xiàn)的,下面分別是部分代碼實(shí)現(xiàn) :

一、接口

public interface ICommand
{
  void Execute();
  void UnDo();
}

二、執(zhí)行器

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>
  /// 執(zhí)行
  /// </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方向移動(dòng)";
    commands[0] = new MoveCommand(cube.transform, Vector3.right);
    textinfos[1] = "y方向移動(dòng)";
    commands[1] = new MoveCommand(cube.transform, Vector3.up);
    textinfos[2] = "z方向移動(dòng)";
    commands[2] = new MoveCommand(cube.transform, Vector3.forward);

    textinfos[3] = "x軸旋轉(zhuǎn)10度";
    commands[3] = new RotateCommand(cube.transform, Vector3.right * 10);
    textinfos[4] = "y軸旋轉(zhuǎn)10度";
    commands[4] = new RotateCommand(cube.transform, Vector3.up * 10);
    textinfos[5] = "z軸旋轉(zhuǎn)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] = "變藍(lán)";
    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--;
  }

}

四、顏色轉(zhuǎn)換命令腳本

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();
  }
}

五、移動(dòng)命令腳本

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;
  }
}

六、轉(zhuǎn)換命令腳本

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>
  /// 執(zhí)行
  /// </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();
  }
}

僅供參考,謝謝閱讀。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

  • C#中XmlTextWriter讀寫xml文件詳細(xì)介紹

    C#中XmlTextWriter讀寫xml文件詳細(xì)介紹

    .NET中包含了很多支持XML的類,這些類使得程序員使用XML編程就如同理解XML文件一樣簡(jiǎn)單。在這篇文章中,我將給出這樣的一個(gè)類的使用示例,這個(gè)類就是XmlTextWriter類
    2013-04-04
  • Winform界面中實(shí)現(xiàn)通用工具欄按鈕的事件處理方法

    Winform界面中實(shí)現(xiàn)通用工具欄按鈕的事件處理方法

    下面小編就為大家分享一篇Winform界面中實(shí)現(xiàn)通用工具欄按鈕的事件處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助
    2017-11-11
  • C#使用WebClient實(shí)現(xiàn)上傳下載

    C#使用WebClient實(shí)現(xiàn)上傳下載

    這篇文章介紹了C#使用WebClient實(shí)現(xiàn)上傳下載的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C# WinForm捕獲未處理的異常實(shí)例解析

    C# WinForm捕獲未處理的異常實(shí)例解析

    這篇文章主要介紹了C# WinForm捕獲未處理的異常,包括了常見的未捕獲的異常、UI線程異常、非UI線程異常等,非常實(shí)用,需要的朋友可以參考下
    2014-09-09
  • C# 獲取IP及判斷IP是否在區(qū)間

    C# 獲取IP及判斷IP是否在區(qū)間

    本文主要介紹了C# 獲取IP及判斷IP是否在區(qū)間的方法。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-02-02
  • C# Rx的主要接口深入理解

    C# Rx的主要接口深入理解

    這篇文章主要介紹了C# Rx的主要接口深入理解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • C#使用iTextSharp從PDF文檔獲取內(nèi)容的方法

    C#使用iTextSharp從PDF文檔獲取內(nèi)容的方法

    這篇文章主要介紹了C#使用iTextSharp從PDF文檔獲取內(nèi)容的方法,涉及C#基于iTextSharp操作pdf文件的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • C#數(shù)據(jù)導(dǎo)入到EXCEL的方法

    C#數(shù)據(jù)導(dǎo)入到EXCEL的方法

    今天小編就為大家分享一篇關(guān)于C#數(shù)據(jù)導(dǎo)入到EXCEL的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • C# Dockpanel入門基礎(chǔ)必看篇

    C# Dockpanel入門基礎(chǔ)必看篇

    這篇文章主要介紹了C# Dockpanel入門基礎(chǔ)必看篇,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • WinForm中KeyDown,KeyPress和KeyUp的順序與區(qū)別解析

    WinForm中KeyDown,KeyPress和KeyUp的順序與區(qū)別解析

    這篇文章主要介紹了WinForm中KeyDown,KeyPress和KeyUp的順序與區(qū)別解析,對(duì)C#初學(xué)者來(lái)說(shuō)很有學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下
    2014-08-08

最新評(píng)論