Unity3d 如何使Vector3字段的行为与CustomEditor中的Transform字段类似
在下面的gif中,您可以看到Unity3d 如何使Vector3字段的行为与CustomEditor中的Transform字段类似,unity3d,custom-controls,unity3d-editor,unity-editor,Unity3d,Custom Controls,Unity3d Editor,Unity Editor,在下面的gif中,您可以看到Vector3字段在Transform检查器和myMonoBehaviors检查器中的行为方式之间的差异 Transform甚至是一个CustomEditor我也使用EditorGUILayout.Vector3Field()编写的 [自定义编辑器(typeof(Transform),true)] [CaneditMultipleObject] 公共类AdvancedTransformEditor:编辑器 { //Unity的内置编辑器 私人编辑(默认编辑),; 私
Vector3
字段在Transform
检查器和myMonoBehavior
s检查器中的行为方式之间的差异
Transform
甚至是一个CustomEditor
我也使用EditorGUILayout.Vector3Field()
编写的
[自定义编辑器(typeof(Transform),true)]
[CaneditMultipleObject]
公共类AdvancedTransformEditor:编辑器
{
//Unity的内置编辑器
私人编辑(默认编辑),;
私有变换_变换;
私有void OnEnable()
{
//创建此检查器时,还要创建内置检查器
_defaultEditor=CreateEditor(targets,Type.GetType(“UnityEditor.TransformInspector,UnityEditor”);
_变换=作为变换的目标;
}
私有无效不可撤销()
{
//调用OnDisable时,应该销毁我们创建的默认编辑器以避免内存泄漏。
//另外,确保调用任何必需的方法,如OnDisable
var disableMethod=_defaultEditor.GetType().GetMethod(“OnDisable”,BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if(disableMethod!=null)disableMethod.Invoke(_defaultEditor,null);
立即销毁(_defaultEditor);
}
公共覆盖无效OnInspectorGUI()
{
EditorGUILayout.LabelField(“本地空间”,EditorStyles.boldLabel);
_defaultEditor.OnInspectorGUI();
serializedObject.Update();
//显示世界空间变换
EditorGUILayout.Space();
EditorGUILayout.LabelField(“世界空间”,EditorStyles.boldLabel);
_transform.position=EditorGUILayout.Vector3Field(“位置”,_transform.position);
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.Vector3Field(“旋转”,_transform.euleranges);
EditorGUILayout.Vector3Field(“缩放”,_transform.lossyScale);
EditorGUI.EndDisabledGroup();
serializedObject.ApplyModifiedProperties();
}
}
它仅在具有\u defaultEditor.OnInspectorGUI()时工作因此Unity的原始编辑器中用于转换的部分必须做一些不同的事情
当我尝试在任何其他CustomEditor
中为monobhavior
//没有自定义编辑器
公共课示例:单一行为
{
公共向量3例;
}
及
//自定义编辑器的宽度
公共类示例MinWidth:MonoBehavior
{
公共向量3例;
}
[CustomEditor(typeof(ExampleMinWidth))]
公共类示例MinWidthEditor:编辑器
{
私有财产的例子;
私有void OnEnable()
{
示例=serializedObject.FindProperty(“exmaple”);
}
公共覆盖无效OnInspectorGUI()
{
serializedObject.Update();
example.vector3Value=EditorGUILayout.Vector3Field(“example”,example.vector3Value);
//我尝试了这两种方法,也只是简单地使用了PropertyField
//EditorGUILayout.PropertyField(示例);
serializedObject.ApplyModifiedProperties();
}
}
或者跳过行\u defaultEditor.OnInspectorGUI()在AdvancedTransformEditor
中,将Vector3
字段折叠一定宽度
我如何才能获得与转换组件相同的字段行为-不是折叠而是保持在同一行上?
更新
- 我用
GUILayout.MinWidth()
尝试了它,但这并没有改变任何事情
- 正如我建议的那样,我也试过了
example.vector3Value=EditorGUILayout.Vector3Field(“example”,example.vector3Value,GUILayout.ExpandHeight(false));
(同样适用于属性字段()
),但这并没有改变任何事情
- 为了尝试,我还使用了
ExpandWidth(false)
。。。结果不是很令人满意:D
- 我甚至尝试了
GUILayout.MaxHeight(EditorGUIUtility.singleLineHeight)
,但这使得该字段仍然折叠,但“bload”/透支到下面的字段中
Unity3d在GitHub上提供了它的功能,您可以在那里看到组件的实现
TransformInspector.cs:
/ Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
namespace UnityEditor
{
[CustomEditor(typeof(Transform))]
[CanEditMultipleObjects]
internal class TransformInspector : Editor
{
SerializedProperty m_Position;
SerializedProperty m_Scale;
TransformRotationGUI m_RotationGUI;
class Contents
{
public GUIContent positionContent = EditorGUIUtility.TrTextContent("Position", "The local position of this GameObject relative to the parent.");
public GUIContent scaleContent = EditorGUIUtility.TrTextContent("Scale", "The local scaling of this GameObject relative to the parent.");
public string floatingPointWarning = LocalizationDatabase.GetLocalizedString("Due to floating-point precision limitations, it is recommended to bring the world coordinates of the GameObject within a smaller range.");
}
static Contents s_Contents;
public void OnEnable()
{
m_Position = serializedObject.FindProperty("m_LocalPosition");
m_Scale = serializedObject.FindProperty("m_LocalScale");
if (m_RotationGUI == null)
m_RotationGUI = new TransformRotationGUI();
m_RotationGUI.OnEnable(serializedObject.FindProperty("m_LocalRotation"), EditorGUIUtility.TrTextContent("Rotation", "The local rotation of this GameObject relative to the parent."));
}
public override void OnInspectorGUI()
{
if (s_Contents == null)
s_Contents = new Contents();
if (!EditorGUIUtility.wideMode)
{
EditorGUIUtility.wideMode = true;
EditorGUIUtility.labelWidth = EditorGUIUtility.currentViewWidth - 212;
}
serializedObject.Update();
Inspector3D();
// Warning if global position is too large for floating point errors.
// SanitizeBounds function doesn't even support values beyond 100000
Transform t = target as Transform;
Vector3 pos = t.position;
if (Mathf.Abs(pos.x) > 100000 || Mathf.Abs(pos.y) > 100000 || Mathf.Abs(pos.z) > 100000)
EditorGUILayout.HelpBox(s_Contents.floatingPointWarning, MessageType.Warning);
serializedObject.ApplyModifiedProperties();
}
private void Inspector3D()
{
EditorGUILayout.PropertyField(m_Position, s_Contents.positionContent);
m_RotationGUI.RotationField();
EditorGUILayout.PropertyField(m_Scale, s_Contents.scaleContent);
}
}
}
我在表格中找到了相关的行
if(!EditorGUIUtility.wideMode)
{
EditorGUIUtility.wideMode=true;
EditorGUIUtility.labelWidth=EditorGUIUtility.currentViewWidth-212;
}
- 执行两项操作:返回当前编辑器是否处于widemode下,以及设置此组件/下一行的编辑器是否应像在widemode下一样运行。因此,他们只是强迫他们的场只“处于”宽模式
- 在此之后,有必要使用一个“固定”字段,即在widemode中3
Vectror3
字段将采用的宽度减小(Unity used 212)