本文由博主(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的更多相关文章

  1. Unity3D Object.DontDestroyOnLoad 备忘

    初学Untiy3D,记录备忘. public static void DontDestroyOnLoad(Object target); Makes the object target not be ...

  2. DontDestroyOnLoad(Unity3D开发之五)

    Unity中我们从A场景切换到B场景的时候,A场景全部对象都会销毁,但有时候我不须要销毁某些东西. 比方一个简单的游戏的背景音乐,我不须要多次反复创建,多个场景播放这一个即可了.这个时候就须要用到Do ...

  3. u3d DontDestroyOnLoad多场景问题

    using UnityEngine; using System.Collections; public class DontDel : MonoBehaviour { public GameObjec ...

  4. unity3d中的DontDestroyOnLoad来回切换出现多个实例问题

    在用Unity3D开发游戏中,我们会经常创建多个场景,但是在场景过度的时候,通常场景中的对象会被删除.所以Unity3D给了我们一个不删除前一个 场景中的某一个对象或者脚本的API,那就是“DontD ...

  5. Application.LoadLevel & Object.DontDestroyOnLoad

    [Application.LoadLevel] 只有在File->Build Setting中设置了的按钮才能被加载. 当level加载完成后,MonoBehaviour.OnLevelWasL ...

  6. Unity3D研究院之DontDestroyOnLoad的坑

    http://www.xuanyusong.com/archives/2938 Unity中的一个方法DontDestroyOnLoad可以让某些游戏对象在切换场景的时候不是施放,听起来是一个非常好的 ...

  7. unity, 慎用DontDestroyOnLoad

    如果想让一个节点切换场景时不销毁,可以为它添加如下脚本: using UnityEngine;using System.Collections; public class dontDestroyOnL ...

  8. 【转】DontDestroyOnLoad(Unity3D开发之五)

    原文  http://blog.csdn.net/cocos2der/article/details/38320773 主题 Unity3D Unity中我们从A场景切换到B场景的时候,A场景所有对象 ...

  9. 【转】Unity3D研究院之DontDestroyOnLoad的坑

    http://www.xuanyusong.com/archives/2938 Unity中的一个方法DontDestroyOnLoad可以让某些游戏对象在切换场景的时候不是施放,听起来是一个非常好的 ...

随机推荐

  1. 【spring-boot】spring aop 面向切面编程初接触--切点表达式

    众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...

  2. 【Java&Android开源库代码剖析】のandroid-smart-image-view

    Android应用开发已经进入到相对成熟的阶段,特别在国外,涌现出了各式各样的成熟稳定的开源库,供普通开发者使用.这种情况虽然极大加速了app开发的进程,但同时带来的问题是大多数普通开发者在使用这些开 ...

  3. poj 1265 Area(pick定理)

    Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4373 Accepted: 1983 Description Bein ...

  4. 【转】BLE 学习记录

    原文网址:http://m.blog.csdn.net/blog/chiooo/43985401 BLE 学习记录 ANROID BLE 开发,基于 bluetoothlegatt 分析 mBluet ...

  5. SQLCMD的用法,使用CMD 执行sql语句

    SQLCMD的用法,使用CMD 执行sql语句 SQLCMD 允许在Windows命令窗中通过命令行提示符运行脚本. 语法如下: sqlcmd  [  { { -U <login id> ...

  6. 数据结构典型算法的VC实现(袁辉勇)

    1. 迷宫问题求解 #include <stdio.h> #define m 8 //迷宫内有8列 #define n 8 //迷宫内有8行 #define MAXSIZE 100//栈尺 ...

  7. #345 div2 D. Image Preview

    Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed ...

  8. HDU 1203 I NEED A OFFER! 01背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 解题思路:简单的01背包,用dp[i]表示花费不超过i时的最大可能性 状态转移方程 dp[i]= ...

  9. Postman 安装 & 资料

    安装 下载地址: http://chromecj.com/web-development/2014-09/60/download.html 怎么在谷歌浏览器中安装.crx扩展名的离线Chrome插件? ...

  10. Servlet的一些细节(2)

    1. Servlet的创建时间 Servlet是不能单独运行,调用它的叫做Servlet引擎,或者叫做web服务器 针对客户端的多长Servlet请求,通常情况下,服务器只会创建一个Servlet实例 ...