static IEnumerable <Type> GetConstructableTypes(Type type) { if (TypeConstruction.CanBeConstructed(type)) { yield return(type); } foreach (var t in UnityEditor.TypeCache.GetTypesDerivedFrom(type).Where(TypeConstruction.CanBeConstructed)) { yield return(t); } }
static IEnumerable <Type> GetConstructableTypes <TType>() { if (TypeConstruction.CanBeConstructed <TType>()) { yield return(typeof(TType)); } foreach (var type in UnityEditor.TypeCache.GetTypesDerivedFrom <TType>().Where(TypeConstruction.CanBeConstructed)) { yield return(type); } }
static void RegisterInspectorType(Dictionary <Type, List <Type> > typeMap, Type interfaceType, Type inspectorType) { var inspectorInterface = inspectorType.GetInterface(interfaceType.FullName); if (null == inspectorInterface || inspectorType.IsAbstract || inspectorType.ContainsGenericParameters) { return; } var genericArguments = inspectorInterface.GetGenericArguments(); var componentType = genericArguments[0]; if (!TypeConstruction.HasParameterLessConstructor(inspectorType)) { Debug.LogError($"Could not create a custom inspector for type `{inspectorType.Name}`: no default or empty constructor found."); } if (!typeMap.TryGetValue(componentType, out var list)) { typeMap[componentType] = list = new List <Type>(); } list.Add(inspectorType); }
public NullElement(string name) { styleSheets.Add(AssetDatabase.LoadAssetAtPath <StyleSheet>("Packages/com.unity.properties/Editor Default Resources/uss/null-element.uss")); AddToClassList("unity-properties-null"); var label = new Label(name); label.AddToClassList("unity-properties-null--label"); Add(label); var constructibleTypes = TypeConstruction.GetAllConstructibleTypes <TType>(); if (constructibleTypes.Count > 0) { var button = new Button() { text = "New Instance", bindingPath = name }; // Multiple choices are available if (constructibleTypes.Count > 1) { button.clickable.clicked += () => { var menu = new GenericMenu(); foreach (var type in TypeConstruction.GetAllConstructibleTypes <TType>()) { var t = type; menu.AddItem(new GUIContent(t.Name), false, () => { var newValue = TypeConstruction.Construct <TType>(type); var binding = GetFirstAncestorOfType <PropertyElement>(); binding.SetValue(button, newValue); binding.Refresh(true); }); } menu.DropDown(button.worldBound); }; } else { button.clickable.clicked += () => { var type = constructibleTypes[0]; var newValue = type == typeof(TType) ? TypeConstruction.Construct <TType>() : TypeConstruction.Construct <TType>(type); var binding = GetFirstAncestorOfType <PropertyElement>(); binding.SetValue(button, newValue); binding.Refresh(true); }; } button.AddToClassList("unity-properties-null--button"); Add(button); } else { var value = new Label("Cannot create new Instance"); value.AddToClassList("unity-properties-null--value"); Add(value); } }