unity获取一个目录下的所有prefab路径

心已入冬 提交于 2020-01-15 08:36:53

目录结构:

获取Prefab下的所有prefab

using System;
using System.IO;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem("BuildTool/Lugs")]
    static void LugsTest()
    {
        string path = "Assets/UI/Prefab";
        GetAllPrefabs(path);
    }

    static void GetAllPrefabs(string directory)
    {
        if (string.IsNullOrEmpty(directory) || !directory.StartsWith("Assets"))
            throw new ArgumentException("folderPath");

        string[] subFolders = Directory.GetDirectories(directory);
        string[] guids = null;
        string[] assetPaths = null;
        int i = 0, iMax = 0;
        foreach (var folder in subFolders)
        {
            guids = AssetDatabase.FindAssets("t:Prefab", new string[] { folder });
            assetPaths = new string[guids.Length];
            for (i = 0, iMax = assetPaths.Length; i < iMax; ++i)
            {
                assetPaths[i] = AssetDatabase.GUIDToAssetPath(guids[i]);
                Debug.Log(assetPaths[i]);
            }
        }
    }
}

 执行结果:

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!