Search Unity

Add uxml code to the template via c#

Discussion in 'UIElements' started by Unique-Player, Jun 1, 2019.

  1. Unique-Player

    Unique-Player

    Joined:
    May 6, 2017
    Posts:
    14
    Hi, i have a question. All examples i saw were only creating UI from UXML file which was prewritten.
    I would like to know how we can insert elements into the UI with C# so it will be procedural.
    For examples, when i click a button it will do some calculations and add a new button below the first one, the second click will add another button etc.
    Thank you.
     
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    101
    Hello,

    In this case you would use the C# API to directly create Button objects, exactly like what happens when instantiating a UMXL template. For example:

    Code (CSharp):
    1. rootVisualElement.Add(new Button());
    Thanks