在我们的Unity游戏项目中,GUI的表现采用NGUI。记录一下我们的处理方式:

需要解决的问题

1、需要处理大量按钮的点击事件

2、需要处理界面跳转事件

3、需要处理界面元素更新事件

解决方案

GUIClickEventReceiver

解决按钮点击事件

GUIManager

管理GUI的跳转与更新

具体实现

Inspector选择事件

在编辑器中为按钮选择点击事件

[3D跑酷] UI事件处理系统_UI组件[3D跑酷] UI事件处理系统_UI组件_02

 

事件代码结构

[3D跑酷] UI事件处理系统_NGUI_03

主要逻辑

public void OnClick ()
    {
        bool playSoundEffect = true;
        switch (clickType) {
        case ClickType.StartGame:
            GameManager.instance.startGame ();
            break;
        case ClickType.Store:
            GUIManager.instance.showGUI (GUIState.Store);
            break;
        case ClickType.Stats:
            GUIManager.instance.showGUI (GUIState.Stats);
            break;
        case ClickType.DoubleCoinUpgrade:
            GameManager.instance.upgradePowerUp (PowerUpTypes.DoubleCoin);
            break;
        case ClickType.CoinMagnetUpgrade:
            GameManager.instance.upgradePowerUp (PowerUpTypes.CoinMagnet);
            break;
        case ClickType.InvincibilityUpgrade:
            GameManager.instance.upgradePowerUp (PowerUpTypes.Invincibility);
            break;
        case ClickType.AddSpeedUpgrade:
            GameManager.instance.upgradePowerUp (PowerUpTypes.AddSpeed);
            break;
        case ClickType.ProtectionCoverUpgrade:
            GameManager.instance.upgradePowerUp(PowerUpTypes.ProtectionCover);
            break;
        case ClickType.AttackGunUpgrade:
            GameManager.instance.upgradePowerUp(PowerUpTypes.AttackGun);
            break;
        case ClickType.EndGame:
            GUIManager.instance.showGUI (GUIState.EndGame);
            break;
        case ClickType.Restart:
            GameManager.instance.restartGame (true);
            break;
        case ClickType.MainMenu:
            GameManager.instance.backToMainMenu (false);
            break;
        case ClickType.MainMenuRestart:
            GameManager.instance.backToMainMenu (true);
            break;
        case ClickType.Pause:
            GameManager.instance.pauseGame (true);
            playSoundEffect = false;
            break;
        case ClickType.Resume:
            GameManager.instance.pauseGame (false);
            break;
        case ClickType.ToggleTutorial:
            GameManager.instance.toggleTutorial ();
            break;
        case ClickType.Missions:
            GUIManager.instance.showGUI (GUIState.Missions);
            break;
        }

        if (playSoundEffect)
            AudioManager.instance.playSoundEffect (SoundEffects.GUITapSoundEffect);
    }