gpt4 book ai didi

unity-game-engine - 禁用 Unity 下拉菜单中的选项

转载 作者:行者123 更新时间:2023-12-02 18:22:14 24 4
gpt4 key购买 nike

我需要从 Unity 的下拉菜单中禁用 1(或 2)个下拉选项。 enter image description here下拉菜单不应重新填充
下拉菜单中不应有任何删除/停用选项

任何人都知道如何做到这一点。 ?

最佳答案

类似于this answer但方法略有不同。

另一个答案使用硬编码的 toggle.name == "Item 1: Option B" 来比较按钮。我宁愿使用中央和基于索引的系统:

将此组件放在DropDown

[RequireComponent(typeof(Dropdown))]
[DisallowMultipleComponent]
public class DropDownController : MonoBehaviour, IPointerClickHandler
{
[Tooltip("Indexes that should be ignored. Indexes are 0 based.")]
public List<int> indexesToDisable = new List<int>();

private Dropdown _dropdown;

private void Awake()
{
_dropdown = GetComponent<Dropdown>();
}

public void OnPointerClick(PointerEventData eventData)
{
var dropDownList = GetComponentInChildren<Canvas>();
if (!dropDownList) return;

// If the dropdown was opened find the options toggles
var toogles = dropDownList.GetComponentsInChildren<Toggle>(true);

// the first item will always be a template item from the dropdown we have to ignore
// so we start at one and all options indexes have to be 1 based
for (var i = 1; i < toogles.Length; i++)
{
// disable buttons if their 0-based index is in indexesToDisable
// the first item will always be a template item from the dropdown
// so in order to still have 0 based indexes for the options here we use i-1
toogles[i].interactable = !indexesToDisable.Contains(i - 1);
}
}

// Anytime change a value by index
public void EnableOption(int index, bool enable)
{
if (index < 1 || index > _dropdown.options.Count)
{
Debug.LogWarning("Index out of range -> ignored!", this);
return;
}

if (enable)
{
// remove index from disabled list
if (indexesToDisable.Contains(index)) indexesToDisable.Remove(index);
}
else
{
// add index to disabled list
if (!indexesToDisable.Contains(index)) indexesToDisable.Add(index);
}

var dropDownList = GetComponentInChildren<Canvas>();

// If this returns null than the Dropdown was closed
if (!dropDownList) return;

// If the dropdown was opened find the options toggles
var toogles = dropDownList.GetComponentsInChildren<Toggle>(true);
toogles[index].interactable = enable;
}

// Anytime change a value by string label
public void EnableOption(string label, bool enable)
{
var index = _dropdown.options.FindIndex(o => string.Equals(o.text, label));

// We need a 1-based index
EnableOption(index + 1, enable);
}
}

在检查器中配置

enter image description here

或通过脚本,例如

dropDownReference.GetComponent<DropDownController>().EnableOption("Option B", false);
<小时/>

enter image description here

关于unity-game-engine - 禁用 Unity 下拉菜单中的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55297626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com