Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Hiding default transform handles

Discussion in 'Immediate Mode GUI (IMGUI)' started by Dantus, Apr 23, 2011.

  1. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I would like to hide the default transform handles in a custom editor script. But I have no idea how to do that.
    It should be the same behaviour as in the TreeCreator. If you select Tree Root Node in the inspector, you can move, rotate and scale the whole tree with handles.
    But if e.g. a Branch Group is selected in the inspector, the default handle disappears. How can this be made?
     
    Eristen likes this.
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Just found the solution. Unfortunately undocumented, but it works.
    Code (csharp):
    1. Tools.current = -1;
     
  3. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I was a little bit too fast. This also changes the transform tool. But that's not what I actually want.
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Got it. It is actually not allowed (for good reasons). It is a terrible hack, but just in case someone wants to use it too.

    Code (csharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System;
    4. using System.Reflection;
    5.  
    6. public class ToolsSupport {
    7.  
    8.     public static bool Hidden {
    9.         get {
    10.             Type type = typeof (Tools);
    11.             FieldInfo field = type.GetField ("s_Hidden", BindingFlags.NonPublic | BindingFlags.Static);
    12.             return ((bool) field.GetValue (null));
    13.         }
    14.         set {
    15.             Type type = typeof (Tools);
    16.             FieldInfo field = type.GetField ("s_Hidden", BindingFlags.NonPublic | BindingFlags.Static);
    17.             field.SetValue (null, value);
    18.         }
    19.     }
    20. }
     
    yyysukiii, funkyCoty and IgorAherne like this.
  5. alexfeature

    alexfeature

    Joined:
    Apr 1, 2010
    Posts:
    132
    I love you man!
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It's nice to hear that someone else found it useful!
     
  7. thienhaflash

    thienhaflash

    Joined:
    Jun 16, 2012
    Posts:
    513
    thanks man, this works great, but is there a way to automatically show / hide the handles based on the Object selected ? How can I monitor the active selected Object, so i can make toggling automatically ?

    Ideally I would like to have a static function where i can register object that will hide the handles, so when it's selected, the script got to know and automatically turn off the default handles.

    ================ EDIT ===============

    Ok, I got it working, just use OnEnable() and OnDisable() to check and hide the handles manually.

    thanks man.
     
    Last edited: Jun 21, 2012
  8. lxjk001

    lxjk001

    Joined:
    Feb 13, 2013
    Posts:
    1
    Awesome... Thank you!
     
  9. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You're welcome :)
    It's awesome to know that there are still people who find this useful.
     
  10. nsxdavid

    nsxdavid

    Joined:
    Apr 6, 2009
    Posts:
    476
    The non-hacky and "correct" way of doing this is:

    Code (csharp):
    1.     Tool LastTool = Tool.None;
    2.  
    3.     void OnEnable()
    4.     {
    5.         LastTool = Tools.current;
    6.         Tools.current = Tool.None;
    7.     }
    8.  
    9.     void OnDisable()
    10.     {
    11.         Tools.current = LastTool;
    12.     }
    13.  
    Do this in your editor class. This will remember the tool that was on, then set it to be no tool (thus no handles), and will restore it once they deselect the object.

    Of course while they have it selected they could always select a tool again and then the handles you don't want will reappear. But this is easy to prevent by just adding:

    Code (csharp):
    1. Tools.current = Tool.None;
    somewhere in your OnSceneGUI() method. This forces it off constantly.

    Hiding it with the hack works... but is not the documented way to do it and requires reflection to sneak into private fields of the Tools class. Doing that could break in the future since that's private functionality.


    David
     
    yyysukiii, Mnemotic, owlrazum and 4 others like this.
  11. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You are right, I just realized that I also updated my code after I read about it in some release notes. I changed it and never though about it again and that's the reason why I wasn't aware of my own update anymore. When I posted this code, there was no public api for it, that's why this hack was needed.
    Even if Unity really does a great job, I don't understand why there are so many dark corners in the api and why several apis can not even be accessed, like lots of Shuriken modules.
     
  12. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
    Unfortunately the hacky solution wins over the non-hacky one. In my editor, I am giving the user the ability to move around points using the exact same tools as what is provided for moving around transforms. These points appear when the user selects the game object with this script attached. However, the main gizmo is still visible, and often gets in the way, so I need to be able to disable it, while still allowing the user to switch between move and rotate tools.
     
    closingiris likes this.
  13. Michael-Ryan

    Michael-Ryan

    Joined:
    Apr 10, 2009
    Posts:
    184
    I'm not sure if it was part of the API in Feb 2015, but the "hidden" property does the same thing the hack does (it sets "s_Hidden" internally).

    Code (csharp):
    1. Tools.hidden = false;
     
  14. ifdef_ben

    ifdef_ben

    Joined:
    Apr 18, 2016
    Posts:
    1
    This is great! Now all we need to do is:

    Code (csharp):
    1.  
    2. void OnEnable()
    3. {
    4.     Tools.hidden = true;
    5. }
    6.  
    7. void OnDisable()
    8. {
    9.     Tools.hidden = false;
    10. }
    11.  
     
    Last edited: Apr 28, 2016
    yyysukiii, Mnemotic, Eristen and 13 others like this.
  15. germza123

    germza123

    Joined:
    Feb 26, 2022
    Posts:
    1
    WOOOOOAH just found this, thanks i was really stuck on this one
     
  16. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,367
    There's a Like button to express appreciation without dragging up a 12-year-old dead thread to the top of the list for everyone.