Kazoeja
Kazoeja

Reputation: 143

Unity: Get resource path

I work in Unity3D.

I use Resources.LoadAll(path); to load all items in the folder and subfolders. After I do this I would like to get the subfolder's name of the objects, or the complete path. Is this possible?

And don't suggest AssetDatabase, because it is an editor class, and I need it in my build.

Thanks in advance.

Upvotes: 3

Views: 11530

Answers (2)

Codemaker2015
Codemaker2015

Reputation: 1

Resources.Load() method returns the asset at path if it can be found and if its type matches the requested generic parameter type, otherwise it returns null.

// Loading assets from the Resources folder using the generic Resources.Load<T>(path) method
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    void Start()
    {
        //Load a text file (Assets/Resources/Text/textFile01.txt)
        var textFile = Resources.Load<TextAsset>("Text/textFile01");

        //Load text from a JSON file (Assets/Resources/Text/jsonFile01.json)
        var jsonTextFile = Resources.Load<TextAsset>("Text/jsonFile01");
        //Then use JsonUtility.FromJson<T>() to deserialize jsonTextFile into an object

        //Load a Texture (Assets/Resources/Textures/texture01.png)
        var texture = Resources.Load<Texture2D>("Textures/texture01");

        //Load a Sprite (Assets/Resources/Sprites/sprite01.png)
        var sprite = Resources.Load<Sprite>("Sprites/sprite01");

        //Load an AudioClip (Assets/Resources/Audio/audioClip01.mp3)
        var audioClip = Resources.Load<AudioClip>("Audio/audioClip01");
    }
}

For more: https://docs.unity3d.com/ScriptReference/Resources.Load.html

Upvotes: 0

Calvin
Calvin

Reputation: 4195

What I do is write out a "Resources/manifest.txt" file in the editor that I load in the game if I need to find files/subfolders in the Resources folder at runtime. Creating this manifest.txt can be automated in the editor so that it's always up to date.

Then at run time instead of Resources.LoadAll, I load the manifest.txt, look for the folder/asset in there and Resources.Load it.

Upvotes: 7

Related Questions