Skip to content

Instantly share code, notes, and snippets.

@Naphier
Last active November 14, 2015 13:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Naphier/f128192f295b9177722a to your computer and use it in GitHub Desktop.
Save Naphier/f128192f295b9177722a to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor; // Required for any editor functions
using UnityEditor.AnimatedValues; // Required for fade out groups
[CustomEditor(typeof(Transform))] // This makes it override the standard inspector for Transforms
[CanEditMultipleObjects] // This allows us to select mutiple components and edit them simultaneously
// Inherits from Editor class so we can override OnInspectorGUI()
// Also note that Editor scripts should always be placed in an "Editor" folder.
public class MyTransformComponent : Editor
{
private Transform _transform; // This is the currently selected transform
private AnimBool m_showExtraFields; // This is used to hold the state of the fade group for "Special Operations"
void OnEnable()
{
// First we set up our AnimBool as false to have the fade group collapsed
m_showExtraFields = new AnimBool(false);
// Then we add a listener to repaint the inspector GUI every time the AnimBool's value changes.
m_showExtraFields.valueChanged.AddListener(Repaint);
}
public override void OnInspectorGUI()
{
// We need this reference to our transform for all OnInspectorGUI sub methods
_transform = (Transform)target;
StandardTransformInspector();
QuaternionInspector();
}
// This is basically the same as Unity's default transform inspector
private void StandardTransformInspector()
{
// Notice how we set the local position (and other variables) from the field that displays
// the variable.
_transform.localPosition = EditorGUILayout.Vector3Field("Position", _transform.localPosition);
// Here I labeled the rotation as "Euler Rotation" since this inspector will also
// be showing the Quaternion rotation
_transform.localEulerAngles = EditorGUILayout.Vector3Field("Euler Rotation", _transform.localEulerAngles);
_transform.localScale = EditorGUILayout.Vector3Field("Scale", _transform.localScale);
// Notice how I'm using the local values for each of these fields as that is what Unity
// displays by default. I could imagine it being very useful to also see the global values.
}
private bool quaternionFoldout = false; // This bool keeps track of the open/closed state of the quaternion foldout
private void QuaternionInspector()
{
// Additional element to view and also edit the Quaternion rotation values.
// You really should only modify quaternion values if you understand them well.
// However, this script give us the chance to modify them and gain some understanding to their
// relationship to regular Euler (0-360deg) angles.
// Note how we're feeding in the quaternionFoldOut bool as well as setting it as it changes.
quaternionFoldout = EditorGUILayout.Foldout(quaternionFoldout, "Quaternion Rotation: " + _transform.localRotation.ToString("F3"));
// If the fold out should be open then we should display the editable quaternion values.
if (quaternionFoldout)
{
// First call this the start monitoring the field for changes.
EditorGUI.BeginChangeCheck();
// Display an editable quaternion field with a little warning.
Vector4 qRotation = EditorGUILayout.Vector4Field("Be careful!", QuaternionToVector4(_transform.localRotation));
// If the value did change then record the change for undo-ing and apply the new value.
if (EditorGUI.EndChangeCheck())
{
// We record the _tranform object's state before the change so the change can be undone.
// The string is arbitrary, but maybe someday Unity will make this undo history visible.
Undo.RecordObject(_transform, "modify quaternion rotation on " + _transform.name);
//Finally apply the change
_transform.localRotation = ConvertToQuaternion(qRotation);
}
}
}
// This is just a simple method to allow us to view and edit the quaternion as a vector4.
private Vector4 QuaternionToVector4(Quaternion q)
{
return new Vector4(q.x, q.y, q.z, q.w);
}
// This turns the vector4 back into a quaterion when we're done with it.
private Quaternion ConvertToQuaternion(Vector4 v4)
{
return new Quaternion(v4.x, v4.y, v4.z, v4.w);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment