Unity之DontDestroyOnLoad的使用
发表于2019-01-14
Unity中的一个方法DontDestroyOnLoad可以让某些游戏对象在切换场景的时候不是释放,听上去很好用的方法,
了解一下官方文档:
public static void DontDestroyOnLoad(Object target);
Parameters
target An Object not destroyed on Scene change.
Description
Do not destroy the target Object when loading a new Scene.
The load of a new Scene destroys all current Scene objects. Call Object.DontDestroyOnLoad to preserve an Object during level loading. If the target Object is a component or GameObject, Unity will also preserve all of the Transform’s children. Object.DontDestroyOnLoad does not return a value. Change the argument type using the typeof operator.
公共静态空洞DontDestroyOnLoad(对象目标);
参数
目标:场景改变时未被摧毁的物体。
描述:加载新场景时不要销毁目标对象。
新场景的负载会破坏所有当前场景对象。调用Object.DontDestroyOnLoad在级别加载期间保存对象。如果目标对象是组件或游戏对象,Unity还将保留Transform的所有子对象。对象。DontDestroyOnLoad不返回值。使用typeof操作符更改参数类型。
官方的举例:当我从游戏scence1开始播放背景音乐,而切换到scence2的时候还希望继续播放这个音乐,那么久可以使用这个DontDestroyLoad这种形式,
(请创建两个新的场景,命名为scene1和scene2。打开scene1并添加SceneSwap.cs脚本为空游戏对象给它起个名字Menu。接下来,添加DontDestroy.cs换一个新的游戏对象给它起个名字BackgroundMusic。添加一个音频源到BackgroundMusic - Add Component > Audio > Audio Source-并进口AudioClip进入你的项目。分配AudioClip到音频源氏AudioClip场。创建一个标签,称之为music,并将其添加到BackgroundMusic。切换到scene2。再加SceneSwap.cs换一个新的游戏对象给它起个名字Menu。拯救计划。回到scene1并从Editor.)
using UnityEngine; using UnityEngine.SceneManagement; // Object.DontDestroyOnLoad example. // // Two scenes call each other. This happens when OnGUI button is clicked. // scene1 will load scene2; scene2 will load scene1. Both scenes have // the Menu GameObject with the SceneSwap.cs script attached. // // AudioSource plays an AudioClip as the game runs. This is on the // BackgroundMusic GameObject which has a music tag. The audio // starts in AudioSource.playOnAwake. The DontDestroy.cs script // is attached to BackgroundMusic. public class SceneSwap : MonoBehaviour { private void OnGUI() { int xCenter = (Screen.width / 2); int yCenter = (Screen.height / 2); int width = 400; int height = 120; GUIStyle fontSize = new GUIStyle(GUI.skin.GetStyle("button")); fontSize.fontSize = 32; Scene scene = SceneManager.GetActiveScene(); if (scene.name == "scene1") { // Show a button to allow scene2 to be switched to. if (GUI.Button(new Rect(xCenter - width / 2, yCenter - height / 2, width, height), "Load second scene", fontSize)) { SceneManager.LoadScene("scene2"); } } else { // Show a button to allow scene1 to be returned to. if (GUI.Button(new Rect(xCenter - width / 2, yCenter - height / 2, width, height), "Return to first scene", fontSize)) { SceneManager.LoadScene("scene1"); } } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; // Object.DontDestroyOnLoad example. // // This script example manages the playing audio. The GameObject with the // "music" tag is the BackgroundMusic GameObject. The AudioSource has the // audio attached to the AudioClip. public class DontDestroy : MonoBehaviour { void Awake() { GameObject[] objs = GameObject.FindGameObjectsWithTag("music"); if (objs.Length > 1) { Destroy(this.gameObject); } DontDestroyOnLoad(this.gameObject); } }
其实真的使用起来并没有想象的那么简单,当你从一个场景切换到另一个然后在切回来的时候,会多了一个DontDestroyOnLoad的游戏对象,建议把需要DontDestroyOnLoad的游戏对象放到一个在游戏逻辑中不会返回的一个场景,比如说放到登录时加载的那个场景,或者代码使用一个静态变量做标志,(static bool isHave )是否DontDestroyOnLoad过,若DontDestroyOnLoad就将其赋值为True,想要找到这个游戏物体的话,搜索Find 是可以找到的,或者使用标签的形式FindGameObjectWithTag。