标签:项目 art public 继承 项目开发 更新 封装 code 工业
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,国家专利发明人;已出版书籍:《手把手教你架构3D游戏引擎》电子工业出版社和《Unity3D实战核心技术详解》电子工业出版社等。
CSDN视频网址:http://edu.csdn.net/lecturer/144、
上篇博客实现了LoginState的封装,其实作为UI的各个窗口,每个窗口都对应着自己的State也就是状态类。这么多状态类,不能鼓孤立的存在着,它也需要我们封装一个状态管理类去处理所有的状态,它的主要功能是注册状态, 状态之间的切换等操作,该函数继承于单例模式,代码如下所示:
using UnityEngine; using System.Collections; using System.Collections.Generic; namespace Game.GameState { public enum GameStateType { GS_Continue, GS_Login, GS_User, GS_Lobby, GS_Room, GS_Hero, GS_Loading, GS_Play, GS_Over, } public class GameStateManager : Singleton<GameStateManager> { Dictionary<GameStateType, IGameState> gameStates; IGameState currentState; public GameStateManager() { gameStates = new Dictionary<GameStateType, IGameState>(); IGameState gameState; gameState = new LoginState(); gameStates.Add(gameState.GetStateType(), gameState); gameState = new UserState(); gameStates.Add(gameState.GetStateType(), gameState); gameState = new LobbyState(); gameStates.Add(gameState.GetStateType(), gameState); gameState = new RoomState(); gameStates.Add(gameState.GetStateType(), gameState); gameState = new HeroState(); gameStates.Add(gameState.GetStateType(), gameState); gameState = new LoadingState(); gameStates.Add(gameState.GetStateType(), gameState); gameState = new PlayState(); gameStates.Add(gameState.GetStateType(), gameState); gameState = new OverState(); gameStates.Add(gameState.GetStateType(), gameState); } public IGameState GetCurState() { return currentState; } public void ChangeGameStateTo(GameStateType stateType) { if (currentState != null && currentState.GetStateType() != GameStateType.GS_Loading && currentState.GetStateType() == stateType) return; if (gameStates.ContainsKey(stateType)) { if (currentState != null) { currentState.Exit(); } currentState = gameStates[stateType]; currentState.Enter(); } } public void EnterDefaultState() { ChangeGameStateTo(GameStateType.GS_Login); } public void FixedUpdate(float fixedDeltaTime) { if (currentState != null) { currentState.FixedUpdate(fixedDeltaTime); } } public void Update(float fDeltaTime) { GameStateType nextStateType = GameStateType.GS_Continue; if (currentState != null) { nextStateType = currentState.Update(fDeltaTime); } if (nextStateType > GameStateType.GS_Continue) { ChangeGameStateTo(nextStateType); } } public IGameState getState(GameStateType type) { if (!gameStates.ContainsKey(type)) { return null; } return gameStates[type]; } } }
void Awake(){ WindowManager.Instance.ChangeScenseToLogin(EScenesType.EST_None); } // Use this for initialization void Start () { GameStateManager.Instance.EnterDefaultState(); }
//更新游戏状态机 GameStateManager.Instance.Update(Time.deltaTime); //UI更新 WindowManager.Instance.Update(Time.deltaTime);
标签:项目 art public 继承 项目开发 更新 封装 code 工业
原文地址:http://blog.csdn.net/jxw167/article/details/69003830