GameObject rootObject = contentsRoot.transform.root.gameObject;

        var all = rootObject.GetComponentsInChildren<Transform>(true);

        foreach (var el in all)

        {

            if(PrefabUtility.IsPartOfRegularPrefab(el.gameObject))

            {

                PrefabUtility.UnpackPrefabInstance(el.gameObject, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);

            }

        }

Colored by Color Scripter

 

 

 

using UnityEngine;

using UnityEditor;

 

public class Example

{

    [MenuItem("Examples/Add BoxCollider to Prefab Asset")]

    static void AddBoxColliderToPrefab()

    {

        // Get the Prefab Asset root GameObject and its asset path.

        GameObject assetRoot = Selection.activeObject as GameObject;

        string assetPath = AssetDatabase.GetAssetPath(assetRoot);

 

        // Load the contents of the Prefab Asset.

        GameObject contentsRoot = PrefabUtility.LoadPrefabContents(assetPath);

 

        // Modify Prefab contents.

        contentsRoot.AddComponent<BoxCollider>();






        // Save contents back to Prefab Asset and unload contents.

        PrefabUtility.SaveAsPrefabAsset(contentsRoot, assetPath);

        PrefabUtility.UnloadPrefabContents(contentsRoot);

    }

 

    [MenuItem("Examples/Add BoxCollider to Prefab Asset"true)]

    static bool ValidateAddBoxColliderToPrefab()

    {

        GameObject go = Selection.activeObject as GameObject;

        if (go == null)

            return false;

 

        return PrefabUtility.IsPartOfPrefabAsset(go);

    }

}

 

 

 

 

 

 

ref : docs.unity3d.com/ScriptReference/PrefabUtility.LoadPrefabContents.html?_ga=2.99521374.1778878325.1608379497-1959443505.1601370427

 

반응형

+ Recent posts