Search Unity

Runtime companion script, how to hook up?

Discussion in 'UI Toolkit' started by unitydungeonmas, Sep 6, 2020.

  1. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    How do I hook up a runtime UI Document to a companion script? I just want use C# to dynamically change some labels, but do not know how to get the root of a UI Document already in the scene to drag. VisualTreeAssets and VisualElements cannot be draggedd
     
  2. Refeas

    Refeas

    Joined:
    Nov 8, 2016
    Posts:
    192
    You can access the root via
    rootVisualElement
    property of the
    UIDocument
    component. BTW, if you make a public VisualTreeAsset property, it actually displays the reference slot in the inspector and you can drag in UXML file there.
     
    unitydungeonmas likes this.
  3. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    But the UXML file is not the instance that's created already right? Or am I misunderstanding this?

    Also I cannot find UIDocument in C# as it is a sealed class, am I doing this wrong, thanks
     
  4. Refeas

    Refeas

    Joined:
    Nov 8, 2016
    Posts:
    192
    The instance that is already created is accessible from the UIDocument's rootVisualElement property as I already described.

    Regarding the UI Document - have you already attached the UI Document (and UI Toolkit event system) component to a GameObject in your scene? If you have, then you can attach your companion script to the same GameObject and just call
    GetComponent<UIDocument>().rootVisualElement
    from your script to get the instance of the UXML you attached to the UIDocument.
     
    unitydungeonmas likes this.
  5. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    I get "The type or namespace name 'UIDocument' could not be found (are you missing a using directive or an assembly reference?) " - what is the namespace?
     
  6. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    I'm on UIBuilder 1.0.0-preview.6 UI Toolkit 1.0.0-preview.8
     
  7. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    I opened UIToolkitUnityRoyaleRuntimeDemo and it is as you described, any idea on how to add this to an existing project? seems like some prereq is missing
     
  8. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    I tried adding this to my asmdef: "UnityEngine.UIElementsGameObjectsModule.dll", but still no good
     
  9. Refeas

    Refeas

    Joined:
    Nov 8, 2016
    Posts:
    192
    The namespace is simply UnityEngine.UIElements. Simple using directive and adding that to asmdef should do the trick.
     
    unitydungeonmas likes this.
  10. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    Yes, seems like that does not do it for me, though it works for a newly created project, I might have some conflicting setup
     
  11. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    I added the following to the asmdef and it works, thanks:

    "UnityEngine.UIElementsModule",
    "UnityEngine.UIElementsGameObjectsModule",
    "UnityEngine"
     
  12. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    700
    Hi @unitydungeonmas , you don't usually need to add anything to the asmdef, only add the using clause for UnityEngine.UIElements should do it, but I don't know your particular setup so if it's working for you it's all that matters!

    As others have answered, all you have to do is GetComponent<UIDocument>() on the Game Object that you know contains the UIDocument and through it's rootVisualElement you have access to the full hierarchy. If your companion is on the same Game Object as the UIDocument just call GetComponent directly.

    Let us know if you have more questions!
     
    unitydungeonmas likes this.
  13. chris_gamedev

    chris_gamedev

    Joined:
    Feb 10, 2018
    Posts:
    34
    If you need to access the
    UIDocument
    component from a script on a different GameObject what is the best approach?

    I could create and assign a tag to my GameObject that contains the
    UIDocument
    and then do
    GameObject.FindWithTag("tag")
    and then call GetComponent on that but I don't know if there's a more sensible approach?
     
  14. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    700
    Using tags is not the most effective way to code, but if there's no other way it's an option. I would personally suggest you keep a reference to the GameObject (or the UIDocument component directly) on the GameObject you need that access, and then drag the reference to assign on the scene. If this is not possible then you'd have to find it through calling FindObjectOfType which is also not something you want to do all the time as it can be a heavy call to make.

    Hope this helps!
     
    chris_gamedev likes this.
  15. chris_gamedev

    chris_gamedev

    Joined:
    Feb 10, 2018
    Posts:
    34
    Fair comments. Thanks for the tips!
     
    JuliaP_Unity likes this.
  16. AdamBebko

    AdamBebko

    Joined:
    Apr 8, 2016
    Posts:
    168
    When trying to
    Code (CSharp):
    1. GetComponent<UIDocument>();
    I'm also getting or namespace not found error from a script inside an assembly I created.

    I had to reference the assembly UnityEngine.UIElementsGameObjectsModule.

    it sounds like this isn't intended?

    Also, it's very strange that you install a package called UIToolkit, but have to make using statements and assembly references for something called UIElements. A bit counter-intuitive.

    I'm on 2020.2, up to date as of today with UI builder and UItoolkit up to date as well..

    Edit: Also, just wanted to say that my using statements didn't need to be changed:
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    This is same as before. It was the assembly reference that fixed the error
     
    dyamanoha_ likes this.
  17. dyamanoha_

    dyamanoha_

    Joined:
    Mar 17, 2013
    Posts:
    87
    I also had to manually add `UnityEngine.UIElementsGameObjectsModule` to the project assemblies list
     
    shazwar and acandael like this.