Search Unity

Scripting a custom inspector.

Discussion in 'Scripting' started by Digitalos, Apr 27, 2010.

  1. Digitalos

    Digitalos

    Joined:
    Jun 1, 2009
    Posts:
    112
    Hi all,

    I am making some small modifications to the built in Unity inspector for the Transform component. I am aware there is a DrawDefaultInspector() command, however this doesn't draw the default inspector in the same way, it actually looks more like the Debug inspector even when in Normal view mode. So, due to me being a little fastidious with how my things look, I set about just coding the same inspector look into my editor script. However when getting to the rotation, I've noticed it glitches. Sometimes in a static scene when I have just selected an object, I will see the rotation change slightly over time. In addition when using the option to adjust the rotation with the inspector, by click-dragging over one of the axis, the model flips about and generally freaks out, instead of rotating smoothly as with the normal inspector. Scale/Position work fine, so I guess it's something to do with how I set the rotation?

    I also can't seem to set the rotation, for example if I enter 0 in an axis, it will sometimes snap back to something like 0.006835938 or 3.2 (oddly).

    If someone could take a look and let me know how I could better this. I admit I don't actually know -what- the problem is, I was hoping to look at how Unity does it's inspector but that's obviously not possible, such a small thing and it's sucking up hours. :>

    Thanks!
    Digitalos

    The following code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [CustomEditor( typeof( Transform ) )]
    6.  
    7. public class InspectorEnhancements : Editor {
    8.  
    9.     string transText = "";
    10.     Vector3 transPos;
    11.     Vector3 transRot;
    12.     Vector3 transScale;
    13.     Transform currentTarget;
    14.  
    15.     void Awake() {
    16.         currentTarget = (Transform)target;
    17.         transPos = currentTarget.position;
    18.         transRot = currentTarget.localEulerAngles;
    19.         transScale = currentTarget.lossyScale;
    20.     }
    21.  
    22.     public override void OnInspectorGUI() {
    23.         transPos = EditorGUILayout.Vector3Field( "Position", currentTarget.localPosition );
    24.  
    25.         if( GUILayout.Button( "Copy To Clipboard" ) ) {
    26.             transText = transPos.ToString();
    27.             transText = transText.Replace( "(", "" );
    28.             transText = transText.Replace( ")", "" );
    29.  
    30.             EditorGUIUtility.systemCopyBuffer = transText;
    31.         }
    32.  
    33.         transRot = currentTarget.localEulerAngles;
    34.  
    35.         // Wrap
    36.         transRot.x = Mathf.Repeat( transRot.x, 360f );
    37.         transRot.y = Mathf.Repeat( transRot.y, 360f );
    38.         transRot.z = Mathf.Repeat( transRot.z, 360f );
    39.  
    40.         transRot = EditorGUILayout.Vector3Field( "Rotation", transRot );
    41.         transScale = EditorGUILayout.Vector3Field( "Scale", currentTarget.localScale );
    42.  
    43.         currentTarget.localPosition = transPos;
    44.         currentTarget.localRotation = Quaternion.Euler( transRot );
    45.         currentTarget.localScale = transScale;
    46.     }
    47. }
    48.  
     
  2. Digitalos

    Digitalos

    Joined:
    Jun 1, 2009
    Posts:
    112
    Oddly, I've just noticed only the X axis click-drag method causes it to glitch. Though the errors still seem prone when trying to specify set values, such as 10, or 0, or something or even when I click Reset Rotation, sometimes it resets with non 0 values.