批量处理Prefab

 
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;

using Object = UnityEngine.Object;
using System.IO;

public class AddItemEffect {

    [ExecuteInEditMode]
    [MenuItem("Tools/AddItemEffect")]
    private static void RecordPointAddFlame() {
        GameObject itemPrefab = AssetDatabase.LoadAssetAtPath("Assets/Configs/Resources/NewUI/@Plugin/item_base_new.prefab", typeof(GameObject)) as GameObject;
        GameObject itembase = PrefabUtility.InstantiatePrefab(itemPrefab) as GameObject;
        itembase.SetActive(true);
        Transform effect = itembase.transform.FindChild("Icon1@Quality/IconEffect@");

        string[] ids = AssetDatabase.FindAssets("t:Prefab", new string[] { "Assets/Configs/Resources/NewUI" });
        for (int i = 0; i < ids.Length; i++) {
            string path = AssetDatabase.GUIDToAssetPath(ids[i]);
            Debug.Log(path);
            if (path.Contains("item_base_new.prefab")) {
                continue;
            }
            GameObject originPrefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
            GameObject prefabClone = PrefabUtility.InstantiatePrefab(originPrefab) as GameObject;

            Transform[] allChild = prefabClone.GetComponentsInChildren<Transform>(true);
            bool isChange = false;
            foreach (Transform item in allChild) {
                if (item.name.Contains("Icon1@")) {
                    if (item.FindChild("IconEffect@") == null) {
                        isChange = true;
                        GameObject go = GameObject.Instantiate(effect.gameObject) as GameObject;
                        go.transform.parent = item;
                        go.transform.localPosition = Vector3.zero;
                        go.transform.localScale = Vector3.one;
                        go.transform.localRotation = Quaternion.identity;
                        go.name = "IconEffect@";
                        UISprite spriteChild = go.GetComponent<UISprite>();
                        UISprite spriteParent = item.GetComponent<UISprite>();
                        spriteChild.width = spriteParent.width * 120 / 92;
                        spriteChild.height = spriteParent.height * 120 / 92;
                        spriteChild.depth = spriteParent.depth + 3;
                    }
                }
            }
            if (isChange) {
                PrefabUtility.ReplacePrefab(prefabClone, originPrefab, ReplacePrefabOptions.Default);
            }
            MonoBehaviour.DestroyImmediate(prefabClone);
        }

        AssetDatabase.SaveAssets();
        EditorUtility.DisplayDialog("成功", "AddItemEffect完成!", "确定");
        Debug.Log("Done");
    }
    [ExecuteInEditMode]
    [MenuItem("Tools/RemoveItemEffect")]
    private static void RemoveItemEffect() {

        string[] ids = AssetDatabase.FindAssets("t:Prefab", new string[] { "Assets/Configs/Resources/NewUI" });
        for (int i = 0; i < ids.Length; i++) {
            string path = AssetDatabase.GUIDToAssetPath(ids[i]);
            Debug.Log(path);
            if (path.Contains("item_base_new.prefab")) {
                continue;
            }
            GameObject originPrefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
            GameObject prefabClone = PrefabUtility.InstantiatePrefab(originPrefab) as GameObject;

            Transform[] allChild = prefabClone.GetComponentsInChildren<Transform>(true);
            bool isChange = false;
            for (int m = allChild.Length-1;m>-1;m--) {
                if (allChild[m].name.Contains("Icon1@")) {
                    Transform ts = allChild[m].FindChild("IconEffect@");
                    if (ts != null) {
                        isChange = true;
                        MonoBehaviour.DestroyImmediate(ts.gameObject);
                    }
                }
            }
            if (isChange) {
                PrefabUtility.ReplacePrefab(prefabClone, originPrefab, ReplacePrefabOptions.Default);
            }
            MonoBehaviour.DestroyImmediate(prefabClone);
        }

        AssetDatabase.SaveAssets();
        EditorUtility.DisplayDialog("成功", "RemoveItemEffect完成!", "确定");
        Debug.Log("Done");
    }
}



查找指定文件下,所有prefab,在所有的名字中含有Icon1&&字符串的子物体下,添加一张图片

参考自:http://www.cnblogs.com/klkucan/p/4934518.html

原文地址:https://www.cnblogs.com/kuluodisi/p/7857228.html