本文由博主(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. .net ref关键字在引用类型上的使用

    只接上干货. namespace ConsoleApplication1 { class Person { public string UserName { get; set; } } class P ...

  2. 基于Node.js的强大爬虫 能直接发布抓取的文章哦

    基于Node.js的强大爬虫 能直接发布抓取的文章哦 基于Node.js的强大爬虫能直接发布抓取的文章哦!本爬虫源码基于WTFPL协议,感兴趣的小伙伴们可以参考一下 一.环境配置 1)搞一台服务器,什 ...

  3. ubuntu下新建VPN连接

    1. 安装VPN Client#sudo apt-get install pptp-linux2. 安装网络管理器对VPN的支持#sudo apt-get install network-manage ...

  4. 【转】Android 菜单(OptionMenu)大全 建立你自己的菜单--不错

    原文网址:http://www.cnblogs.com/salam/archive/2011/04/04/2005329.html 菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中, ...

  5. 【数学规律】Vijos P1582 笨笨的L阵游戏

    题目链接: https://vijos.org/p/1582 题目大意: 就是o(o<=50)个人在n*m(n,m<=2000)的格子上放L型的东西(有点像俄罗斯方块的L,可对称旋转),问 ...

  6. HDOJ(HDU) 2123 An easy problem(简单题...)

    Problem Description In this problem you need to make a multiply table of N * N ,just like the sample ...

  7. JavaScript 操作 DOM 常用 API 总结

    文本整理了javascript操作DOM的一些常用的api,根据其作用整理成为创建,修改,查询等多种类型的api,主要用于复习基础知识,加深对原生js的认识. 基本概念 在讲解操作DOM的api之前, ...

  8. 优化exp/imp导入导出速度大全

    .exp  个相关的参数:DIRECT和RECORDLENGTH参数. DIRECT参数定义了导出是使用直接路径方式(DIRECT=Y),还是常规路径方式(DIRECT=N).常规路径导出使用SQL ...

  9. 百度云推送的Java实现

    推送现在基本APP都有,项目中要通知和消息,所以综合考虑用了百度云推送 Java实现步骤: 1. 下载 http://push.baidu.com/sdk/push_server_sdk_for_ja ...

  10. strus2与spring3 mvc的差别

    比较了一下strus2与spring3 mvc的差别: spring3 mvc是方法级别的拦截,拦截到方法后根据参数上的注解,把request数据注入进去,在spring3mvc中,一个方法对应一个r ...