用于自动加载 Prefab 文件的内容、保存内容并再次卸载内容的一次性辅助结构。
此结构允许您暂时加载 Prefab 文件的 GameObject 内容,在代码块内按您的意愿修改内容,并在退出范围时自动将结果保存回 Prefab 文件,并卸载临时内容。
此范围结构包装了 API 的 LoadPrefabContents、SaveAsPrefabAsset 和 UnloadPrefabContents。
using UnityEditor; using UnityEngine;
public static class PrefabUtilityTesting { [MenuItem("Prefabs/Test_EditPrefabContentsScope")] public static void Test() { // Create a simple test Prefab Asset. Looks like this: // Root // A // B // C var assetPath = "Assets/MyTempPrefab.prefab"; var source = new GameObject("Root"); var childA = new GameObject("A"); var childB = new GameObject("B"); var childC = new GameObject("C"); childA.transform.parent = source.transform; childB.transform.parent = source.transform; childC.transform.parent = source.transform; PrefabUtility.SaveAsPrefabAsset(source, assetPath);
using (var editingScope = new PrefabUtility.EditPrefabContentsScope(assetPath)) { var prefabRoot = editingScope.prefabContentsRoot;
// Removing GameObjects is supported Object.DestroyImmediate(prefabRoot.transform.GetChild(2).gameObject);
// Reordering and reparenting are supported prefabRoot.transform.GetChild(1).parent = prefabRoot.transform.GetChild(0);
// Adding GameObjects is supported var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.parent = prefabRoot.transform; cube.name = "D";
// Adding and removing components are supported prefabRoot.AddComponent<AudioSource>(); }
// Prefab Asset now looks like this: // Root // A // B // D } }
assetPath | Prefab 资产的文件路径。 |
prefabContentsRoot | Prefab 内容的根 GameObject。 |