使用下拉 Unity 隐藏/显示面板

我刚刚开始了一些基本游戏内容的统一项目。


这可能是错误的问题或无效的过程。


我已经在单击按钮时隐藏/显示面板,现在我想在下拉列表的值更改后隐藏/显示。


我有两个面板,一个用于基本信息,另一个用于安全信息。在选择下拉值后,我想显示其中一个面板并隐藏第二个面板。


但我不知道如何实现这一目标。


我正在尝试一些基本逻辑并坚持下去。


我做了什么:


using UnityEngine;

using UnityEngine.UI;

using UnityEngine.Events;

using System.Collections;


public class WithrowModalPanel : MonoBehaviour

{


    public Button cancelButton;

    public GameObject modalPanelObject;

    public GameObject modalPanelObjectAdvance;

    public Dropdown myDropdown;


    private static WithrowModalPanel modalPanel;


    public static WithrowModalPanel Instance()

    {

        if (!modalPanel)

        {

            modalPanel = FindObjectOfType(typeof(WithrowModalPanel)) as WithrowModalPanel;

            if (!modalPanel)

                Debug.LogError("There needs to be one active ModalPanel script on a GameObject in your scene.");

        }


        return modalPanel;

    }


    void Update()

    {

        switch (myDropdown.value)

        {

            case 1:

                Debug.Log("Basic panel!");

                modalPanelObject.SetActive(true);

                modalPanelObjectAdvance.SetActive(false);

                break;


            case 2:

                Debug.Log("Advance panel!");

                modalPanelObjectAdvance.SetActive(true);

                modalPanelObject.SetActive(false);

                break;

        }

    }

}

我只是盯着 unity 看,对它的结构并没有太多的了解。


MMMHUHU
浏览 220回答 1
1回答

当年话下

请注意,这Dropdown.value是 0 基索引,因此第一个条目0不是1.&nbsp;我不知道您的完整设置,但我想这是您尝试的主要问题。然后 Dropdowns 有一个事件onValueChanged而不是在Update你应该注册一个监听器private void Start(){&nbsp; &nbsp; // Just to be sure it is always only added once&nbsp; &nbsp; // I have the habit to remove before adding a listener&nbsp; &nbsp; // This is valid even if the listener was not added yet&nbsp; &nbsp; myDropdown.onValueChanged.RemoveListener(HandleValueChanged);&nbsp; &nbsp; myDropdown.onValueChanged.AddListener(HandleValueChanged);}private void OnDestroy(){&nbsp; &nbsp; // To avoid errors also remove listeners as soon as they&nbsp; &nbsp; // are not needed anymore&nbsp; &nbsp; // Otherwise in the case this object is destroyed but the dropdown is not&nbsp; &nbsp; // it would still try to call your listener -> Exception&nbsp; &nbsp; myDropdown.onValueChanged.RemoveListener(HandleValueChanged);}private void HandleValueChanged(int newValue){&nbsp; &nbsp; switch (newValue)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; case 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("Basic panel!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modalPanelObject.SetActive(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modalPanelObjectAdvance.SetActive(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("Advance panel!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modalPanelObjectAdvance.SetActive(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modalPanelObject.SetActive(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}提示:你可以使用通用的FindObjectOfTypemodalPanel = FindObjectOfType<WithrowModalPanel>();
打开App,查看更多内容
随时随地看视频慕课网APP