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可以让某些游戏对象在切换场景的时候不是施放,听起来是一个非常好的 ...
随机推荐
- windows 10家庭版升级到专业版
因为要搭建一个服务器,需要用到Docker,根据Docker的文档,Docker必须要安装在windows 10 企业版,专业版,或者教育版上.不然不能使用.一直以为要重新下载专业版的镜像重新安装wi ...
- BZOJ1621: [Usaco2008 Open]Roads Around The Farm分岔路口
1621: [Usaco2008 Open]Roads Around The Farm分岔路口 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 521 S ...
- 嵌入式 linux 查看内存
在Windows系统中查看内存的使用情况很简单,想必大家都已经耳熟能详了,那么在linux系统如何查看内存使用情况呢?下面和大家分享在Linux下查看内存使用情况的free命令: [root@scs- ...
- 数据结构(线段树):CodeForces 145E Lucky Queries
E. Lucky Queries time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- bzoj1208 [HNOI2004]宠物收养所(STL,Treap)
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5956 Solved: 2317[Submit][Sta ...
- bzoj1251 序列终结者(Splay Tree+懒惰标记)
Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...
- 尚学堂 JAVA DAY12 java程序执行时内存的分配
- SwingConsole
Java的Swing默认不是线程安全的,类的调度应该由线程分派器来安排.如果每个类都各个各的调度,有可能造成线程紊乱,带来一些难以检测的错误. 对于编写实验性代码(每次都只有一个JFrame),如果每 ...
- JavaScript之作用域与作用域链
今天是2016的第一天,我们得扬帆起航踏上新的征程了.此篇阐述JavaScript中很重要的几个概念:作用域与作用域链及相关知识点. 我们先从变量与作用域的行为关系开始讨论. 变量作用域 JavaSc ...
- Linux下多任务间通信和同步-概述
Linux下多任务间通信和同步-概述 嵌入式开发交流群280352802,欢迎加入! 在前面,我们学习了两种多任务的实现手段:进程和线程.由于进程是工作在独立的内存空间中,不同的进程间不能直接访问到对 ...



