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

Question What is Unity.Hierarchy for?

Discussion in 'Scripting' started by PhantasmicDev, Aug 14, 2023.

  1. PhantasmicDev

    PhantasmicDev

    Joined:
    Jul 10, 2013
    Posts:
    35
    I'm looking for a way to create a hierarchy collection that can reflect the child/parent relationships similar to the editor's Hierarchy window, I stumbled across the Unity.Hierarchy namespace but I can't seem to figure out what it's purpose is for. Don't know how recent it was added to the engine since there is no results on google of other people using the API. Any one know what it's for? Is it meant to be expanded on by users or an internal api?
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    If I'm not mistaken, a namespace is just something you can add to group up certain scripts. So just googling "namespace" should give you all relevant information on them.

    But if you mean you want your parent object script to find all children, or have children scripts to know their parent?
    Code (CSharp):
    1. // get parent of entire hierarchy
    2. childObj.transform.root.gameObject
    3.  
    4. // get parent of this object
    5. childObj.transform.parent.gameObject
    6.  
    7. // get child by index
    8. parentObj.transform.GetChild(index).transform.gameObject
    9.  
    10. // get child by name
    11. parentObj.transform.Find("childObjectName").gameObject
    * Note
    transform.Find()
    is not the same as
    GameObject.Find()

    as it goes through the transform hierarchy to search, and you need to declare hierarchy to find deeper children:
    Code (CSharp):
    1. spine3 = transform.Find("body/metarig/spine/spine.001/spine.002/spine.003").gameObject;
     
    Last edited: Aug 14, 2023
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Sorry but that's not what OP asked for. Quite clearly the question is about the purpose of Unity.Hierarchy namespace, which is a legit question.

    @PhantasmicDev Are you sure it's Unity.Hierarchy and not, for example, UnityEngine.UIElements.Hierarchy?

    There are only a handful of (documented) libraries in the Unity namespace, namely:
    Burst
    CodeEditor
    Collections
    Content
    IO
    Jobs
    Loading
    Profiling
    Properties

    Through IDE we also have access to other stuff, but this is mainly because of packages, for example Unity.Mathematics, XR, and VisualScripting.

    But I'm not seeing Hierarchy anywhere.
     
  4. PhantasmicDev

    PhantasmicDev

    Joined:
    Jul 10, 2013
    Posts:
    35
    I was looking into researching how UnityEngine.UIElements.Hierarchy works to begin with but I stumbled on the aforementioned Unity.Hierarchy namespace. Looking through the docs it seems that only 2023.2 has this namespace check it out here

    Looking through the code in Rider it doesn't really look like you can have any of the HierarchyNodes contain any custom data and since it's a struct, it can't be inherited to supply that custom data.

    I couldn't tell what is using this API in the engine but it seems to be a core API.
     
    orionsyndrome likes this.
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Sure, right, okay. Well this seems to be a newer addition, and here's a core module with the explanation.
    It doesn't say much but I believe this is some kind of a general-purpose container, specifically intended for complex hierarchical organization, presumably in the editor. I also believe this is used for the Hierarchy view and the search function etc, and is perhaps planned as a core API for expansions or 3rd-party customization, but there is nothing else I can dig out. I was hoping for an article or announcement of some kind, but the docs are very scarce on this, and Google can't find anything useful (except this thread). Maybe it's too fresh or underdeveloped, maybe it's top secret, maybe people working on this got laid off, who knows? :)

    Another day in the Unity dev world.
    In any case, unless you're able to find (or someone else fills in) more information, this seems to be just an unreliable artifact atm.
     
  6. robertg_unity

    robertg_unity

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    32
    Greetings!

    Since you guys have found the built-in hierarchy core module, I guess it is worth bringing some information about the topic. :)

    The goal behind this new module is to provide a high performance, highly scalable data container tailored for hierarchical data, that is also fully runtime compatible. Those APIs will then serve as a foundation for other projects that we are working on. Please note that the API is not final yet and will most likely change (hopefully not too much).

    Let’s discuss the new APIs. The main point of entry is the Hierarchy class. This object can be used to create and manage hierarchical data. To achieve the highest level of performance, the implementation uses a data oriented approach, where the nodes of the hierarchy are lightweight handles; they do not carry actual data themselves. This is why you will notice the HierarchyNode struct only contains an ID and version.

    When you need to add data per node, you do this using the property APIs on the Hierarchy. Properties have a name and storage strategy. Once you create or get a property, you can then get or set the value of that property for the specified node. The storage strategy allows to control whether or not you want to allocate memory for every node (sparse array strategy), or only allocate memory for nodes that actually have a value set (dense array strategy). Property data is allocated in native and fully aligned, so that we can filter values as fast as possible, which is necessary to support fast searches.

    By default, nodes added to the hierarchy are typeless. That is, they do not have special functionalities. But in case you need to handle different types of nodes within the same hierarchy, you can register what we call a “hierarchy node type handler”. This node handler essentially defines the behavior of your node type. For example, node handlers will allow game objects, entities and other kinds of nodes to all happily live in the same hierarchy.

    The next big class is the HierarchyFlattened, which depends on a Hierarchy instance to feed it data. This is essentially an acceleration structure that is used when you need to transform the hierarchical data into a flat array, which is usually required for displaying efficiently in UI. It is highly optimized to only update when the hierarchy topology changes, and it is able to copy results from a previous update if it detects an entire branch didn’t change. The HierarchyFlattened is an array of HierarchyFlattenedNode, which holds additional information about the structure of the hierarchical data, so you can quickly get offsets of parent, siblings, etc. within the array.

    And finally, there is the HierarchyViewModel class. This one sits on top of a HierarchyFlattened instance, and its main purpose is to filter the flattened array for viewing. For example, it's used to filter out nodes that are collapsed, or to filter nodes based on advanced search queries. Essentially, this is the object from which one would read the data to display in the UI.

    We are planning new features to be built on top of this technology and are using it to improve existing features. We are also considering making a public UI Toolkit control to take advantage of our accelerated container. All these new functionalities will be available at runtime, which means it will all be available to your games if you need to store, manage and display hierarchies. If you have questions about this new API, feel free to ask here. Cheers!