跳至主要内容

[3D跑酷] GUIManager UI管理

UI元素更新及界面跳转

继上篇日志《Unity开发之 GUIClickEventReceiver》,再谈一下我们如何管理游戏中的UI元素更新及界面跳转

UI绑定

image
图一:Inspector面板 Public GameObjectName与GameObject一一对应

UI结构及命名规范

image
图二:Hierarchy面板 UI父子结构及组件命名规范

UI枚举种类

image
图三:enum GUIState

UI绑定代码

image
图四:public UI控件定义 与Hierarchy命名规范

UI主要方法及逻辑

image
图五:主要方法及逻辑

主要方法

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
        }
    }

评论

此博客中的热门博文

Unity3D研究院之提取游戏资源的三个工具支持Unity5(八十四)

这两天无意间又发现了两个提取Unity游戏资源的工具,这会儿刚好有时间我就码点字总结一下。 一、disunity 因为之前写过了所以这里就不介绍了 。Unity3D研究院之mac上从.ipa中提取unity3D游戏资源(六十六)  http://www.xuanyusong.com/archives/2584 二、UnityAssetsExplorer 下载地址: http://www.nexusmods.com/pillarsofeternity/mods/27/?tab=2&navtag=http%3A%2F%2Fwww.nexusmods.com%2Fpillarsofeternity%2Fajax%2Fmodfiles%2F%3Fid%3D27&pUp=1 百度云下载地址: http://pan.baidu.com/s/1AwTrg 如果你想解Unity5的游戏包,请使用 UnityAssetsExplorer 1.5 以上版本。 百度云下载地址: http://pan.baidu.com/s/1sjkjSJz 如下图所示,OpenAssets-File 选一个 .asset 或者一个.assetbundle 然后Extract All即可导出,感觉只是在disunity上增加了一个可视化的功能。 三、UnityStudio 其实我觉得这个工具非常牛逼、前面两个都只能把资源解开,预览图片资源得用别的工具才能打开。然而UnityStudio可以直接在自己的软件上查 看图片、shader、文本、还能直接播放音频、甚至还能看场景Hierarchy视图的树状结构。强烈推荐用UnityStudio啊。 下载地址: http://forum.xentax.com/viewtopic.php?f=10&t=11807 百度云下载地址: http://pan.baidu.com/s/1hqjMnOg UnityStudio最新版本下载 支持unity5.x   https://github.com/Perfare/UnityStudio/releases 直接预览图片 直接播放音频 查看场景树状结构 查看Shader代码。 ...

[3D跑酷] AudioManager

Unity音频管理 游戏中的声音管理最常用的组件莫过于AudioSource和AudioClip,我的做法是建立是一个AudioManager类(单例类)管理各个音频,谈一下我的经验: 函数列表 Start函数:设置音频整体参数; 编辑器面板 拖拽文件赋值 AudioSource文档 逻辑实现代码 public void playSoundEffect(SoundEffects soundEffect) { AudioClip clip = null ; float pitch = 1; switch (soundEffect) { case SoundEffects.ObstacleCollisionSoundEffect: clip = obstacleCollision; break ; case SoundEffects.CoinSoundEffect: clip = coinCollection; pitch = 1.5f; break ; case SoundEffects.PowerUpSoundEffect: clip = powerUpCollection; break ; case SoundEffects.GameOverSoundEffect: clip = gameOver; break ; case SoundEffects.GUITapSoundEffect: clip = guiTap; break ; } soundEffectsAudio.pitch = pitch; //音调 so...