Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How To: Add Buttons To The Transform Component In Editor Inspector

Discussion in 'Scripting' started by keenanwoodall, Jul 20, 2014.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I really like the button the ngui added to all objects transform component. Next to the position was a button that would zero the position, there were similar buttons for the rotation and scale. I've added that functionality to a MenuItem, but I'd like to add it to each objects transform in the scene. How would I go about doing that?
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    I've always wanted to do this too. But I never really had the chance to explore it. I always thought if you made a script that extended the transform component theme wrote an editor script that might work.. But I'm not entirely sure
     
  3. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    That sounds like that might work! Any idea on how to go about that?
     
  4. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
  5. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
  6. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Thanks guys, I'll check out all that stuff and tell you what I come up with. Btw, I'm doing this in javascript
     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    You can get a .NET reflection software and check in the UnityEditor.dll how the standard Transform editor is made.

    .NET Reflector has a 30 days free trial.
     
  8. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    ok I might look into that if I can't figure it out
     
  9. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    So I know I need to use the Editor.target so that I know which object is selected, but how would I go about doing that? Heres my weak code as I'm trying to figure it out. This does not work at all :) Also, I just made the function public and override because I saw that on an example of an OnInspectorGUI script, but I don't really know what it does. I only ever make a normal function without any "modifiers" typed before them.
    Code (js):
    1.  
    2. class TransformInspector extends Editor
    3. {
    4.     public override function  OnInspectorGUI ()
    5.     {
    6.         var t : Transform = Editor.target.transform;
    7.     }
    8. }
    Here's the error
    Assets/Editor/KeenansCoolTools/KeenansCoolTransformButtons.js(6,44): BCE0020: An instance of type 'UnityEditor.Editor' is required to access non static member 'target'.
     
  10. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Not sure how you do it in unityscript but you need to add the namespace

    In c# we'd do it as using UnityEditor at the very top of the class
     
  11. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Don't do "Editor.target", just do "target.transform", it's a member variable.
     
  12. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Ok, so I've made a bunch of progress. I found this page,
    https://code.google.com/p/pixelplac...ripts/TransformInspector.cs?spec=svn149&r=149
    It helped me figure out a bunch of stuff. Everything works except for "t.localEulerAngles = FixifNaN(eulerAngles)." I'm not sure what the problem is. Here's my current code:
    Code (js):
    1.  
    2. @CustomEditor (typeof (Transform))
    3. class TransformInspector extends Editor
    4. {
    5.     //Make a function that replaces the Transform Component in the Inpector
    6.     public override function  OnInspectorGUI ()
    7.     {
    8.         //target is the object you have selected in the editor. We are making a variable that is equal to the target's transform
    9.         var t : Transform = target.transform;
    10.         //Below we are creating new buttons on the Transform component
    11.         if (GUILayout.Button("Reset Position"))
    12.         {
    13.             //Here we are telling Unity that the stuff that's being done by this button (changing the transform) needs to be undo-able
    14.             Undo.RegisterUndo (t, "Reset Position " + t.name);
    15.             t.transform.position = Vector3.zero;
    16.         }
    17.         if (GUILayout.Button("Reset Rotation"))
    18.         {
    19.             Undo.RegisterUndo (t, "Reset Rotation " + t.name);
    20.             t.transform.rotation = Quaternion.identity;
    21.         }
    22.         if (GUILayout.Button("Reset Scale"))
    23.         {
    24.             Undo.RegisterUndo (t, "Reset Scale " + t.name);
    25.             t.transform.localScale = Vector3 (1,1,1);
    26.         }
    27.         //Because we are overriding the transform components GUI in the Inspector, we have to recreate it below
    28.         EditorGUIUtility.LookLikeControls();
    29.         EditorGUI.indentLevel = 0;
    30.         //Here we are recreating the part of the Transform component that gave you acces to the position.
    31.         var position : Vector3 = EditorGUILayout.Vector3Field("Position", t.localPosition);
    32.         //Here we are recreating the part of the Transform component that gave you acces to the rotation.
    33.         var rotation : Vector3 = EditorGUILayout.Vector3Field("Rotation", t.localEulerAngles);
    34.         //Here we are recreating the part of the Transform component that gave you acces to the rotation.
    35.         var scale : Vector3 = EditorGUILayout.Vector3Field("Scale", t.localScale);
    36.         EditorGUIUtility.LookLikeInspector();
    37.         if (GUI.changed)
    38.         {
    39.             Undo.RegisterUndo (t, "Transform Change");
    40.             t.localPosition = FixIfNaN(position);
    41.             t.localEulerAngles = FixIfNaN(eulerAngles);
    42.             t.localScale = FixIfNaN(scale);
    43.         }
    44.     }
    45.     private function FixIfNaN(v : Vector3)
    46.     {
    47.         if (v.x == null)
    48.         {
    49.             v.x = 0;
    50.         }
    51.         if (v.y == null)
    52.         {
    53.             v.y = 0;
    54.         }
    55.         if (v.z == null)
    56.         {
    57.             v.z = 0;
    58.         }
    59.         return v;
    60.     }
    61. }
    The error is:
    Assets/Editor/KeenansCoolTools/KeenansCoolTransformButtons.js(40,43): BCE0005: Unknown identifier: 'eulerAngles'.

    The error is on line 41 on this page
     
  13. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I just saw that where I made a private function called FixIfNaN was wrong. According to the reference page I was looking at, FixIfNaN is a variable..but also a function? I dont understand that at all! Ill paste the code from the site
    Code (cs):
    1.  
    2. // Alternative version, with redundant code removed
    3. using UnityEngine;
    4. using UnityEditor;
    5. using System.Collections;
    6. [CustomEditor(typeof(Transform))]
    7. public class TransformInspector:Editor
    8. {
    9.     public override voidOnInspectorGUI()
    10.     {
    11.         Transform t =(Transform)target;
    12.              
    13.                 if(GUILayout.Button("Reset Transforms")){
    14.                         Undo.RegisterUndo(t,"Reset Transforms "+ t.name);
    15.                         t.transform.position =Vector3.zero;
    16.                         t.transform.rotation =Quaternion.identity;
    17.                         t.transform.localScale =Vector3.one;
    18.                 }
    19.              
    20.         // Replicate the standard transform inspector gui
    21.         EditorGUIUtility.LookLikeControls();
    22.         EditorGUI.indentLevel =0;
    23.         Vector3 position =EditorGUILayout.Vector3Field("Position", t.localPosition);
    24.         Vector3 eulerAngles =EditorGUILayout.Vector3Field("Rotation", t.localEulerAngles);
    25.         Vector3 scale =EditorGUILayout.Vector3Field("Scale", t.localScale);
    26.         EditorGUIUtility.LookLikeInspector();
    27.         if(GUI.changed)
    28.         {
    29.             Undo.RegisterUndo(t,"Transform Change");
    30.             t.localPosition =FixIfNaN(position);
    31.             t.localEulerAngles =FixIfNaN(eulerAngles);
    32.             t.localScale =FixIfNaN(scale);
    33.         }
    34.     }
    35.     private Vector3 FixIfNaN(Vector3 v)
    36.     {
    37.         if(float.IsNaN(v.x))
    38.         {
    39.             v.x =0;
    40.         }
    41.         if(float.IsNaN(v.y))
    42.         {
    43.             v.y =0;
    44.         }
    45.         if(float.IsNaN(v.z))
    46.         {
    47.             v.z =0;
    48.         }
    49.         return v;
    50.     }
    51. }
     
    Qazi_MinorBugs likes this.
  14. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I figured it out!! It was a stupid mistake, I needed to feed in rotation instead of eulerAngles! Now it works fine
     
    Qazi_MinorBugs likes this.