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

Selecting in scene: select as if clicked the object in hierarchy

Discussion in 'Immediate Mode GUI (IMGUI)' started by FeastSC2, Apr 28, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Code (CSharp):
    1. Selection.activeInstanceID = someGameObject.GetInstanceID();
    2. Selection.activeGameObject = someGameObject;
    This doesn't select a gameObject like it would when you click in the hierarchy (it's greyed out in the hierarchy)
    Like this:
    http://i.imgur.com/RJ3Hso0.png

    But when you click an object in the hierarchy as opposed to the scene view, it becomes blue and you can rename with F2.
    Like this:
    http://i.imgur.com/DDLxBkw.png

    I would like to make it so that when I select something in the scene it becomes blue and not grey. Through code. And thus I would be able to rename with the hotkey F2 my scene selected objects.
     
  2. sisus_co

    sisus_co

    Joined:
    May 1, 2011
    Posts:
    14
    It looks like you just need to bring focus to the HierarchyWindow

    Code (CSharp):
    1. EditorApplication.ExecuteMenuItem("Window/Hierarchy")
     
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Thanks, that works perfectly! You're the man, Naster ;)
     
  4. sisus_co

    sisus_co

    Joined:
    May 1, 2011
    Posts:
    14
    Glad I could help :)
     
  5. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Hey Naster, I've been trying to switch from 2d to 3d view when I'm in the Hierarchy window.
    Unity has a shortcut for this with Alpha2 in the scene view but this one does not work when the focus is on the Hierarchy window.

    Code (CSharp):
    1.  
    2. if (e.keyCode.Equals(KeyCode.Alpha1))
    3.                 {
    4.                     if (!e.alt)
    5.                         if (!e.control)
    6.                             if (!e.shift)
    7.                             {
    8.                                 //SceneView. = SceneView.lastActiveSceneView;
    9.                                 //SceneView. = SceneView.currentDrawingSceneView;
    10.                                 EditorApplication.ExecuteMenuItem("Window/Scene");
    11.                                 SceneView.lastActiveSceneView.in2DMode =
    12.                                     !SceneView.currentDrawingSceneView.in2DMode;
    13.                             }
    14.                 }
    15.  
    16.  
    So I tried it to create it this way, but it gives me an error when I have an object selected because the SceneView.lastActiveSceneView is not set to an instance of an object yet.
    However, when I click the hotkey a 2nd time it works fine.
    I'm trying to make it work in one go, do you have any idea how?

    It might be due to the fact that
    Code (CSharp):
    1. EditorApplication.ExecuteMenuItem
    takes time to execute? I'm not sure.

    PS: The reason I have this problem in the first place is because I'm bringing the focus to the Hierarchy Window from the scene view when manipulating objects to have them highlighted in blue, I'd like to keep that.
    This works pretty well, but when I first launch Unity I must always click at least once in the Hierarchy window to make the items be highlighted in blue when I'm moving them in the Scene. Do you know why ?

    Hopefully this message is comprehensible!
    Jeremy.
     
    Last edited: May 6, 2017