我刚刚为一些基本的游戏内容启动了一个统一项目。
这可能是错误的问题或无效的过程。
我已经完成了单击按钮时隐藏/显示面板,现在我想在下拉值更改后隐藏/显示。
我有两个面板,一个用于基本信息,另一个用于安全信息。在选择下拉值后,我想显示这些面板之一并隐藏第二个面板。
但我不知道如何实现这一目标。
我正在尝试一些基本逻辑并坚持下去。
我做了什么:
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,对其结构没有太多了解。
请注意,它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>();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句