DontDestroyOnLoad
本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/Unity_DontDestroyOnLoad.html
public static void DontDestroyOnLoad(Objecttarget);
Parameters
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.
当加载一个新关卡时,所有场景中所有的物体被销毁,然后新关卡中的物体被加载进来。为了保持在加载新关卡时物体不被销毁,使用DontDestroyOnLoad保持,如果物体是一个组件或游戏物体,它的整个transform层次将不会被销毁,全部保留下来。
// Make this game object and all its transform children
// survive when loading a new scene.
//当加载新场景的时候,使游戏物体和它所有的transform子物体存活下来
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
using UnityEngine;
using System.Collections;
using UnityEngine;
using System.Collections; public class DontDestroyOnLOAD : MonoBehaviour {
private Transform m_root = null; void Start() {
Debug.LogError("Start");
try {
int i = ;
int[] tempInt = new int[];
int j = tempInt[];
}
catch (System.Exception ex) {
Debug.LogException(ex);
}
Debug.LogError("Start End");
} void Awake() {
Debug.LogError("Awake");
string rootName = string.Format("{0}s", "DontDestroyOnLOAD");
GameObject gameObj = new GameObject(rootName);
GameObject.DontDestroyOnLoad(gameObj);
m_root = gameObj.transform;
Debug.LogError("Awake End");
} void OnDisable() {
Debug.LogError("OnDisable");
Debug.LogError("OnDisable End");
} void OnEnable() {
Debug.LogError("OnEnable");
Debug.LogError("OnEnable End");
}
}
第一种情况,start会默认执行;
最开始:脚本的勾勾,勾上,那么就会执行Awake、onEnbale、Start
去掉勾勾,执行onDisable 再加上勾勾执行OnEnable
第二种情况,start是没有执行的,只有在OnEnable时,才会在执行Start,且只执行一次
DontDestroyOnLoad的更多相关文章
- Unity3D Object.DontDestroyOnLoad 备忘
初学Untiy3D,记录备忘. public static void DontDestroyOnLoad(Object target); Makes the object target not be ...
- DontDestroyOnLoad(Unity3D开发之五)
Unity中我们从A场景切换到B场景的时候,A场景全部对象都会销毁,但有时候我不须要销毁某些东西. 比方一个简单的游戏的背景音乐,我不须要多次反复创建,多个场景播放这一个即可了.这个时候就须要用到Do ...
- u3d DontDestroyOnLoad多场景问题
using UnityEngine; using System.Collections; public class DontDel : MonoBehaviour { public GameObjec ...
- unity3d中的DontDestroyOnLoad来回切换出现多个实例问题
在用Unity3D开发游戏中,我们会经常创建多个场景,但是在场景过度的时候,通常场景中的对象会被删除.所以Unity3D给了我们一个不删除前一个 场景中的某一个对象或者脚本的API,那就是“DontD ...
- Application.LoadLevel & Object.DontDestroyOnLoad
[Application.LoadLevel] 只有在File->Build Setting中设置了的按钮才能被加载. 当level加载完成后,MonoBehaviour.OnLevelWasL ...
- Unity3D研究院之DontDestroyOnLoad的坑
http://www.xuanyusong.com/archives/2938 Unity中的一个方法DontDestroyOnLoad可以让某些游戏对象在切换场景的时候不是施放,听起来是一个非常好的 ...
- unity, 慎用DontDestroyOnLoad
如果想让一个节点切换场景时不销毁,可以为它添加如下脚本: using UnityEngine;using System.Collections; public class dontDestroyOnL ...
- 【转】DontDestroyOnLoad(Unity3D开发之五)
原文 http://blog.csdn.net/cocos2der/article/details/38320773 主题 Unity3D Unity中我们从A场景切换到B场景的时候,A场景所有对象 ...
- 【转】Unity3D研究院之DontDestroyOnLoad的坑
http://www.xuanyusong.com/archives/2938 Unity中的一个方法DontDestroyOnLoad可以让某些游戏对象在切换场景的时候不是施放,听起来是一个非常好的 ...
随机推荐
- java 修饰符的作用一(public protected default private 组)
1.public protected default private 组 public 权限最大,同类,同包,不同包,同包子类父类之间,不同包子类父类之间都可以访问. java 默认的权限是defau ...
- InnoDB这种行锁实现特点意味者:只有通过索引条件检索数据,InnoDB才会使用行级锁,否则,InnoDB将使用表锁!
InnoDB行锁是通过索引上的索引项来实现的,这一点MySQL与Oracle不同,后者是通过在数据中对相应数据行加锁来实现的. InnoDB这种行锁实现特点意味者:只有通过索引条件检索数据,InnoD ...
- java学习之关键字
java语言当中的关键字,之所以存在,是为了告诉编译器如何解释一段有意义的代码段.比如说 /**需求:演示java中关键字存在的含义步骤:用class,public,static,void等说明什么是 ...
- PHP去除空白字符
例子1: <?php $var = " This is a beautiful day!"; // 删除字符串中的所有空白字符(不包括全角空格) $var1 = preg_r ...
- 图论(网络流):COGS 410. [NOI2009] 植物大战僵尸
410. [NOI2009] 植物大战僵尸 ★★★ 输入文件:pvz.in 输出文件:pvz.out 简单对比时间限制:2 s 内存限制:512 MB [问题描述] Plants vs ...
- WEB性能测试:你应该带上VisualStudio2010
原文地址:http://www.16aspx.com/Article/62 在Web性能测试方面,增加了循环(Loops)和条件(Conditions),让开发人员可以为他们的应用程序写出更复杂,更智 ...
- [基础] 重载的时候什么时候用引用&
一般返回值还要继续被处理,而不仅仅是得到其值的时候,返回引用& 一般有[], =, ++, --, 还有输入输出运算符<<, >> Classtype &ope ...
- SRM 398(1-250pt)
题意:有两个变量x和y,三种运算符+,*,-,组成等式"变量 运算符 变量 运算符 变量 运算符 变量",要求每个变量恰好出现两次,且等式的值为val的等式有多少个.注意不计算运算 ...
- DNA Sequence - POJ 2778(AC自动机+矩阵乘法)
题目大意:DNA序列是有 ATGC 组成的,现在知道一些动物的遗传片段有害的,那么如果给出这些有害的片段,能否求出来所有长度为 N 的基因中有多少是不包含这些有害片段的. 分析:也是断断续续做了一 ...
- Android程序的入口点和全局变量设置--application
首先看看 application的官方文档 我之前一直以为Android程序的入口点就是带MAIN和LAUNCHER的Activity的onCreate方法,看来我是错了~ 原来真正的入口点是 Ap ...



