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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Very simple example how to use UIToolkit with code

Discussion in 'UI Toolkit' started by UnityIsFunTime, Dec 1, 2020.

  1. UnityIsFunTime

    UnityIsFunTime

    Joined:
    Apr 30, 2020
    Posts:
    2
    Hi is there a very basic code example for using UIToolkit which shows how to associate a script with the Panel or whatever it needs to attach to? I've done the QuickTool tutorial which seems to be very much what I'm looking for, but it's for editor mode and launches via a menu hook. Any tips are appreciated.

    Thanks,
     
  2. UnityIsFunTime

    UnityIsFunTime

    Joined:
    Apr 30, 2020
    Posts:
    2
    I think I sorted it out. I added a UIDocument to my project and added a MonoBehaviour script to that. In that script I added a private OnEnable like so:
    Code (CSharp):
    1.     private void OnEnable()
    2.     {
    3.         var r = GetComponent<UIDocument>();
    4.         VisualElement ve = new VisualElement();
    5.         ve.style.backgroundColor = new StyleColor(Color.green);
    6.         ve.StretchToParentSize();
    7.         r.rootVisualElement.Add(ve);
    8.     }
    And voila the screen turned green. Not sure it's right but it should let me test things out some more.
     
  3. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    EZaca and LoicLeGrosFrere like this.