Search Unity

Question How a Plugin Determine If It's Maintained by UPM

Discussion in 'Package Manager' started by zhirui_li, Jul 30, 2021.

  1. zhirui_li

    zhirui_li

    Joined:
    Mar 31, 2021
    Posts:
    4
    Hi everyone:

    I'm developing a Unity plugin for editor mode. It's file layout like this:

    Code (CSharp):
    1. - Assets
    2.   - MyPlugin
    3.     - Resources
    4.       - Image.jpg
    5.     - Scripts
    6.       - MyScript.cs
    7. - Documents
    As the layout shows, the plugin needs to load some resources at runtime, for example a image. But if I load the image from
    Application.dataPath/Assets/Resources/Image.jpg
    , it can only success when developing the plugin. When it is released to git and downloaded by others and maintained by UPM, the file will not found at runtime. That's because data is not under Assets folder but under Packages folder. But if I replace the path to something like
    Application.dataPath/Packages/com.mypackage.myplugin/Resources/Image.jpg
    , then the image can be loaded when the pacakge is maintained by UPM. But when I develop this plugin, it can't load the image (because the image is not under Packages folder).

    So what's the best way to solve this problem?
     
    Last edited: Jul 30, 2021
  2. firstuser

    firstuser

    Joined:
    May 5, 2016
    Posts:
    147
    Maybe use a scripting symbol?

    #IF MY_PLUGIN_DEVELOPMENT_MODE

    path = this path..

    #ELSE

    path = path for everyone else
     
  3. If you're trying to save/load editor-time data, you should not use the
    Resources
    folder for that. The content of the resources folders get into the builds.
    You should not have
    Resources
    folder inside your packages. Resources folders are for
    Assets/...
    and loading from there you should not use path in runtime (and you should avoid duplicate names and such).
    See the examples here: https://docs.unity3d.com/ScriptReference/Resources.Load.html they are relative to the
    Resources
    folder.

    And finally: use Addressables instead.
     
  4. zhirui_li

    zhirui_li

    Joined:
    Mar 31, 2021
    Posts:
    4
    I think it's hard to automaticlly change the marco value based on envirenment...
     
  5. zhirui_li

    zhirui_li

    Joined:
    Mar 31, 2021
    Posts:
    4
    Resources under
    Resources
    folder is just an example case. Another situation is that we may want to load .uxml file, both when we are editing it as a Unity project and after releasing it as a package.
     
    Last edited: Aug 5, 2021
  6. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,066
    Use the GUID of an asset in the root of your plugin folder to get the path from the asset database. This also allows you to place the plugin folder anywhere under Assets, works if you put your plugin in a subdirectory of a Package and doesn't depend on the package name.

    Code (CSharp):
    1. const string AssetGuid = "1234...";
    2.  
    3. public static string GetPluginPath()
    4. {
    5.     var path = AssetDatabase.GUIDToAssetPath(AssetGuid);
    6.     return Path.GetDirectoryName(path);
    7. }
     
  7. zhirui_li

    zhirui_li

    Joined:
    Mar 31, 2021
    Posts:
    4
    But what if I release the plugin by UPM? In that situation, the asset is not under Asset folder but under Packages folder.
     
  8. firstuser

    firstuser

    Joined:
    May 5, 2016
    Posts:
    147
    What's hard? Just add a checkbox somewhere that adds or removes your flag or something? Seems like an easy way to quickly move forward.
     
  9. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,066
    All assets are managed by the asset database, those in Assets as well as those in Packages. It'll just return the "Packages/com.yourpackage/Path/to/your/plugin" path if the target file is part of a package. As I wrote, It'll work if the file is in the Assets folder and it'll work if it's inside a package (any kind of package – local, remote or git).
     
    zhirui_li likes this.