Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Right way to work with UI

Discussion in 'UGUI & TextMesh Pro' started by shlakva, Sep 19, 2021.

  1. shlakva

    shlakva

    Joined:
    Oct 15, 2017
    Posts:
    15
    Hello

    Working with "scene" objects and UI objects lead me to some inconsistency. Work with gameObject is pretty simple: if I need one I can instantiate it just in time I need, find it by component type, position it via transform and so on.

    Now imagine 3 UI parts: game UI(like health or mana bar, coins), success screen and fail screen. Idk right way but I will describe how I do it now. First of all, everything should be enabled in the inspector in order to being found. So, game is started.
    • In "UIManager" script in Start() I search for all UI parts by it's name
    • Disable success and fail screens
    • During game session some of the ui components(images, buttons), wich I also must GameObject.Find("Name"), can be enabled/disabled in order to being seen.
    • Check if player reach the end or die during game session
    • Disable game UI and enable corresponding screen

    In game view I always see canvas with every element on it (again, because they must be enables to being found) and this say to me: "You do wrong thing, man. There is another approach to work with UI, the better one". Please help me with question below.
    1. Do I need to search every UI element by it's name? Is it good practice? Maybe there is some asset or trick wich I can use to simplify searching proccess?
    2. Can I child canvas to another gameObject? Can I have multiple canvases on the scene?
    3. Is there is a trick to find disabled objects?
    Thanks to everyone and sorry for my english;)
     
  2. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    You are talking about UI Toolkit and not Unity UI, right?

    If so, I just have an empty UIDocument at the root of the scene, and I add and remove UXML VisualTreeAssets as needed. Each top-level thing (so probably one for on-screen elements like health/man/etc, one for a pause/settings menu, one for win/loss screens, etc) is its own UXML, and you reference the VisualTreeAsset in some GameObject. Just drag the UXML onto it in the inspector:

    upload_2021-9-20_5-4-44.png

    Then to add them, I have a helper function like this:

    Code (CSharp):
    1.         public static VisualElement addTemplate(this VisualElement parent, VisualTreeAsset asset)
    2.         {
    3.             VisualElement container = parent.contentContainer;
    4.             asset.CloneTree(container, out int index, out int count);
    5.             if(count != 1)
    6.                 throw new Exception(
    7.                     "addTemplate() should only be called with assets that have a single element. " +
    8.                     "If there are multiple elements, use Unity's builtin CloneTree() instead.");
    9.             return container[index];
    10.         }
    To remove, just call RemoveFromHierarchy

    To find an item by name, the Q function can query, but I have helpers for that that throw exceptions in case I mis-spell the name:

    Code (CSharp):
    1. public static VisualElement find(this VisualElement e, string name)
    2.     => e.Q(name) ?? throw new Exception($"Could not find element named {name} under {e.name} ({e.GetType().FullName})");
    3. public static T find<T>(this VisualElement e, string name) where T : VisualElement
    4.     => e.Q<T>(name) ?? throw new Exception($"Could not find element named {name} of type {typeof(T).FullName} under {e.name} ({e.GetType().FullName})");
    5.  
     
  3. shlakva

    shlakva

    Joined:
    Oct 15, 2017
    Posts:
    15
    Thanks for the answer, @burningmime but no, I was talking about old system. I was trying new and its looks great but as I'm still working with legacy its not good for me. I need a solution for old Unity UI system
     
  4. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    Oh there's a different forum for that: https://forum.unity.com/forums/ugui-textmesh-pro.60/
     
    griendeau_unity likes this.
  5. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    707
    I'm moving this thread to the right forum :cool: