游戏蛮牛学习群(纯技术交流,不闲聊):159852603
游戏蛮牛 手机端
开启辅助访问
 找回密码
 注册帐号

扫一扫,访问微社区

首页   >   博客   >   UnityTerminator

开发技巧-场景状态管理器(状态模式)

个人分类: Unity-开发技巧 | 2018-9-11 20:04
1.状态接口

[code]csharpcode:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 场景状态
/// </summary>
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()
    {

    }

    public override string ToString()
    {
        return string.Format("ISceneState:{0}", StateName);
    }
}

2.场景状态控制类

[code]csharpcode:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

/// <summary>
/// 场景状态控制者
/// </summary>
public class SceneStateController 
{

    private ISceneState m_State;
    private bool m_bRunBegin = false;

    public SceneStateController() { }

    //设定状态
    public void SetState(ISceneState State, string LoadSceneName)
    {
        m_bRunBegin = false;

        //载入场景
        LoadScene(LoadSceneName);

        //通知前一个状态结束
        if (m_State != null)
            m_State.StateEnd();

        //设定
        m_State = State;
    }

    /// <summary>
    /// 载入场景
    /// </summary>
    /// <param name="LoadScenename"></param>
    public void LoadScene(string LoadSceneName)
    {
        if (LoadSceneName == null || LoadSceneName.Length == 0)
            return;
        SceneManager.LoadScene(LoadSceneName);
    }

    /// <summary>
    /// 更新
    /// </summary>
    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();
    }
}
3.加入到GameManager类中

[code]csharpcode:

using UnityEngine;
using System.Collections;

/// <summary>
/// 游戏管理
/// </summary>
public class GameLoop : MonoBehaviour
{
    //场景状态
   
    public SceneStateController m_SceneStateController = new SceneStateController();
    public static GameLoop instance;

    public static int selectQiuNum;

    void Awake()
    {
        selectQiuNum = 0;
        instance = this;
        DontDestroyOnLoad(this.gameObject);
        Screen.sleepTimeout = SleepTimeout.NeverSleep; // 防止屏幕睡眠
        Application.targetFrameRate = 60;
    }

   
    void Start()
    {
        WindowManager.instance.Initialize();           //初始化界面
        WindowManager.instance.Open<LogoWnd>().Initialize();


        TimerManager.instance.Invoke(1.5f, () => 
        {
            m_SceneStateController.SetState(new StartState(m_SceneStateController), "");
        });
       
      
    }


    float dt;
    void Update()
    {
        dt = Time.deltaTime;
        TimerManager.instance.Update(dt);
        WindowManager.instance.Update(dt);
        m_SceneStateController.StateUpdate();
        //if (Input.GetKey(KeyCode.Escape))
        //    Application.Quit();
    }
}
4.添加场景状态

[code]csharpcode:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


public class LevelBattleState : ISceneState
{
    public LevelBattleState(SceneStateController Controller) : base(Controller)
    {
        StateName = "LevelBattleState";
    }

    public override void StateBegin()
    {
        WindowManager.instance.Close<SelectLevelWnd>();
        TimerManager.instance.Invoke(2f, () => { RollController.instance.isStart = true; });      
        WindowManager.instance.Open<LevelWnd>().Initialize();
    }
    public override void StateUpdate()
    {
        if (RollController.instance.isAgain)
        {
            m_Controller.SetState(new MainState(m_Controller), "Main");
        }

    }
    public override void StateEnd()
    {
        WindowManager.instance.Close<LevelSuccessWnd>();
        TimerManager.instance.Clear();
        PoolManager.instance.Clear();
    }
    public void SetStartState()
    {
        m_Controller.SetState(new MainState(m_Controller), "Main");
    }
}


5.执行切换场景时
m_SceneStateController.SetState(new StartState(m_SceneStateController), "");
New 一个就行了
0 0

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册帐号

个人分类

标签

阅读排行

评论排行

推荐博客

最新博客

关闭

站长推荐 上一条 /1 下一条

返回顶部