该游戏对象不会显示在层级视图中,不会保存到场景中,也不会被 Resources.UnloadUnusedAssets 卸载。
这通常用于由脚本创建并完全由脚本控制的游戏对象。
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { // Creates a Material that is explicitly created & destroyed by the component. // Resources.UnloadUnusedAssets will not unload it, and it will not be editable by the Inspector. private Material ownedMaterial; void OnEnable() { ownedMaterial = new Material(Shader.Find("Diffuse")); ownedMaterial.hideFlags = HideFlags.HideAndDontSave; GetComponent<Renderer>().sharedMaterial = ownedMaterial; }
// Objects created as hide and don't save must be explicitly destroyed by the owner of the object. void OnDisable() { DestroyImmediate(ownedMaterial); } }