Unity Editor下 修改 Prefab

[MenuItem("所有Prefab添加PrefabInfoHerlper组件")]
    static void AddPrefabInfoHelper()
    {
        UnityEngine.Object[] selObjs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
        for (int i = 0; i < selObjs.Length; i++)
        {
            Object selObj = selObjs[i];
            string selfPath = AssetDatabase.GetAssetPath(selObj);
            if (selfPath.EndsWith(".prefab"))
            {
                string[] dependPaths = AssetDatabase.GetDependencies(selfPath);
                GameObject go = GameObject.Instantiate(selObj) as GameObject;
                
                PrefabInfoHelper pih = go.GetComponent();
                if (pih == null)
                {
                    pih = go.AddComponent();
                }
                pih.isInstanced = false;
                pih.selfPath = selfPath;
                pih.dependPathList.Clear();
                pih.dependPathList.AddRange(dependPaths);

                PrefabUtility.ReplacePrefab(go, selObj, ReplacePrefabOptions.ConnectToPrefab);

                GameObject.DestroyImmediate(go);
            }
            
        }

    }

你可能感兴趣的:(unity3d,扩展编辑器)