Unity AssetDatabase.FindAssets

Unity里的AssetDataBase简单介绍

UnityEditor:

提示:AssetDatabase.FindAssets

using UnityEngine;
using UnityEditor;
using System.IO;

/*
1.编译器代码:继承自Editor,只适用于Unity编辑器中执行代码
2.[MenuItem("......")] 用于修饰在编译器产生菜单
3.AssetDatabase:只适用于编译器操作资源
4.StreamingAssets:Unity特殊目录之一,该目录中文件不会被压缩,适合于移动端读取资源(只读)(PC端可写入)
5.Application.persistentDataPath: 可以在运行时进行读写,Unity外部目录(安装软件时才产生)
*/

public class GenerateResConfig : Editor
{
   
    [MenuItem("Tool/Resources/Generate ResConfig File")]
    public static void Generate()
    {
        //生成资源配置文件
        //1.查找 Resouces 所有预制件完整路径
        string rootPath = "Assets/Resources";
        //string extension = "prefab";
        string[] resFiles = AssetDatabase.FindAssets("t:Scene t:Prefab t:Texture"/*"t:" + extension*/, new string[] { rootPath });

        for (int i = 0; i < resFiles.Length; i++)
        {
            //GUID
            resFiles[i] = AssetDatabase.GUIDToAssetPath(resFiles[i]);
            //2.生成对应关系
            //"Assets/Resources/Cube (1).prefab"
            string fileName = Path.GetFileNameWithoutExtension(resFiles[i]);
            string filePath = resFiles[i].Replace(rootPath + "/", string.Empty)/*.Replace("." + extension, string.Empty)*/;
            resFiles[i] = fileName + "=" + filePath;
        }
        //3.写入文件
        string savePath = Application.streamingAssetsPath + "/Config";
        if (Directory.Exists(savePath) == false)
            Directory.CreateDirectory(savePath);
        File.WriteAllLines(savePath + "/ConfigMap.txt", resFiles);
        //4.刷新
        AssetDatabase.Refresh();
    }
}

Unity AssetDatabase.FindAssets_第1张图片

你可能感兴趣的:(unity,游戏引擎)