标签:
大家好,我们今天来一起学习 小十传奇 的控制层。
为了结构美观,新建一个空节点,把Model,View,Ctrl都移动到Ten目录下面:
在assets目录下面新家Code文件夹,在COde下面新建Ctrl,View的C#脚本。
修改Ctrl.cs脚本
一个重要的名词:有限状态机 由游戏开发极客提供的工具包GDGeek包中包含。
有限状态机可以控制游戏的开始,游戏,结束状态的转换。
利用有限状态机添加三个状态。并且设置状态触发函数,代码如下:
using UnityEngine; using System.Collections; using GDGeek; public class Ctrl : MonoBehaviour { private FSM fsm = new FSM(); public View view=null; // Use this for initialization void Start () { fsm.addState ("begin",beginState()); fsm.addState ("play",playState()); fsm.addState ("end",endSdtate()); fsm.init ("begin"); } // Update is called once per frame void Update () { } State beginState () { StateWithEventMap state = new StateWithEventMap (); //带消息映射表的状态类 state.onStart += delegate { view.begin.gameObject.SetActive(true); }; state.onOver += delegate { view.begin.gameObject.SetActive(false); }; return state; } State playState () { StateWithEventMap state = new StateWithEventMap (); //带消息映射表的状态类 state.onStart += delegate { view.play.gameObject.SetActive(true); }; state.onOver += delegate { view.play.gameObject.SetActive(false); }; return state; } State endSdtate () { StateWithEventMap state = new StateWithEventMap (); //带消息映射表的状态类 state.onStart += delegate { view.end.gameObject.SetActive(true); }; state.onOver += delegate { view.end.gameObject.SetActive(false); }; return state; } }
其中利用在未定义的函数和变量上右键,选择快速创建可以节省不少时间:
同时在View中创建三个Public变量
using UnityEngine; using System.Collections; public class View : MonoBehaviour { public GameObject begin=null; public GameObject play=null; public GameObject end=null; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }
把View的Inspector面板上的变量和Start ,Play,End绑定起来
然后类似beginState,添加playState和endState:
State playState () { StateWithEventMap state = new StateWithEventMap (); //带消息映射表的状态类 state.onStart += delegate { view.play.gameObject.SetActive(true); }; state.onOver += delegate { view.play.gameObject.SetActive(false); }; return state; } State endSdtate () { StateWithEventMap state = new StateWithEventMap (); //带消息映射表的状态类 state.onStart += delegate { view.end.gameObject.SetActive(true); }; state.onOver += delegate { view.end.gameObject.SetActive(false); }; return state; }
然后我们准备运行游戏,运行之前我们选中Begin,Paly,End然后点击取消选中显示的勾选。
这样,在初始状态三个界面都不显示,然后界面在Ctrl.cs的脚本控制下运行,运行截图,正确显示初始界面:
为开始界面的按钮添加响应:
在Ctrl中新建public函数:
public void fsmPost(string msg) { fsm.post (msg); }
然后在start界面中,点击按钮,在Inspector面板中增加响应:
点击加号添加响应函数,把Hierarchy中的Ctrl这个对象拖到第一个框中,右边选择fsmPost函数
在第二个框中填写clickStart(千万不要像视频教程中那样写成begin,会把人搞混淆的),这个框中的内容就是点击按钮后,执行fsmPost时自动传入的参数值。
然后在beginState函数值添加事件监听:
state.addEvent("clickStart","play"); //得到开始按钮点击事件的话,进去play状态
这个函数,第一个参数是按钮点击时参数值,第二个参数是要跳转的状态的名称,因为之前写过这段话
fsm.addState ("begin",beginState()); fsm.addState ("play",playState()); fsm.addState ("end",endSdtate());
所以,一旦跳转到Play状态,beginState就会执行onOver响应,playState会执行onStart响应,表现出来就是从开始界面跳转到游戏界面。
好的,今天就写到这里,主要学习了两点:
我是orzangleli,谢谢你一直阅读到现在,如果你看到了这篇文章,请麻烦回复我下,给我点动力更新下去,多谢!
完整代码:ctrl.cs
using UnityEngine; using System.Collections; using GDGeek; public class Ctrl : MonoBehaviour { private FSM fsm = new FSM(); public View view=null; public void fsmPost(string msg) { fsm.post (msg); } // Use this for initialization void Start () { fsm.addState ("begin",beginState()); fsm.addState ("play",playState()); fsm.addState ("end",endSdtate()); fsm.init ("begin"); } // Update is called once per frame void Update () { } State beginState () { StateWithEventMap state = new StateWithEventMap (); //带消息映射表的状态类 state.onStart += delegate { view.begin.gameObject.SetActive(true); }; state.onOver += delegate { view.begin.gameObject.SetActive(false); }; state.addEvent("clickStart","play"); //得到开始按钮电击事件的话,进去play状态 return state; } State playState () { StateWithEventMap state = new StateWithEventMap (); //带消息映射表的状态类 state.onStart += delegate { view.play.gameObject.SetActive(true); }; state.onOver += delegate { view.play.gameObject.SetActive(false); }; return state; } State endSdtate () { StateWithEventMap state = new StateWithEventMap (); //带消息映射表的状态类 state.onStart += delegate { view.end.gameObject.SetActive(true); }; state.onOver += delegate { view.end.gameObject.SetActive(false); }; return state; } }
View.cs
using UnityEngine; using System.Collections; public class View : MonoBehaviour { public GameObject begin=null; public GameObject play=null; public GameObject end=null; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }
标签:
原文地址:http://www.cnblogs.com/orzangleli/p/4995781.html