在下面的 gif 中,您可以看到Vector3
字段在 Inspector ofTransform
和 my MonoBehaviour
s 中的行为方式之间的区别。
Transform
甚至是CustomEditor
我也使用EditorGUILayout.Vector3Field()
.
[CustomEditor(typeof(Transform), true)]
[CanEditMultipleObjects]
public class AdvancedTransformEditor : Editor
{
//Unity's built-in editor
private Editor _defaultEditor;
private Transform _transform;
private void OnEnable()
{
//When this inspector is created, also create the built-in inspector
_defaultEditor = CreateEditor(targets, Type.GetType("UnityEditor.TransformInspector, UnityEditor"));
_transform = target as Transform;
}
private void OnDisable()
{
//When OnDisable is called, the default editor we created should be destroyed to avoid memory leakage.
//Also, make sure to call any required methods like OnDisable
var disableMethod = _defaultEditor.GetType().GetMethod("OnDisable", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (disableMethod != null) disableMethod.Invoke(_defaultEditor, null);
DestroyImmediate(_defaultEditor);
}
public override void OnInspectorGUI()
{
EditorGUILayout.LabelField("Local Space", EditorStyles.boldLabel);
_defaultEditor.OnInspectorGUI();
serializedObject.Update();
//Show World Space Transform
EditorGUILayout.Space();
EditorGUILayout.LabelField("World Space", EditorStyles.boldLabel);
_transform.position = EditorGUILayout.Vector3Field("Position", _transform.position);
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.Vector3Field("Rotation", _transform.eulerAngles);
EditorGUILayout.Vector3Field("Scale", _transform.lossyScale);
EditorGUI.EndDisabledGroup();
serializedObject.ApplyModifiedProperties();
}
}
它只有_defaultEditor.OnInspectorGUI();
在 Unity 的原始编辑器中为Transform
组件做一些不同的事情时才有效。
当我尝试在其他任何地方做同样的事情CustomEditor
时MonoBehaviour
// without a CustomEditor
public class Example : MonoBehaviour
{
public Vector3 example;
}
和
// Width the custom editor
public class ExampleMinWidth : MonoBehaviour
{
public Vector3 example;
}
[CustomEditor(typeof(ExampleMinWidth))]
public class ExampleMinWidthEditor : Editor
{
private SerializedProperty example;
private void OnEnable()
{
example = serializedObject.FindProperty("exmaple");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
example.vector3Value = EditorGUILayout.Vector3Field("Example", example.vector3Value);
// I tried both also simply using a PropertyField
//EditorGUILayout.PropertyField(example);
serializedObject.ApplyModifiedProperties();
}
}
或跳过字段_defaultEditor.OnInspectorGUI();
中AdvancedTransformEditor
的行Vector3
会折叠一定的检查器宽度。
如何使我的字段与Transform
组件具有相同的行为 - 不折叠但保持在同一行?
更新
我试过了,
GUILayout.MinWidth()
但这并没有改变任何东西。正如建议的那样,我也尝试过
example.vector3Value = EditorGUILayout.Vector3Field("Example", example.vector3Value, GUILayout.ExpandHeight(false));
(也适用于
PropertyField()
),但这并没有改变任何东西。只是为了尝试,我也做了
ExpandWidth(false)
......结果不是很令人愉快:D我什至尝试过
GUILayout.MaxHeight(EditorGUIUtility.singleLineHeight)
,但这使得该字段仍然折叠但“bload”/overdraw 进入下面的字段。