首页 » .NET » Unity设计模式之状态模式

Unity设计模式之状态模式

原文 http://blog.csdn.net/a977621265/article/details/78988331

2018-01-07 02:00:21阅读(626)

在多种状态中进行切换时,当前仅当只保持一种状态时,就可以用到状态模式。比如场景的切换,人物状态等。

用状态模式表示当前场景。

<a href=Unity设计模式之状态模式" src="http://img.blog.csdn.net/20180106135022257?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTk3NzYyMTI2NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

ISceneState:所有状态的基类。

public class ISceneState{
    //状态名称
    private string m_StateName = "ISceneState";
    public string StateName { get { return m_StateName; } set { m_StateName = value; } }
	//状态控制
    protected SceneStateController m_Controller = null;
    public ISceneState(SceneStateController controller)
    {
        m_Controller = controller;
    }
    public virtual void StateBegin()
    { //状态初始化}
    public virtual void StateEnd()
    { //状态结束}
    public virtual void StateUpdate()
    { //状态中的更新 }
}

SceneStateController:状态控制器。

public class SceneStateController{
    private ISceneState m_State;
    private bool m_bRunBegin = false;
    //设置状态
    public void SetState(ISceneState state, string loadSceneName)
    {
        m_bRunBegin = false;
        
        LoadScene(loadSceneName);
        if (m_State != null)
            m_State.StateEnd();
        m_State = state;
    }
    private void LoadScene(string loadSceneName)
    {
        if (loadSceneName == null || loadSceneName.Length == 0)
            return;
        SceneManager.LoadScene(loadSceneName);
    }
    public void StateUpdate()
    {
        if (Application.isLoadingLevel)
            return;
        if (m_State != null && m_bRunBegin == false)
        {
            m_State.StateBegin();
            m_bRunBegin = true;
        }
        if (m_State != null)
            m_State.StateUpdate();
    }
}

StartState:开始状态

public class StartState : ISceneState {
    public StartState(SceneStateController controller)
        : base(controller)
    {
        this.StateName = "StartState";
    }
    public override void StateUpdate()
    {
        m_Controller.SetState(new MenuState(m_Controller), "MenuScene");
    }
}
MenuState:菜单状态

public class MenuState : ISceneState {
    public MenuState(SceneStateController controller)
        : base(controller)
    {
        this.StateName = "MenuState";
    }
    public override void StateBegin()
    {
        Button btn = GameObject.Find("Sta rtGameButton").GetComponent<Button>();
        if (btn != null)
            btn.onClick.AddListener(() => OnStartGameBtnClick());
    }
    private void OnStartGameBtnClick()
    {
        m_Controller.SetState(new BattleState(m_Controller), "BattleScene");
    }
}
BattleState:游戏场景状态

public class BattleState : ISceneState {
    public BattleState(SceneStateController controller)
        : base(controller)
    {
        this.StateName = "BattleState";
    }
}

这样就简单实现了状态模式在unity中的场景应用。


最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹