void ShowInline(Rect position, SerializedProperty property, GUIContent label, ReferenceTypeSelectorAttribute typeSelector, object targetObject) { var labalPosition = new Rect(position.x, position.y, EditorGUIUtility.labelWidth, position.height); var buttonPosition = new Rect(position.x + EditorGUIUtility.labelWidth + 2, position.y, position.width - EditorGUIUtility.labelWidth - 2, position.height); EditorGUI.LabelField(labalPosition, label); ShowInstanceMenu(buttonPosition, property, typeSelector, targetObject); }
void ShowFoldout(Rect position, SerializedProperty property, GUIContent label, ReferenceTypeSelectorAttribute typeSelector, object targetObject) { EditorGUI.PropertyField(position, property, label, true); if (property.isExpanded) { var buttonPosition = new Rect(position.x, position.y + position.height - EditorGUIUtility.singleLineHeight, position.width, EditorGUIUtility.singleLineHeight); using (new EditorGUI.IndentLevelScope(1)) { buttonPosition = EditorGUI.IndentedRect(buttonPosition); ShowInstanceMenu(buttonPosition, property, typeSelector, targetObject); } } }
void ShowInstanceMenu(Rect position, SerializedProperty property, ReferenceTypeSelectorAttribute typeSelector, object targetObject) { var baseType = typeSelector.baseType; var buttonText = (targetObject == null) ? "null" : targetObject.GetType().Name; if (GUI.Button(position, buttonText)) { var context = new GenericMenu(); context.AddItem(new GUIContent("Set null"), false, () => { property.managedReferenceValue = null; property.serializedObject.ApplyModifiedProperties(); }); if (typeSelector.isAbstractBase) { context.AddDisabledItem(new GUIContent(baseType.Name)); } else { context.AddItem(new GUIContent(baseType.Name), false, () => { property.managedReferenceValue = Activator.CreateInstance(baseType); property.serializedObject.ApplyModifiedProperties(); }); } context.AddSeparator(""); foreach (var subType in typeSelector.subTypes) { context.AddItem(new GUIContent(subType.Name), false, () => { property.managedReferenceValue = Activator.CreateInstance(subType); property.serializedObject.ApplyModifiedProperties(); }); } var pos = new Rect(position.x, position.y + EditorGUIUtility.singleLineHeight, 0, 0); context.DropDown(pos); } }