Object.DontDestroyOnLoad

本文详细介绍了如何在Unity中使用DontDestroyOnLoad函数,使对象在加载新场景时不被销毁。该函数可以确保指定的游戏对象或组件在场景切换时保持不变,这对于跨场景保留状态非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

static function DontDestroyOnLoad (target : Object) : void

Description

Makes the object target not be destroyed automatically when loading a new scene.

When loading a new level all objects in the scene are destroyed, then the objects in the new level are loaded. In order to preserve an object during level loading call DontDestroyOnLoad on it. If the object is a component or game object then its entire transform hierarchy will not be destroyed either.

C#
using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Awake() {
        DontDestroyOnLoad(transform.gameObject);
    }
}
 
  
 


Note though that you will need to call Object.Destroy() if you decide to load a new scene and do not need those objects around anymore, otherwise they will persist until the application dies.

using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace MultiMouseUnity { // Token: 0x0200000B RID: 11 public class MultiMouseWrapper : MonoBehaviour { // Token: 0x17000004 RID: 4 // (get) Token: 0x0600002C RID: 44 RVA: 0x00002698 File Offset: 0x00000898 public int ActiveDeviceCount { get { return this.activeDevices.Count; } } // Token: 0x0600002D RID: 45 RVA: 0x000026A8 File Offset: 0x000008A8 public MultiMouseDevice TryGetDeviceAtIndex(int index) { if (!this.IsMouseActive(index)) { return null; } Debug.Log("TryGetDeviceAtIndex" + this.activeDevices.Count.ToString()); return this.activeDevices[index]; } // Token: 0x0600002E RID: 46 RVA: 0x000026EE File Offset: 0x000008EE public bool IsMouseActive(int index) { return Mathf.Abs(index) < this.activeDevices.Count; } // Token: 0x17000005 RID: 5 // (get) Token: 0x0600002F RID: 47 RVA: 0x00002703 File Offset: 0x00000903 private MultiMouseSettings settings { get { return MultiMouseSettings.Settings; } } // Token: 0x17000006 RID: 6 // (get) Token: 0x06000030 RID: 48 RVA: 0x0000270A File Offset: 0x0000090A public static MultiMouseWrapper Instance { get { if (MultiMouseWrapper.instance == null) { MultiMouseWrapper.instance = UnityEngine.Object.FindObjectOfType<MultiMouseWrapper>(); if (MultiMouseWrapper.instance == null) { Debug.LogWarning("MultiMouseWrapper not found in this scene!"); } } return MultiMouseWrapper.instance; } } // Token: 0x17000007 RID: 7 // (get) Token: 0x06000031 RID: 49 RVA: 0x0000273F File Offset: 0x0000093F public static bool Initialized { get { return MultiMouse.Initialized; } } // Token: 0x17000008 RID: 8 // (get) Token: 0x06000032 RID: 50 RVA: 0x00002746 File Offset: 0x00000946 public static Action[] OnLeftMouseButtonDown { get { if (MultiMouseWrapper.onLeftMouseButtonDown == null) { MultiMouseWrapper.onLeftMouseButtonDown = new Action[MultiMouseWrapper.Instance.settings.maxDevices]; } return MultiMouseWrapper.onLeftMouseButtonDown; } } // Token: 0x17000009 RID: 9 // (get) Token: 0x06000033 RID: 51 RVA: 0x0000276D File Offset: 0x0000096D public static Action[] OnLeftMouseButtonUp { get { if (MultiMouseWrapper.onLeftMouseButtonUp == null) { MultiMouseWrapper.onLeftMouseButtonUp = new Action[MultiMouseWrapper.Instance.settings.maxDevices]; } return MultiMouseWrapper.onLeftMouseButtonUp; } } // Token: 0x1700000A RID: 10 // (get) Token: 0x06000034 RID: 52 RVA: 0x00002794 File Offset: 0x00000994 public static Action[] OnRightMouseButtonDown { get { if (MultiMouseWrapper.onRightMouseButtonDown == null) { MultiMouseWrapper.onRightMouseButtonDown = new Action[MultiMouseWrapper.Instance.settings.maxDevices]; } return MultiMouseWrapper.onRightMouseButtonDown; } } // Token: 0x1700000B RID: 11 // (get) Token: 0x06000035 RID: 53 RVA: 0x000027BB File Offset: 0x000009BB public static Action[] OnRightMouseButtonUp { get { if (MultiMouseWrapper.onRightMouseButtonUp == null) { MultiMouseWrapper.onRightMouseButtonUp = new Action[MultiMouseWrapper.Instance.settings.maxDevices]; } return MultiMouseWrapper.onRightMouseButtonUp; } } // Token: 0x06000036 RID: 54 RVA: 0x000027E2 File Offset: 0x000009E2 [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] private static void OnRuntimeMethodLoad() { } // Token: 0x06000037 RID: 55 RVA: 0x000027E4 File Offset: 0x000009E4 private void Update() { MultiMouseWrapper.StaticUpdate(); } // Token: 0x06000038 RID: 56 RVA: 0x000027EC File Offset: 0x000009EC private void Awake() { if (MultiMouseWrapper.Instance != null) { if (MultiMouseWrapper.Instance.gameObject.scene.name == "DontDestroyOnLoad") { this.destroySelf = true; UnityEngine.Object.Destroy(base.gameObject); return; } if (string.IsNullOrEmpty(MultiMouseWrapper.Instance.gameObject.scene.name)) { MultiMouseWrapper.instance = null; } } } // Token: 0x06000039 RID: 57 RVA: 0x00002860 File Offset: 0x00000A60 private void OnDestroy() { if (MultiMouseWrapper.Instance == this) { MultiMouseWrapper.instance = null; if (MultiMouse.Initialized) { MultiMouse.Destroy(); } } } // Token: 0x0600003A RID: 58 RVA: 0x00002884 File Offset: 0x00000A84 private void Start() { if (this.destroySelf) { return; } if (this.settings == null) { base.enabled = false; this.DebugError("Could not find MultiMouse Settings! If you continue to see this message, you may have to restart the Unity Editor."); return; } MultiMouse.Initialize(); if (base.transform.parent != null) { base.transform.SetParent(null); } UnityEngine.Object.DontDestroyOnLoad(this); if (this.settings.detectDevicesOnStart) { this.detectDeviceRoutine = base.StartCoroutine(this.DetectDevice()); } } // Token: 0x0600003B RID: 59 RVA: 0x00002904 File Offset: 0x00000B04 private static void StaticUpdate() { if (MultiMouseWrapper.instance) { MultiMouseWrapper.instance.UpdateDevices(); } } // Token: 0x0600003C RID: 60 RVA: 0x0000291C File Offset: 0x00000B1C private void UpdateDevices() { for (int i = 0; i < this.activeDevices.Count; i++) { if (this.detectDeviceRoutine == null) { MultiMouse.MultiMousePoll(this.activeDevices[i].DeviceID); } for (int j = 1; j <= 4; j++) { if (MultiMouse.GetMouseButtonDown(this.activeDevices[i].DeviceID, j)) { Debug.Log("update"); this.isButtonDown[i][j] = true; if (j != 1) { if (j == 2) { Action action = MultiMouseWrapper.OnRightMouseButtonDown[i]; if (action != null) { action(); } } } else { Action action2 = MultiMouseWrapper.OnLeftMouseButtonDown[i]; if (action2 != null) { action2(); } } } else if (MultiMouse.GetMouseButtonUp(this.activeDevices[i].DeviceID, j)) { this.isButtonDown[i][j] = false; if (j != 1) { if (j == 2) { Action action3 = MultiMouseWrapper.OnRightMouseButtonUp[i]; if (action3 != null) { action3(); } } } else { Action action4 = MultiMouseWrapper.OnLeftMouseButtonUp[i]; if (action4 != null) { action4(); } } } } this.UpdateMousePosition(this.activeDevices[i]); } } // Token: 0x0600003D RID: 61 RVA: 0x00002A46 File Offset: 0x00000C46 [ContextMenu("StartDetectingDevice")] public void StartDetectingDevice() { if (this.detectDeviceRoutine != null) { base.StopCoroutine(this.detectDeviceRoutine); } this.detectDeviceRoutine = base.StartCoroutine(this.DetectDevice()); } // Token: 0x0600003E RID: 62 RVA: 0x00002A6E File Offset: 0x00000C6E private IEnumerator DetectDevice() { do { string id = ""; while (string.IsNullOrEmpty(id)) { id = MultiMouse.GetAnyMousePressingButton(1); if (!string.IsNullOrEmpty(id) && this.idToDevice.ContainsKey(id)) { id = ""; } yield return null; } Debug.Log("MultiMouse: Found device of ID " + id); MultiMouseDevice multiMouseDevice = new MultiMouseDevice(); multiMouseDevice.DeviceID = id; multiMouseDevice.IsLightgun = (MultiMouse.GetAbsoluteMousePosition(id) != Vector2.zero); multiMouseDevice.Position = new Vector2((float)Screen.width / 2f, (float)Screen.height / 2f); this.activeDevices.Add(multiMouseDevice); this.idToDevice.Add(id, multiMouseDevice); this.isButtonDown.Add(new bool[4]); Action<int> onDeviceFound = MultiMouseWrapper.OnDeviceFound; if (onDeviceFound != null) { onDeviceFound(this.activeDevices.Count - 1); } id = null; } while (this.settings.keepDetectingUntilFull && this.activeDevices.Count < this.settings.maxDevices); this.detectDeviceRoutine = null; yield break; } // Token: 0x0600003F RID: 63 RVA: 0x00002A7D File Offset: 0x00000C7D public bool GetMouseButton(int deviceID, int buttonIndex) { if (!this.IsMouseActive(deviceID)) { this.DebugWarning("No device found at ID " + deviceID.ToString()); return false; } return this.isButtonDown[deviceID][buttonIndex + 1]; } // Token: 0x06000040 RID: 64 RVA: 0x00002AB4 File Offset: 0x00000CB4 public bool GetMouseButtonDown(int deviceID, int buttonIndex) { MultiMouseDevice multiMouseDevice = this.TryGetDeviceAtIndex(deviceID); if (multiMouseDevice == null) { this.DebugWarning("No device found at ID " + deviceID.ToString()); return false; } return MultiMouse.GetMouseButtonDown(multiMouseDevice.DeviceID, buttonIndex + 1); } // Token: 0x06000041 RID: 65 RVA: 0x00002AF4 File Offset: 0x00000CF4 public bool GetMouseButtonUp(int deviceID, int buttonIndex) { MultiMouseDevice multiMouseDevice = this.TryGetDeviceAtIndex(deviceID); if (multiMouseDevice == null) { this.DebugWarning("No device found at ID " + deviceID.ToString()); return false; } return MultiMouse.GetMouseButtonUp(multiMouseDevice.DeviceID, buttonIndex + 1); } // Token: 0x06000042 RID: 66 RVA: 0x00002B34 File Offset: 0x00000D34 private void UpdateMousePosition(MultiMouseDevice device) { if (device.IsLightgun) { Vector2 absoluteMousePosition = MultiMouse.GetAbsoluteMousePosition(device.DeviceID); Rect normalizedUISpaceContainerRect = MultiMouseWrapper.GetNormalizedUISpaceContainerRect(); absoluteMousePosition.x /= 65535f; absoluteMousePosition.y /= 65535f; float num = Mathf.InverseLerp(normalizedUISpaceContainerRect.xMin, normalizedUISpaceContainerRect.xMax, absoluteMousePosition.x); float num2 = Mathf.InverseLerp(normalizedUISpaceContainerRect.yMax, normalizedUISpaceContainerRect.yMin, absoluteMousePosition.y); device.Position = new Vector2(num * (float)Screen.width, num2 * (float)Screen.height); return; } device.Position += MultiMouse.GetRelativeMousePosition(device.DeviceID); if (!this.settings.unclampMousePosition) { device.Position = new Vector2(Mathf.Clamp(device.Position.x, 0f, (float)Screen.width), Mathf.Clamp(device.Position.y, 0f, (float)Screen.height)); } } // Token: 0x06000043 RID: 67 RVA: 0x00002C40 File Offset: 0x00000E40 public Vector2 GetMousePosition(int deviceID) { MultiMouseDevice multiMouseDevice = this.TryGetDeviceAtIndex(deviceID); if (multiMouseDevice == null) { this.DebugWarning("No device found at ID " + deviceID.ToString()); return Vector2.zero; } return multiMouseDevice.Position; } // Token: 0x06000044 RID: 68 RVA: 0x00002C7B File Offset: 0x00000E7B private void OnApplicationFocus(bool focus) { if (!MultiMouse.Initialized || !focus) { return; } Debug.Log("Application regained focus, re-initializing MultiMouse..."); MultiMouse.Destroy(); MultiMouse.Initialize(); } // Token: 0x06000045 RID: 69 RVA: 0x00002C9C File Offset: 0x00000E9C private void DebugWarning(string message) { Debug.LogWarning("MultiMouse Warning: " + message); } // Token: 0x06000046 RID: 70 RVA: 0x00002CAE File Offset: 0x00000EAE private void DebugError(string message) { Debug.LogWarning("MultiMouse Error: " + message); } // Token: 0x06000047 RID: 71 RVA: 0x00002CC0 File Offset: 0x00000EC0 private static Rect GetNormalizedUISpaceContainerRect() { Rect rect = new Rect(0f, 0f, (float)Screen.width, (float)Screen.height); DisplayInfo mainWindowDisplayInfo = Screen.mainWindowDisplayInfo; return new Rect(rect.x / (float)mainWindowDisplayInfo.width, rect.y / (float)mainWindowDisplayInfo.height, rect.width / (float)mainWindowDisplayInfo.width, rect.height / (float)mainWindowDisplayInfo.height); } // Token: 0x04000017 RID: 23 private List<MultiMouseDevice> activeDevices = new List<MultiMouseDevice>(); // Token: 0x04000018 RID: 24 private Dictionary<string, MultiMouseDevice> idToDevice = new Dictionary<string, MultiMouseDevice>(); // Token: 0x04000019 RID: 25 private const int MAX_BUTTONS = 4; // Token: 0x0400001A RID: 26 private List<bool[]> isButtonDown = new List<bool[]>(); // Token: 0x0400001B RID: 27 private bool mouseHeld; // Token: 0x0400001C RID: 28 private static MultiMouseWrapper instance; // Token: 0x0400001D RID: 29 public static Action<int> OnDeviceFound; // Token: 0x0400001E RID: 30 private static Action[] onLeftMouseButtonDown; // Token: 0x0400001F RID: 31 private static Action[] onLeftMouseButtonUp; // Token: 0x04000020 RID: 32 private static Action[] onRightMouseButtonDown; // Token: 0x04000021 RID: 33 private static Action[] onRightMouseButtonUp; // Token: 0x04000022 RID: 34 private bool destroySelf; // Token: 0x04000023 RID: 35 private Coroutine detectDeviceRoutine; } } 解释一下上边的代码
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值