Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Loading UI from UXML in Unity DLL

Discussion in 'UI Toolkit' started by sheralex1998, Mar 22, 2023.

  1. sheralex1998

    sheralex1998

    Joined:
    Apr 4, 2020
    Posts:
    5
    I want to make one DLL file with all code and assets for small Editor tool.
    In this case I need some UI for custom Editor window, and I am going to make it with UI Toolkit.
    So, I have 3 files: C# script, .uxml file and .uss file. And I need to do something like this for making editor window:

    Code (CSharp):
    1. // Each editor window contains a root VisualElement object
    2.         VisualElement root = rootVisualElement;
    3.  
    4. // Import UXML
    5.         var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Scripts/UI/AutoGenSkybox.uxml");
    6.         VisualElement labelFromUXML = visualTree.Instantiate();
    7.         root.Add(labelFromUXML);
    The problem is that the AssetDatabase will not work with the uxml file as embeded DLL resource. So I am using this:

    Code (CSharp):
    1. Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
    How can I convert this data to VisualTreeAsset?
     
  2. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    567
    We rely on a scripted importer to process the .uxml and .uss into a usable asset. I never tried including assets in a dll like you are doing, but I am pretty certain the asset database won't know about theses. You can possibly save the imported asset as a .asset, and load that file from the dll, but this will make the asset specific to that version of the editor.

    Are you able to use an imported asset (like a texture or a scriptable object) form the dll?
     
  3. sheralex1998

    sheralex1998

    Joined:
    Apr 4, 2020
    Posts:
    5
    I think that I can include only text or byte[] assets in to DLL what C# BinaryFormatter or Stream reader will understand. The main goal of DLL - to be one separate file which I can just drop in Assets folder without any another folders and files. So I assumed there was an option to convert UXML text to VisualElement. But, as I see, this may not be very reliable.

    I will be able to achieve my goal by writing C# code for the UI instead of saving it in UXML file. Just wanted to make sure that this is the best option.

    Thank You!