Unity:获取资源路径

3

我在Unity3D中工作。

我使用Resources.LoadAll(path);加载文件夹及其子文件夹中的所有项目。之后,我想获取对象所在的子文件夹名称或完整路径。这是否可能?

请不要建议AssetDatabase,因为它是一个编辑器类,而我需要它在我的构建中。

提前致谢。

2个回答

7
我所做的是在编辑器中编写一个“Resources/manifest.txt”文件,如果需要在运行时查找资源文件/子文件夹,则在游戏中加载它。可以在编辑器中自动化创建此manifest.txt文件,以便始终保持最新状态。
然后,在运行时,我不使用Resources.LoadAll,而是加载manifest.txt,在其中查找文件夹/资产并使用Resources.Load进行加载。

我已经想到了一个类似的解决方案,希望能有一个更好的解决方案。但还是谢谢你的见解 ;) - Kazoeja
只是为了确认您的怀疑,您可以确定所加载的对象的类型,但我还没有找到任何恢复其路径的方法。 - Calvin
嗨,我可以知道你是如何获取文件和文件夹的吗?我使用了Directory.GetFiles,但它不起作用。 - Seaky Lone

0

Resources.Load() 方法会返回指定路径下的资源,如果能找到并且其类型与请求的泛型参数类型匹配,则返回该资源;否则返回 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");
    }
}

更多信息请查看:https://docs.unity3d.com/ScriptReference/Resources.Load.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接