从检查器调试菜单或Unity编辑器中的检查器中自由编辑四元数 [英] Freely edit quaternion from inspector debug menu or inspector in Unity editor
问题描述
我有一个四元数,我想将其设置为Unity编辑器中对象的transform属性. 检查器"选项卡中的调试模式显示四元数的当前值,并且该值是可编辑的,但是如果不立即将它们覆盖,则无法一一设置字段.看起来Unity在每次编辑后都会尝试对其进行规范化/规范化.如何禁用此功能?还有其他方法可以在统一编辑器中输入四元数的值吗?
I have a quaternion that I would like to set as a transform property on an object in the Unity editor. The debug mode in the inspector tab shows the current value of the quaternion and it is editable, however I can't set fields one by one without them being overwritten immediately. It looks like Unity tries to properize/normalize it after each edit. How can I disable this? Is there any way other way to just type in the value of a quaternion in the unity editor?
将其转换为欧拉角"不是对此问题的可接受答案.
"Convert it to euler angles" is not an acceptable answer to this question.
推荐答案
据我所知,不能禁用该功能.
As far as I know, that functionality can't be disabled.
但是,如果可以使用常规检查器,那么定制检查器可以在这里为您提供帮助.
However, if using the normal inspector is acceptable, a custom inspector can help you here.
首先,一个用于收集局部旋转的类.包含一个布尔值,在您对其进行编辑之前要取消选中该布尔值,以阻止值被更新/覆盖. Vector4
在此处用于允许方便的PropertyField
出现在自定义编辑器中:
First, a class to collect the local rotation. Includes a boolean to uncheck before you edit it, to stop the values from being updated/overwritten. Vector4
is used here to allow the convenient PropertyField
to appear in the custom editor:
using UnityEngine;
public class Demo : MonoBehaviour
{
public Vector4 rotation;
public bool updateFromTransform = true;
void Update()
{
// for testing purposes
// transform.Rotate(0.3f * Vector3.up);
if (updateFromTransform)
{
rotation.x = transform.localRotation.x;
rotation.y = transform.localRotation.y;
rotation.z = transform.localRotation.z;
rotation.w = transform.localRotation.w;
}
}
}
还有一个自定义编辑器,其中包括一个按钮,用于将对Vector4
的更改应用于本地旋转:
And a custom editor including a button to apply changes to the Vector4
to the local rotation:
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Demo))]
public class DemoEditor : Editor
SerializedProperty rotation
SerializedProperty updateFromTransform;
private void OnEnable()
{
rotation = serializedObject.FindProperty("rotation");
updateFromTransform = serializedObject.FindProperty("updateFromTransform");
}
public override void OnInspectorGUI()
{
Demo d = (Demo)target;
serializedObject.Update();
EditorGUILayout.PropertyField(updateFromTransform);
EditorGUILayout.PropertyField(rotation);
serializedObject.ApplyModifiedProperties();
if (GUILayout.Button("Apply as local rotation"))
{
d.updateFromTransform = true;
d.transform.localRotation =
new Quaternion(d.rotation.x, d.rotation.y, d.rotation.z, d.rotation.w);
}
}
}
这篇关于从检查器调试菜单或Unity编辑器中的检查器中自由编辑四元数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!