[3D跑酷] GUIManager UI管理
本文为作者原创,转载请注明出处:https://www.cnblogs.com/zhaoqingqing/p/3398876.html
UI元素更新及界面跳转#
继上篇日志《Unity开发之 GUIClickEventReceiver》,再谈一下我们如何管理游戏中的UI元素更新及界面跳转
UI绑定#
图一:Inspector面板 Public GameObjectName与GameObject一一对应
UI结构及命名规范#
图二:Hierarchy面板 UI父子结构及组件命名规范
UI枚举种类#
图三:enum GUIState
UI绑定代码#
图四:public UI控件定义 与Hierarchy命名规范
UI主要方法及逻辑#
图五:主要方法及逻辑
主要方法#
1、隐藏Transform及子Transform
#if !UNITY_3_5 private void activeRecursively(Transform obj, bool active) { foreach (Transform child in obj) { activeRecursively(child, active); } obj.gameObject.SetActive(active); } #endif
private GameObject panelFromState(GUIState state) { switch (state) { case GUIState.MainMenu: return mainMenuPanel; case GUIState.InGame: return inGamePanel; case GUIState.EndGame: return endGamePanel; case GUIState.Store: return storePanel; case GUIState.Stats: return statsPanel; case GUIState.Pause: return pausePanel; case GUIState.Tutorial: return tutorialPanel; case GUIState.Missions: return missionsPanel; } return null; // how'd we get here? }
private void changeGUIState(bool activate, GUIState state) { GameObject activePanel = panelFromState(state); if (activePanel != null) { #if UNITY_3_5 activePanel.SetActiveRecursively(activate); #else activeRecursively(activePanel.transform, activate); #endif } }
public void showGUI(GUIState state) { // activate the new gui state, deactivate the old. changeGUIState(true, state); changeGUIState(false, guiState); switch (state) { case GUIState.EndGame: endGameScore.text = dataManager.getScore().ToString(); endGameCoins.text = dataManager.getLevelCoins().ToString(); endGameMultiplier.text = string.Format("{0}x", missionManager.getScoreMultiplier()); break; case GUIState.Store: refreshStoreGUI(); // go back to the correct menu that we came from if (guiState == GUIState.MainMenu) { storeBackButtonReceiver.clickType = ClickType.MainMenu; } else { // coming from GUIState.EndGame storeBackButtonReceiver.clickType = ClickType.EndGame; } break; //...... } }
public void refreshStoreGUI() { storeTotalCoins.text = dataManager.getTotalCoins().ToString(); int cost = dataManager.getPowerUpCost(PowerUpTypes.DoubleCoin); if (cost != -1) { storeCoinDoublerCost.text = string.Format("{0} Coins", cost); } else { #if UNITY_3_5 storeCoinDoublerGroup.gameObject.SetActiveRecursively(false); #else storeCoinDoublerGroup.SetActive(false); #endif } cost = dataManager.getPowerUpCost(PowerUpTypes.CoinMagnet); if (cost != -1) { storeCoinMagnetCost.text = string.Format("{0} Coins", cost); } else { #if UNITY_3_5 storeCoinMagnetGroup.SetActiveRecursively(false); #else storeCoinMagnetGroup.SetActive(false); #endif } cost = dataManager.getPowerUpCost(PowerUpTypes.Invincibility); if (cost != -1) { storeInvincibilityCost.text = string.Format("{0} Coins", cost); } else { #if UNITY_3_5 storeInvincibilityGroup.SetActiveRecursively(false); #else storeInvincibilityGroup.SetActive(false); #endif } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器