有不可选选项的Dropdown
前几日群里的小伙伴问道,如何让Dropdown下的选项不可选(变成灰色)。
最初直接想的是把Toggle
的Interactable
设置成false
就好了。
可是发现Unity的Dropdown没法获取物体。
好吧,我们可以用GetChild()
方法进去找子物体的Toggle
,可是太麻烦了。
所以干脆就变成做个编辑器扩展。
完成后的设置
- 其中Default Disable Options,就是在创建列表的时候默认为不可选的
Index
;
完成后的截图
在创建时,用Unity的Dropdown创建,然后删除Unity的Dropdown,再添加这个脚本。
Dropdown代码:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[AddComponentMenu("UI/DR DropDown")]
[RequireComponent(typeof(Image))]
public class DRDropDown : Dropdown
{
/// <summary>
/// 在添加Item时,不可用的Index,如果是动态添加的不同列表,可以在打开之前这个里改变值
/// </summary>
[SerializeField]
[HideInInspector]
private List<int> m_DefaultDisables = new List<int>();
public List<int> disables
{
get { return m_DefaultDisables; }
}
/// <summary>