Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Feedback Too many hard coded paths in the templates and documentation

Discussion in 'UIElements' started by DrummerB, Aug 14, 2019 at 8:11 PM.

  1. DrummerB

    DrummerB

    Joined:
    Dec 19, 2013
    Posts:
    40
    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.
     
  2. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    274
    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.
     
  3. DrummerB

    DrummerB

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

    DrummerB

    Joined:
    Dec 19, 2013
    Posts:
    40
    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:
    4,096
    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.