Unity- 小“东西”

菜单栏遍历处理预制体工具


    public class GameEditor : Editor
    {

        private static void ProcessPrefabs(Action<GameObject> doProcess)
        {
            List<GameObject> prefabs = new List<GameObject>();
            var resourcesPath = Application.dataPath;
            var absolutePaths = System.IO.Directory.GetFiles(resourcesPath, "*.prefab", System.IO.SearchOption.AllDirectories);
            for (int i = 0; i < absolutePaths.Length; i++)
            {
                EditorUtility.DisplayProgressBar("获取预制体……", "获取预制体中……", (float)i / absolutePaths.Length);

                string path = "Assets" + absolutePaths[i].Remove(0, resourcesPath.Length);
                path = path.Replace("\\", "/");
                GameObject prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
                if (prefab != null)
                {
                    prefabs.Add(prefab);
                    doProcess(prefab);
                }
                else
                    Debug.Log("预制体不存在! " + path);
            }
            EditorUtility.ClearProgressBar(); 
        }
        [MenuItem("遍历文件夹预制体并处理")]
        static void ProcessPrefabs()
        {
            bool isDirty =false;
            ProcessPrefabs((prefab) =>
            {
                prefab.AddComponent<Animation>();
                EditorUtility.SetDirty(prefab);
                isDirty = true;
            });
            if(isDirty)
             AssetDatabase.SaveAssets();
            EditorUtility.DisplayDialog("tips", "完成了", "确定");
        }
         
    }
posted @ 2020-05-21 17:42  天山鸟  阅读(131)  评论(0编辑  收藏  举报