UnitySerializedReferenceUI/Editor/DefaultUI/SerializeReferenceInspectorButton.cs

41 lines
1.7 KiB
C#
Raw Permalink Normal View History

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
2022-12-28 14:06:42 +01:00
namespace Textus.SerializeReferenceUI.Editor
{
2022-12-28 14:06:42 +01:00
public static class SerializeReferenceInspectorButton
{
2022-12-28 14:06:42 +01:00
/// Must be drawn before DefaultProperty in order to receive input
public static void DrawSelectionButtonForManagedReference(this SerializedProperty property, Rect position, IEnumerable<Func<Type, bool>> filters = null)
{
Rect buttonPosition = position;
buttonPosition.x += EditorGUIUtility.labelWidth + 1 * EditorGUIUtility.standardVerticalSpacing;
buttonPosition.width = position.width - EditorGUIUtility.labelWidth - 1 * EditorGUIUtility.standardVerticalSpacing;
buttonPosition.height = EditorGUIUtility.singleLineHeight;
2022-12-28 14:06:42 +01:00
Color storedColor = GUI.backgroundColor;
int storedIndent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
2022-12-28 14:06:42 +01:00
(string AssemblyName, string ClassName) = ManagedReferenceUtility.GetSplitNamesFromTypename(property.managedReferenceFullTypename);
2022-12-28 14:06:42 +01:00
bool isNull = string.IsNullOrEmpty(ClassName);
GUI.backgroundColor = isNull ? Color.red : storedColor;
string className = isNull ? "Null (Assign)" : ClassName;
string assemblyName = AssemblyName;
2022-12-28 14:12:18 +01:00
if (EditorGUI.DropdownButton(buttonPosition, new GUIContent(className, className + " ( " + assemblyName + " )"), FocusType.Keyboard))
2022-12-28 14:06:42 +01:00
{
property.ShowContextMenuForManagedReference(buttonPosition, filters);
}
GUI.backgroundColor = storedColor;
EditorGUI.indentLevel = storedIndent;
}
2022-12-28 14:06:42 +01:00
}
}
#endif