编辑现有的编辑检查器?
Edit existing editor inspector?
我想为我的一个脚本编写自定义检查器。我想要更改的只是要弹出的字符串之一的输入法(因此,每次我从预制的字符串列表(如枚举)中选择它时,都不会编写整个字符串)。但问题是它是一个非常长的检查器,有很多变量并且只为这个输入重写所有内容对我来说并没有点击。我对默认检查器如何显示所有字段感到非常满意,期待我想要更改的这一个字符串。有没有一种方法可以在不自己重写整个检查器的情况下做到这一点?
与其像您提到的那样实施一个有点矫枉过正的全新 Inspector,不如只为该字段使用 PropertyDrawer。
这在很大程度上取决于您从何处获得可用选项。一般来说,我会例如实施自定义 PropertyAttribute
[AttributeUsage(AttributeTargets.Field)]
public class SelectionAttribute : PropertyAttribute
{
public int Index;
}
自定义 PropertyDrawer
并将此抽屉放入名为 Editor
的文件夹中
[CustomPropertyDrawer(typeof(SelectionAttribute))]
public class SelectionAttributDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
// The 6 comes from extra spacing between the fields (2px each)
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
if (property.propertyType != SerializedPropertyType.String)
{
EditorGUI.HelpBox(position, "Only works with string", MessageType.Error);
return;
}
//TODO: somewhere you have to get the options from
var options = new[] { "A", "B", "C", "D", "E" };
if (options == null || options.Length < 1)
{
EditorGUI.HelpBox(position, "No options available", MessageType.Error);
return;
}
var selectionAttribute = (SelectionAttribute)attribute;
EditorGUI.BeginProperty(position, label, property);
EditorGUI.BeginChangeCheck();
selectionAttribute.Index = EditorGUI.Popup(position, options.ToList().IndexOf(property.stringValue), options);
if (EditorGUI.EndChangeCheck())
{
property.stringValue = options[selectionAttribute.Index];
}
EditorGUI.EndProperty();
}
}
现在您可以在多个 类 项目中使用它,例如
public class Example : MonoBehaviour
{
[Selection] public string MySelection;
private void Start()
{
Debug.Log(MySelection);
}
}
无需自定义编辑器
我想为我的一个脚本编写自定义检查器。我想要更改的只是要弹出的字符串之一的输入法(因此,每次我从预制的字符串列表(如枚举)中选择它时,都不会编写整个字符串)。但问题是它是一个非常长的检查器,有很多变量并且只为这个输入重写所有内容对我来说并没有点击。我对默认检查器如何显示所有字段感到非常满意,期待我想要更改的这一个字符串。有没有一种方法可以在不自己重写整个检查器的情况下做到这一点?
与其像您提到的那样实施一个有点矫枉过正的全新 Inspector,不如只为该字段使用 PropertyDrawer。
这在很大程度上取决于您从何处获得可用选项。一般来说,我会例如实施自定义 PropertyAttribute
[AttributeUsage(AttributeTargets.Field)]
public class SelectionAttribute : PropertyAttribute
{
public int Index;
}
自定义 PropertyDrawer
并将此抽屉放入名为 Editor
[CustomPropertyDrawer(typeof(SelectionAttribute))]
public class SelectionAttributDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
// The 6 comes from extra spacing between the fields (2px each)
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
if (property.propertyType != SerializedPropertyType.String)
{
EditorGUI.HelpBox(position, "Only works with string", MessageType.Error);
return;
}
//TODO: somewhere you have to get the options from
var options = new[] { "A", "B", "C", "D", "E" };
if (options == null || options.Length < 1)
{
EditorGUI.HelpBox(position, "No options available", MessageType.Error);
return;
}
var selectionAttribute = (SelectionAttribute)attribute;
EditorGUI.BeginProperty(position, label, property);
EditorGUI.BeginChangeCheck();
selectionAttribute.Index = EditorGUI.Popup(position, options.ToList().IndexOf(property.stringValue), options);
if (EditorGUI.EndChangeCheck())
{
property.stringValue = options[selectionAttribute.Index];
}
EditorGUI.EndProperty();
}
}
现在您可以在多个 类 项目中使用它,例如
public class Example : MonoBehaviour
{
[Selection] public string MySelection;
private void Start()
{
Debug.Log(MySelection);
}
}
无需自定义编辑器