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. Dismiss Notice

UI Toolkit at Package Level?

Discussion in 'UI Toolkit' started by phreezie, Mar 13, 2022.

  1. phreezie

    phreezie

    Joined:
    Oct 3, 2019
    Posts:
    119
    This might be a dumb question, but I'm about to start with UI Toolkit, and I'm adding it to a package, not a project. So I create a new Editor Window, which creates the three files (.cs, .uss and .uxml), and following the tutorial I want to link the USS to my window, which results in "Asset at path {cut-off path} is not a StyleSheet".

    Creating a new USS results in "Could not find a part of the path {cut-off path}" in the console. It seems to me that your tooling is not set up for supporting package-level UI. Is there a workaround or something I'm missing?
     
  2. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    515
    What is your editor version? I will try it on my side to see if I end up with a similar error
     
  3. phreezie

    phreezie

    Joined:
    Oct 3, 2019
    Posts:
    119
    I'm on 2021.2.14f.

    I played around some more, and when you create a new UXML through the editor, the generated .cs actually loads the style sheet via code. Here, the package file path works. So I'm not sure what the UI Builder is supposed to do actually when you add style sheets there.
     
  4. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    515
    I am assuming you are doing an editor window but I could be wrong, please let me know if this is the case.

    I started testing in 2022.2, and I think someone changed the default example when you do a right click->create->UI toolkit ->editor window.

    I think you need an asmdef for editor assemblies in the package (instead of just a folder called editor). Don't quote me on this, but I have the reflex of creating one anyway before creating the window.

    The resulting CreateGUI() does not refer to the style anymore,
    upload_2022-3-15_16-38-40.png


    The resulting UXML does refer to the style :

    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <engine:UXML
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.     xmlns:engine="UnityEngine.UIElements"
    5.     xmlns:editor="UnityEditor.UIElements"
    6.     xsi:noNamespaceSchemaLocation="../../../UIElementsSchema/UIElements.xsd"
    7. >
    8.     <Style src="project://database/Packages/com.test.mypackage/editor/usstest.uss?fileID=7433441132597879392&amp;guid=6f4f2b74561dd414ebd820524047ee96&amp;type=3#usstest" />
    9.     <engine:Label text="Hello World! From UXML" />
    10.     <engine:Label class="custom-label" text="Hello World! With Style" />
    11.  
    12. </engine:UXML>
    And the style from the uss is applied to the third label:
    upload_2022-3-15_16-41-46.png



    So, if I understood your problem correctly, it is not reproducible in 2022.2a8 so far. I didn't have 2021.2.14 installed so I figured I should start with something I already had on hands.

    I will do another pass with the builder later on today if everything goes well.


    Can you post the resulting style tag from the uxml you are having issues with? Like this one:
    <Style src="project://database/Packages/com.test.mypackage/editor/usstest.uss?fileID=7433441132597879392&amp;guid=6f4f2b74561dd414ebd820524047ee96&amp;type=3#usstest" />


    FYI the first part is the asset path that is used by default, and we fall back to the GUI if the asset was moved.
     

    Attached Files:

  5. phreezie

    phreezie

    Joined:
    Oct 3, 2019
    Posts:
    119
    Yes, it's an editor window. And the result is functional. It's just that in the tutorial, it says, quote

    In StyleSheet, click Add Existing USS and select DragAndDropWindow.uss​

    Which doesn't work for an USS located in a package, but isn't necessary, because the stylesheet is already assigned through the generated .cs.

    I'll try to add the stylesheet through the markup as you suggested later.

    In any case, thanks for your quick help, really appreciated!
     
  6. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    515
    Just confirmed with the UI builder team that it is not currently working with package path and that they have a task for this, but no ETA for now.
     
  7. phreezie

    phreezie

    Joined:
    Oct 3, 2019
    Posts:
    119
    Alright, thanks for checking!
     
  8. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    934
    A workaround is to add it from C#, but that won't show it in the editor.

    Hope they backport the fix if it ever comes -.-
     
  9. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    Update for this? I would like to refer to a package path when adding a uss file
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    Had this issue too, though with visual tree assets, though found a simple work around that should work with style sheets assets too:
    • Serialize a VisualTreeAsset/StyleSheet field in your editor window
    • Select your script asset, and assign a reference through the default value fields
    • The values will be assigned when the window is open, so you can apply them via C# easily
     
    alexanderameye likes this.
  11. TenaciousDan

    TenaciousDan

    Joined:
    Jul 11, 2019
    Posts:
    22
    I had a similar problem where I was developing an asset library and then using it as a package within another project. I just check to see if the package's virtual path is valid using AssetDatabase API and if it is I simply switch the base path. See example below.

    Add this script into an Editor folder:

    Code (CSharp):
    1. public static class AssetPaths
    2. {
    3.     public static string packageRootDir
    4.     {
    5.         get
    6.         {
    7.             // regular assets path
    8.             string rootDir = "Assets/whatever/";
    9.  
    10.             // IMPORTANT: this is linked to the "name" property of the package manifest json file
    11.             string packagesDir = "Packages/com.whatever/";
    12.  
    13.             if (AssetDatabase.IsValidFolder(packagesDir))
    14.                 rootDir = packagesDir;
    15.  
    16.             return rootDir;
    17.         }
    18.     }
    19. }
    Then you can use the correct path like so:

    Code (CSharp):
    1. protected void CreateGUI()
    2. {
    3.     // build GUI from UXML
    4.     VisualTreeAsset visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
    5.         AssetPaths.packageRootDir + "Editor/MyCustomEditorWindow.uxml"
    6.     );
    7.     visualTree.CloneTree(rootVisualElement);
    8.  
    9.     // add stylesheet
    10.     StyleSheet styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(
    11.         AssetPaths.packageRootDir + "Editor/MyCustomEditorWindow.uss"
    12.     );
    13.    
    14.     if (styleSheet != null)
    15.         rootVisualElement.styleSheets.Add(styleSheet);
    16. }
    This does not affect UIBuilder, you can still use it like normal.
     
    Last edited: Jul 19, 2023
    spiney199 likes this.
  12. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    364
    This Bug is over a year old and still not fixed...
    It is really annoying that you cannot use stylesheet files with the ui builder when working in a package...
     
  13. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    364
    It works with the UI builder if you add the style file in the uxml via text editor and with relative path to the uxml.
    Code (CSharp):
    1. <ui:UXML ...>
    2.     <Style src="styles.uss" />
    3.     <ui:VisualElement name="root" />
    4. </ui:UXML>
    like this when they are in the same folder
     
    Last edited: Jul 20, 2023
  14. CitrioN

    CitrioN

    Joined:
    Oct 6, 2016
    Posts:
    66
    Is there any ETA on this now? Been 1 1/2 years and with the recommended workflow of using packages this feature is pretty much necessary.
     
  15. mvriel

    mvriel

    Joined:
    Mar 17, 2015
    Posts:
    13
    I would like to +1 this issue, in our project we are building a framework of packages where each package features its own UI assets; but currently we cannot bring it together in the project due to this issue
     
  16. TenaciousDan

    TenaciousDan

    Joined:
    Jul 11, 2019
    Posts:
    22
    @mvriel Have you tried my suggestion above? I am able to use the UI Builder to design Editor UI in a package and I can import it just fine in other projects.