Search Unity

How to load assets at runtime from within a UPM package?

Discussion in 'Package Manager' started by oshoham, Sep 8, 2019.

  1. oshoham

    oshoham

    Joined:
    Oct 12, 2017
    Posts:
    4
    Hi, I've been developing a Unity package, and I'm running into an issue with loading assets from my package at runtime. As far as I can tell, Resouces.Load() doesn't work because packages are installed outside of the Assets folder.

    So far, I've come across this documentation on loading package assets via AssetDatabase.LoadAssetAtPath(), which works great in the Editor, but doesn't work in a build, because AssetDatabase is part of UnityEditor. Is there another solution to this problem that will work in the both the Editor and in a build? Or is there some way to get Resources.Load() working with assets located in a package?

    Thanks!
     
  2. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    TextMesh Pro package copied runtime assets into Assets folder...
     
  3. oshoham

    oshoham

    Joined:
    Oct 12, 2017
    Posts:
    4
    If anyone comes across this in the future, turns out I was wrong - you can put a Resources folder in your UPM package and it'll work as expected.
     
  4. WingWeaver

    WingWeaver

    Joined:
    Oct 3, 2018
    Posts:
    2
    What do you mean by this? and how do you load from this Package Resource folder? Work as expected? I wouldnt even expect that to work :DDD
     
  5. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    243
    Can someone confirm if assets in a Resources folder inside a UPM package are accessible through Resources API and bundled inside resources.assets ?

    EDIT : documentation is not clear, but this message from a Unity dev seems to indicate that Resources folders inside packages behave like Resources in Assets/ :
    https://forum.unity.com/threads/local-packages.637630/#post-4280413
     
    Last edited: Sep 15, 2022
  6. QFord

    QFord

    Joined:
    Nov 9, 2013
    Posts:
    17
    Using the Resources loading method in UPM is not an official recommended practice, and there is a potential risk of conflict, as you can imagine, if UPM packages all place Resources under resources. Please use AB mode instead.
     
    Midiphony-panda likes this.