Gameplay Ingredients是一组用于 Unity 游戏的运行时和编辑器工具:一组脚本的集合,可在制作游戏和原型时简化简单的任务。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

60 行
1.8 KiB

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace GameplayIngredients.Editor
{
[CustomPropertyDrawer(typeof(TypeDropDownAttribute))]
public class TypeDropDownPropertyDrawer : PropertyDrawer
{
Dictionary<string, List<string>> m_AssignableTypeNames;
Type type;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if(type == null)
type = ((TypeDropDownAttribute)attribute).m_BaseType;
CacheType(type);
string TypeName = type.FullName;
int index = m_AssignableTypeNames[TypeName].IndexOf(property.stringValue);
EditorGUI.BeginChangeCheck();
int newVal = EditorGUI.Popup(position, index, m_AssignableTypeNames[TypeName].ToArray());
if(EditorGUI.EndChangeCheck() && index != newVal)
{
property.stringValue = m_AssignableTypeNames[TypeName][newVal];
}
}
void CacheType(Type baseType)
{
if (m_AssignableTypeNames == null)
{
m_AssignableTypeNames = new Dictionary<string, List<string>>();
string key = baseType.FullName;
if (!m_AssignableTypeNames.ContainsKey(key))
m_AssignableTypeNames.Add(key, new List<string>());
foreach(var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach(var type in assembly.GetTypes())
{
if(baseType.IsAssignableFrom(type) && !type.IsAbstract)
{
m_AssignableTypeNames[key].Add(type.Name);
}
}
}
}
}
}
}