Does Resources.LoadAssetAtPath only work on Resources?

Odd title. Maybe I can explain better.

I’ve made a Build script so that I can package my assets for WWW transport. I use Resources.LoadAssetAtPath(path, type); to temporarily fill an object array, which I then pass off to BuildPipeline.BuildAssetBundle, which gets LZMA to package… etc etc.

Anyway… I know Resources.LoadAssetAtPath only works in the Editor, and as far as I know, the BuildScripts are part of the editor, so this should work. But, I’m only getting Null returns.

        List<string> fileList = getTextureFiles(folder);
        List<UnityEngine.Object> objectList = new List<UnityEngine.Object>();
        string assetRelFolder = "data" + folder;

        foreach (string file in fileList)
        {
            UnityEngine.Object myText = Resources.LoadAssetAtPath(assetRelFolder + file, typeof(Texture));
            Debug.Log(myText);
            objectList.Add(myText);
        }

        BuildPipeline.BuildAssetBundle(null, objectList.ToArray(), bundleTempDir + bundleName);

Funny thing is, I can File.Exists on the same path used on the LoadAssetAtPath, and it returns true, but I’m getting a null return on LoadAssetAtPath.

Is Resources.LoadAssetAtPath restricted to only “Assets/…” directories? Is it a RELATIVE pathname that I should be using? Or can I use a direct path?

I have an external image directory:
C:\Project\Data\Images

The unity project is:
C:\Project

And there is an assets folder:
C:\Project\Assets

I’m attempting to use Resources.LoadAssetAtPath to pull from the “data/images” directory.

I’m really sorry I’m not good at explaining things.

all unity commands only operate on the content within the assets folder.
There is no world outside for them especially not for editor scripts as they rely on the the asset library data which naturally is only done for the assets folder (otherwise starting up the project would take weeks and gbs of data to collect all possible items on the whole system)

Gotcha! Thanks. Didn’t know if I could have gotten away with it or not, like with .NET. Thanks!

In case you’re like me and you want to load an image in the Editor code, perhaps to make an Atlas, and you don’t want the source asset to live in the Assets folder, you can load it like this:

string path = Path.GetFullPath("NotTheAssetsFolder/feelsgoodman.jpg");
WWW www = new WWW("file://" + path);
while (!www.isDone && www.error == null)
        continue;

Texture2D tex = new Texture2D(www.texture.width, www.texture.height, TextureFormat.ARGB32, false);
www.LoadImageIntoTexture(tex);
tex.name = "mytexturename.jpg" ;

You have to name the texture yourself, and if Unity decides to destroy your texture for no reason try sticking it in a class or static variable.

Cheers.

1 Like