Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by komodor · Feb 21, 2014 at 11:12 AM · gameobjecteditorhierarchy

keep gameObject in hierarchy folded when not selected

i have "folder" gameObject for large group of gameObjects .... any time I click on one of them, the gameObject unfolds and i have to scroll very long way to fold it

is there way t do it by script?, something like on select anything else then "star" unfold this gameObject

Edit: it must be one line script like Hierarchy.Fold(gameObject) in OnDisable function in custom inspector

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by vexe · Mar 06, 2014 at 11:35 AM

This will hopefully cover all your folding needs :)

Using the Collapse method from this answer, I was able to write the following one-liners (as you requested):

 public static class Folds
 {
     [MenuItem("Folds/FoldSelection #&,")]
     public static void FoldSelection()
     {
         Utils.CollapseSelection(false);
     }
     [MenuItem("Folds/CollapseSelection #&.")]
     public static void CollapseSelection()
     {
         Utils.CollapseSelection(true);
     }

     [MenuItem("Folds/FoldAllExceptCurrentSelected %#&,")]
     public static void FoldAllExceptCurrentSelected()
     {
         Utils.CollapseAllExcept(Selection.activeGameObject, false);
     }
     [MenuItem("Folds/CollapseAllExceptCurrentSelected %#&.")]
     public static void CollapseAllExceptCurrentSelected()
     {
         Utils.CollapseAllExcept(Selection.activeGameObject, true);
     }
 }

And then put these in your Utility class (just a static class) (you can tell that mine is called Utils) (`Collapse` goes there as well)

     public static void CollapseSelection(bool value)
     {
         var selection = Selection.GetFiltered(typeof(GameObject), SelectionMode.TopLevel);
         foreach(var go in selection)
             Collapse(go, value);
     }

     public static void CollapseAllExcept(GameObject go, bool value)
     {
         // get the toplevel GOs (whose parents are null) - exclude 'go' - Don't forget to include System.Linq for the Where method to work
         var toplevelGos = Object.FindObjectsOfType<GameObject>().Where(g => g.transform.parent == null && g != go);
         foreach(var g in toplevelGos)
             Collapse(g, value);
         SelectObject(go);
     }

REMARK:

  %: Ctrl on Windows, cmd on OS X
  #: Shift
  &: Alt

However, all this stuff deals with GameObjects in the Hiearchy window, if you wanted to deal with Objects, then this means that we could be selecting something from the Project window (i.e. assets, folders, etc) - so our current Collapse won't work cause it's using the Hierarchy window. But it's very simple to add that functionality to it, modify the Collapse method to:

 public static void Collapse(Object obj, bool collapse, string window = "Hierarchy") // by default, it's the "Hierarchy" window
 {
     // get a reference to the wanted window
     var hierarchy = GetFocusedWindow(window);

     // select our object
     SelectObject(obj);

     // create a new key event (RightArrow for collapsing, LeftArrow for folding)
     var key = new Event { keyCode = collapse ? KeyCode.RightArrow : KeyCode.LeftArrow, type = EventType.keyDown };

     // finally, send the event to the window
     hierarchy.SendEvent(key);
 }

Now you could fold/collapse stuff from the Project window as well:

 Collapse(obj, true, "Project");

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by KillMobil · Feb 21, 2014 at 05:02 PM

You can make an little editor script that creates a menu item with a keyboard shortcut that when it get Hit its selects the parent object if there is one. it will also scroll up to the top! Hope that helps.

Here the code!

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 public class SelectParent  :  Editor{
     
     [MenuItem ("Edit/Select Parent %e")]
     public static void SelectParentFunction () {
         if (Selection.activeGameObject && Selection.activeGameObject.transform.parent)
             Selection.activeGameObject = Selection.activeGameObject.transform.parent.gameObject;
         
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image komodor · Feb 21, 2014 at 05:53 PM 0
Share

thank you for trying to help me but it's not what i seek, i might write it wrong

i need script to fold gameObject in hierarchy

example:

i have gameobject Stars and inside of it i have large group of gameobjects Star 0 to Star N

Stars

___Star 1

___Star 2

___Star 3

when i click on Star 2 i need to have Stars unfolded, when i click on anything else then Star N i need to fold Stars

there is OnDisable function in editor which is perfect place to put the script i need, and it might look like Hierarchy.Fold(gameobject) i just don't know how and if it's possible

avatar image KillMobil · Feb 22, 2014 at 02:15 PM 1
Share

I See! i have a similar problem that i what to solve and it seems that there isn't Hierarchy type class that is accessible and that can accept simple function. How ever in this Post the guy has made a script that focuses the hierarchy window and sends fake keyboard events to rename an object. Bit more than a line of scipt im afraid! I want to look into this soon if i manage to do fix ill post my findings!

avatar image
0

Answer by sandolkakos · Nov 22, 2020 at 09:34 AM

For those who are still trying to find a good solution, based on a lot of research, I've created a utility with these methods:

 - IsExpanded(GameObject go)
 - GetExpandedGameObjects()
 - SetExpanded(GameObject go, bool expand)
 - SetExpandedRecursive(GameObject go, bool expand)

You can find my script here, I hope you guys make good use of it: - https://github.com/sandolkakos/unity-utilities/blob/main/Scripts/Editor/SceneHierarchyUtility.cs

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Modifying several gameobjects with an editor script? 2 Answers

How would you override Editor object dragging? 0 Answers

Show prefab/GO in viewport via script 1 Answer

Detecting moment when GameObiect is created in hierarchy. 1 Answer

Is there anyway to batch renaming via Editor [not on Runtime] multiple game objects in the hierarchy ? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges