Search Unity

Feedback Too many hard coded paths in the templates and documentation

Discussion in 'UI Toolkit' started by DrummerB, Aug 14, 2019.

  1. DrummerB

    DrummerB

    Joined:
    Dec 19, 2013
    Posts:
    135
    I've been looking into UIElements recently and I'm a bit concerned with all the hard-coded paths in the templates and documentation I've seen so far.

    When I create a UIElements Editor Window, the uxml file contains hard coded references to the schema files that are placed in the project root. The C# code contains hard coded references to the uxml and uss files.

    Code (csharp):
    1. var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>
    2.                  ("Assets/Editor/MyEditor.uxml");
    Moving either the uxml or uss file to a different location results in a broken UI.

    Most third party editor extensions and tools these days can handle being moved around in the project e.g. into a ThirdParty folder. It would be a pity if UIElements would cause newer editor extensions to rely on a fixed path within the project.

    One workaround for the hard coded paths in the C# file would be to put the uxml and uss files in the special Editor Default Resources folder and using EditorGUIUtility.Load.

    Code (csharp):
    1.  var visualTree = EditorGUIUtility.Load("MyEditor/MyEditor.uxml")
    2.                   as VisualTreeAsset;
    However, this is not ideal, since it prevents self-contained editor tools that live in a single moveable folder.

    It would be nice to have a suggested and documented way to build custom editor tools that can easily be shared and moved around in the project.
     
    Noblauch and mischa2k like this.
  2. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,011
    For C# file, I think you can use Resources.Load() for your uxml, uss file for portability, like this:
    Code (CSharp):
    1. var styleSheet = Resources.Load<StyleSheet>("[your_uss_name]");
    Then put your uss file to Assets/YourLibrary/Editor/Resources/[your_uss_name].uss.
     
    SudoCat likes this.
  3. DrummerB

    DrummerB

    Joined:
    Dec 19, 2013
    Posts:
    135
    Wouldn't that cause those files to be included in the Standalone build?
     
  4. DrummerB

    DrummerB

    Joined:
    Dec 19, 2013
    Posts:
    135
    Hmm, actually it looks like Resources folders inside an Editor folder are stripped from builds.
    The documentation is a bit contradicting here, as the EditorGUIUtility.Load method description doesn't mention Editor/Resources folders.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    You can use the guid of the file:

    Code (csharp):
    1. var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(AssetDatabase.GUIDToAssetPath("GUIDGUIDGUIDGUID"));
    That supports moving the asset, but it's hard to figure out where it is when you're looking at the file.

    I'm looking at having the uxml/uss/cs files next to each other, with the same name. Then you can do something like:

    Code (csharp):
    1. var pathOfThisFile = AssetDatabase.GetAssetPath(MonoScript.FromScriptableObject(this));
    2. var pathToUSS = pathOfThisFile.Substring(0, pathOfThisFile.Length - 2) + ".uss";
    This works as both Editor and EditorWindow inherits from ScritpableObject.
     
  6. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Sorry for reviving this thread, but seems like a less hacky and built-in solution would still be super useful for this.
    Many people seem to be not aware of this thread, and hardcoding all their plugin paths instead :(
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,842
    I've mentioned this in another thread, but you can use the default references of your editor/editor window's monoscript asset to have these references available when they're opened, as they are scriptable objects after all.