這篇文章將為大家詳細講解有關Unity中有哪些常用的命令模式,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
成都創(chuàng)新互聯(lián)專注于尋甸企業(yè)網(wǎng)站建設,成都響應式網(wǎng)站建設公司,商城網(wǎng)站建設。尋甸網(wǎng)站建設公司,為尋甸等地區(qū)提供建站服務。全流程按需求定制制作,專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務命令模式,主要成員有提出要求的客戶、設置命令的收集者、執(zhí)行命令的接收者。客戶要求很簡單,點擊按扭就要實現(xiàn)一項目具體的效果,設置命令的收集者無需要知道命令如何執(zhí)行,只需要為執(zhí)行者做好配制。用命令的執(zhí)行者將執(zhí)行一個方法,所有的命令者是繼承于有這個方法的接口的類。
抽象到程序代碼中,這三類成員分別對應于界面上的用戶,RemoteControl (這里是隨便命名的),RemoteLoader
先制作如上的界面,方便你比較直觀的認識,其中左邊兩個是用于切換選擇不同的命令。下面第一個按扭可以執(zhí)行選中的命令,第二個按扭可以進行撤銷操作。
程序,UGUI面局如下,在Canvas下分別設置了執(zhí)行者和配制者。
制作好界面之后就可以來實現(xiàn)具體的腳本編輯了,分別創(chuàng)建好接口ICommand,配制腳本RemoteLoader和執(zhí)行腳本RemoteControl,結構如下:
在Commonds中,分別編寫了用于移動,旋轉,顏色,文字的腳本
這樣一來,就可以實現(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軸旋轉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> /// 執(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(); } }
關于Unity中有哪些常用的命令模式就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
標題名稱:Unity中有哪些常用的命令模式-創(chuàng)新互聯(lián)
分享路徑:http://www.chinadenli.net/article44/dhsehe.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航、外貿(mào)網(wǎng)站建設、用戶體驗、網(wǎng)站內(nèi)鏈、自適應網(wǎng)站、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)