该对象不会保存到场景中。加载新场景时不会被销毁。它是 HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor | HideFlags.DontUnloadUnusedAsset 的快捷方式。
您必须使用 DestroyImmediate 手动清除内存中的对象,以避免内存泄漏。
对于场景中的预制实例,您可以将隐藏标志设置为预制实例句柄对象,以此方式为预制实例的所有对象设置相同的隐藏标志。(请参阅 PrefabUtility.GetPrefabInstanceHandle)。
using System.IO; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine;
// Creates new menu items under 'Examples' in the main menu. public class DontSaveExamples { [MenuItem("Examples/GameObject (and child) HideFlags.DontSave example")] static void DontSaveExample_RootWithDontSave() { //Setup hierarchy with root and one child GameObject rootGameObject = new GameObject("Root"); GameObject child = new GameObject("Child"); child.transform.parent = rootGameObject.transform;
rootGameObject.hideFlags = HideFlags.DontSave; //child.hideFlags = HideFlags.DontSave; //No difference for plain GameObjects as the root is marked with DontSave
//Load new scene EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
//Both objects still exist as the root is marked with DontSave Debug.Log("Root name: " + rootGameObject.name + ", Child name: " + child.name);
//Remember to clean up, this also deleted the child GameObject Object.DestroyImmediate(rootGameObject); }
[MenuItem("Examples/Save Prefab with child HideFlags.DontSave example")] static void DontSaveExample_Prefab_ChildNotSavedToDisk() { //Ensure the existence of a Prefabs folder inside the Assets folder if (!Directory.Exists("Assets/Prefabs")) AssetDatabase.CreateFolder("Assets", "Prefabs");
//Setup hierarchy with root and one child GameObject rootGameObject = new GameObject("Root"); GameObject child = new GameObject("Child"); child.transform.parent = rootGameObject.transform;
//Mark child to prevent saving it child.hideFlags = HideFlags.DontSave;
//Save the Prefab asset GameObject prefabAsset = PrefabUtility.SaveAsPrefabAsset(rootGameObject, "Assets/Prefabs/Root.prefab");
//No children in prefab as the child was marked as DontSave. The childCount is 0. Debug.Log("Child GameObject in Prefab: " + prefabAsset.transform.childCount);
//Child still exists in scene. The childCount is 1. Debug.Log("Child GameObject in Scene GameObject: " + rootGameObject.transform.childCount);
//Load new scene EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
//Child is deleted as it was a child of the root GameObject that got deleted in the scene transition if (child == null) Debug.Log("Child deleted correctly"); else Debug.Log("Child was not deleted successfully"); } }