最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity常用命令模式詳解

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

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

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

命令模式,主要成員有提出要求的客戶、設(shè)置命令的收集者、執(zhí)行命令的接收者??蛻粢蠛芎唵危c擊按扭就要實現(xiàn)一項目具體的效果,設(shè)置命令的收集者無需要知道命令如何執(zhí)行,只需要為執(zhí)行者做好配制。用命令的執(zhí)行者將執(zhí)行一個方法,所有的命令者是繼承于有這個方法的接口的類。

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

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

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

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

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

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

其中用于保存undo方法和具體怎么undo都是使用Stack來實現(xiàn)的,下面分別是部分代碼實現(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方向移動";
    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軸旋轉(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] = "變藍";
    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();
  }
}

五、移動命令腳本

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

僅供參考,謝謝閱讀。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

  • C#利用TreeView控件實現(xiàn)目錄跳轉(zhuǎn)

    C#利用TreeView控件實現(xiàn)目錄跳轉(zhuǎn)

    這篇文章主要為大家詳細介紹了C#潤滑利用TreeView控件實現(xiàn)目錄跳轉(zhuǎn)功能,文中的示例代碼講解詳細,感興趣的小伙伴可以動手嘗試一下
    2022-07-07
  • 基于私鑰加密公鑰解密的RSA算法C#實現(xiàn)方法

    基于私鑰加密公鑰解密的RSA算法C#實現(xiàn)方法

    這篇文章主要介紹了基于私鑰加密公鑰解密的RSA算法C#實現(xiàn)方法,是應用非常廣泛,需要的朋友可以參考下
    2014-08-08
  • C#使用NPOI將excel導入到list的方法

    C#使用NPOI將excel導入到list的方法

    這篇文章主要為大家詳細介紹了C#使用NPOI將excel導入到list的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • c# winform 解決PictureBox 無法打印全部圖片的問題

    c# winform 解決PictureBox 無法打印全部圖片的問題

    這篇文章主要介紹了c# winform 解決PictureBox 無法打印全部圖片的問題,幫助大家更好進行c# winform開發(fā),感興趣的朋友可以了解下
    2020-12-12
  • C#項目中跨文件調(diào)用公共類的實例方法

    C#項目中跨文件調(diào)用公共類的實例方法

    在本篇文章里小編給大家整理的是關(guān)于C#項目中如何跨文件調(diào)用公共類的知識點內(nèi)容,需要的朋友們學習下。
    2019-08-08
  • C#泛型委托的用法實例分析

    C#泛型委托的用法實例分析

    這篇文章主要介紹了C#泛型委托的用法,以實例形式較為詳細的分析了C#委托的功能與相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05
  • c#并行編程示例分享

    c#并行編程示例分享

    這篇文章主要介紹了c#并行編程示例,大家直接看下面的代碼吧
    2014-01-01
  • C# Autofac的具體使用

    C# Autofac的具體使用

    Autofac是.NET領(lǐng)域最為流行的IoC框架之一,本文主要介紹了C# Autofac的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-08-08
  • C#實現(xiàn)的UDP收發(fā)請求工具類實例

    C#實現(xiàn)的UDP收發(fā)請求工具類實例

    這篇文章主要介紹了C#實現(xiàn)的UDP收發(fā)請求工具類,結(jié)合具體實例形式分析了C#針對UDP請求的監(jiān)聽、接收、發(fā)送等相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • C#反序列化到類的實現(xiàn)方法

    C#反序列化到類的實現(xiàn)方法

    這篇文章主要介紹了C#反序列化到類的實現(xiàn)方法,涉及C#反序列化的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05

最新評論

泊头市| 利辛县| 鄢陵县| 嘉峪关市| 桂东县| 斗六市| 双城市| 平潭县| 博罗县| 波密县| 乐至县| 古丈县| 庐江县| 泉州市| 河源市| 黑龙江省| 武清区| 锦屏县| 都兰县| 侯马市| 密云县| 吴桥县| 信宜市| 仁怀市| 长沙县| 象山县| 怀柔区| 威海市| 孙吴县| 彭州市| 濉溪县| 昌图县| 皮山县| 长宁区| 若尔盖县| 沾化县| 喀喇| 台中县| 桂平市| 延长县| 荔浦县|