【问题标题】:Hide/show panel using dropdown Unity使用下拉 Unity 隐藏/显示面板
【发布时间】:2019-08-14 03:52:23
【问题描述】:

我刚刚为一些基本游戏内容启动了一个统一项目。

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

我已经完成了按钮点击的隐藏/显示面板,现在我想在下拉值更改后隐藏/显示。

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

但我不知道如何实现。

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

我做了什么:

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,对它的结构没有太多想法。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    请注意,Dropdown.value 是从 0 开始索引的,因此第一个条目是 0 而不是 1。我不知道您的完整设置,但我想这是您尝试的主要问题。

    然后 Dropdowns 有一个事件 onValueChanged 而不是在 Update 中进行,您应该注册一个监听器

    private void Start()
    {
        // Just to be sure it is always only added once
        // I have the habit to remove before adding a listener
        // This is valid even if the listener was not added yet
        myDropdown.onValueChanged.RemoveListener(HandleValueChanged);
        myDropdown.onValueChanged.AddListener(HandleValueChanged);
    }
    
    private void OnDestroy()
    {
        // To avoid errors also remove listeners as soon as they
        // are not needed anymore
        // Otherwise in the case this object is destroyed but the dropdown is not
        // it would still try to call your listener -> Exception
        myDropdown.onValueChanged.RemoveListener(HandleValueChanged);
    }
    
    private void HandleValueChanged(int newValue)
    {
        switch (newValue)
        {
            case 0:
                Debug.Log("Basic panel!");
                modalPanelObject.SetActive(true);
                modalPanelObjectAdvance.SetActive(false);
                break;
    
            case 1:
                Debug.Log("Advance panel!");
                modalPanelObjectAdvance.SetActive(true);
                modalPanelObject.SetActive(false);
                break;
        }
    }
    

    提示:你可以使用FindObjectOfType的泛型

    modalPanel = FindObjectOfType<WithrowModalPanel>();
    

    【讨论】:

    • 如何使用modalPanel = FindObjectOfType();就像我在上面的文件中使用的一样?
    • myDropdown.onValueChanged.RemoveListener(HandleValueChanged);Argument 1: cannot convert from 'method group' to 'UnityAction&lt;int&gt;'上显示错误
    • 哦,显然它需要一个 int 参数现在应该可以工作了 ;) 只在智能手机上,所以我无法测试它
    猜你喜欢
    • 2013-01-23
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多