码迷,mamicode.com
首页 > 其他好文 > 详细

DontDestroyOnLoad

时间:2016-01-11 22:01:45      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

public static void DontDestroyOnLoad(Object target);

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 = 2;
            int[] tempInt = new int[2];
            int j = tempInt[2];
        }
        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

标签:

原文地址:http://www.cnblogs.com/panyingying/p/5122572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!