obsidian/笔记文件/2.笔记/unity资源包含的数据.md
2025-03-26 00:02:56 +08:00

2.1 KiB
Raw Blame History

#unity/日常积累

AssetDatabase.LoadAllAssetsAtPath

assetPath资源的文件系统路径。

描述 返回一个含 assetPath 下所有资源的数组。

一些资源文件可能会包含多个子资源(例如,一个 Maya 文件可能会包含多个网格和游戏对象)。 所有路径均相对于项目文件夹例如“Assets/MyTextures/hello.png”。 该函数在给定路径中返回 主资源以及所有的 子资源,包含那些隐藏在 Project 视图中的资源。

注意:主资源并不保证在数组中处于索引 0 处

using UnityEngine;
using UnityEditor;

public class Example : MonoBehaviour
{
    [MenuItem("AssetDatabase/LoadAllAssetsAtPath")]
    private static void PrintAssets()
    {
        Object[] data = AssetDatabase.LoadAllAssetsAtPath("Assets/MySpriteTexture.png");

        Debug.Log(data.Length + " Assets");

        foreach (Object o in data)
        {
            Debug.Log(o);
        }

        // outputs:
        //  5 Assets
        //  MySpriteTexture (UnityEngine.Texture2D)
        //  MyTexture_0 (UnityEngine.Sprite)
        //  MyTexture_1 (UnityEngine.Sprite)
        //  MyTexture_2 (UnityEngine.Sprite)
        //  MyTexture_3 (UnityEngine.Sprite)
    }
}

AssetDatabase.LoadAllAssetRepresentationsAtPath

描述 返回 assetPath 下所有的子资源。

此函数仅返回 Project 视图中可见的子资源。 所有路径均相对于项目文件夹例如“Assets/MyTextures/hello.png”

using UnityEngine;
using UnityEditor;

public class Example : MonoBehaviour
{
    [MenuItem("AssetDatabase/LoadAllAssetRepresentationsAtPath")]
    private static void PrintSubAssets()
    {
        Object[] data = AssetDatabase.LoadAllAssetRepresentationsAtPath("Assets/MySpriteTexture.png");

        Debug.Log(data.Length + " Sub Assets");

        foreach (Object o in data)
        {
            Debug.Log(o);
        }

        // outputs:
        //  4 Sub Assets
        //  MyTexture_0 (UnityEngine.Sprite)
        //  MyTexture_1 (UnityEngine.Sprite)
        //  MyTexture_2 (UnityEngine.Sprite)
        //  MyTexture_3 (UnityEngine.Sprite)