Search Unity

[Unity 5] Adding Inline Buttons to Transform

Discussion in 'Editor & General Support' started by Studio_Akiba, Aug 3, 2020.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I'm running Unity 5 (5.6.7f1) so can't use the package I'll be linking to as it is for 2018, and I'd like to build my own to understand how it works better anyway.
    I'm looking to add buttons to reset (zero), copy and paste transforms (both local and global) of selected objects.
    Example:


    What I'm trying to replicate is what is seen here (second image).
    Their code seems very complex and I'm looking for something far more lightweight I can actually understand.

    So far, I've been able to draw the custom foldouts I'm using elsewhere in my custom tools, but haven't been able to successfully replicate this without losing significant functionality.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5. using System;
    6. using System.Reflection;
    7. using UnityEngine.SocialPlatforms;
    8.  
    9. [CustomEditor(typeof(Transform))]
    10. [CanEditMultipleObjects]
    11. public class CustomTransformExtension : Editor
    12. {
    13.     //Built-in editor
    14.     Editor defaultEditor;
    15.  
    16.     Transform transform;
    17.  
    18.     bool localSpace = true;
    19.     bool worldSpace = false;
    20.  
    21.     void OnEnable()
    22.     {
    23.         //Called when inspector is created
    24.         defaultEditor = Editor.CreateEditor(targets, Type.GetType("UnityEditor.TransformInspector, UnityEditor"));
    25.         transform = target as Transform;
    26.     }
    27.  
    28.     void OnDisable()
    29.     {
    30.         //Prevents memory leakage
    31.         MethodInfo disableMethod = defaultEditor.GetType().GetMethod("OnDisable", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
    32.  
    33.         if(disableMethod != null)
    34.         {
    35.             disableMethod.Invoke(defaultEditor, null);
    36.         }
    37.  
    38.         DestroyImmediate(defaultEditor);
    39.     }
    40.  
    41.     public override void OnInspectorGUI()
    42.     {
    43.         //If Arcane Transform Extension is enabled
    44.         if(EditorPrefs.GetBool("arcaneTransformExtension") == true)
    45.         {
    46.             EditorGUILayout.Space();
    47.             localSpace = Foldout("Local Space", localSpace);
    48.             if (localSpace)
    49.             {
    50.                 defaultEditor.OnInspectorGUI();
    51.             }
    52.  
    53.             EditorGUILayout.Space();
    54.  
    55.             worldSpace = Foldout("World Space", worldSpace);
    56.             if (worldSpace)
    57.             {
    58.  
    59.             }
    60.         }
    61.         else //Use default Editor
    62.         {
    63.             defaultEditor.OnInspectorGUI();
    64.         }
    65.     }
    66.  
    67.     public static bool Foldout(string title, bool display)
    68.     {
    69.         var style = new GUIStyle("ShurikenModuleTitle");
    70.         style.font = new GUIStyle(EditorStyles.label).font;
    71.         style.border = new RectOffset(15, 7, 4, 4);
    72.         style.fixedHeight = 22;
    73.         style.contentOffset = new Vector2(20f, -2f);
    74.  
    75.         var rect = GUILayoutUtility.GetRect(16f, 22f, style);
    76.         GUI.Box(rect, title, style);
    77.  
    78.         var e = Event.current;
    79.  
    80.         var toggleRect = new Rect(rect.x + 4f, rect.y + 2f, 13f, 13f);
    81.         if (e.type == EventType.Repaint)
    82.         {
    83.             EditorStyles.foldout.Draw(toggleRect, false, false, display, false);
    84.         }
    85.  
    86.         if (e.type == EventType.MouseDown && rect.Contains(e.mousePosition))
    87.         {
    88.             display = !display;
    89.             e.Use();
    90.         }
    91.  
    92.         return display;
    93.     }
    94. }
    95.  
    Does anyone have any idea how I can achieve something similar without breaking everything?
     
  2. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    507
    Hello @ATLAS-INTERACTIVE,

    Maybe you can take a look at these good tutorials that are for Unity 5: https://catlikecoding.com/unity/tutorials/old-tutorials/

    More specifically "Custom Data" and "Custom List" have code to customize the Editor window to place "Inline buttons" as you want.

    More likely the tutorials will not do exactly what you want, but you'll be able to get closer to the feature that you're aiming to achieve.


    Good luck with it!