Version: 2020.2

EditPrefabContentsScope

struct in UnityEditor

切换到手册

描述

Disposable helper struct for automatically loading the contents of a Prefab file, saving the contents and unloading the contents again.

This struct allows you to temporarily load the GameObject contents of a Prefab file, modify the contents to your liking inside of a block of code and automatically save the result back to the Prefab file as well as unload the temporary contents when the scope is exited.

This scope struct wraps the API’s LoadPrefabContents, SaveAsPrefabAsset and 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 } }

变量

assetPathFile path of the Prefab asset.
prefabContentsRootThe root GameObject of the Prefab contents.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961