zoukankan      html  css  js  c++  java
  • DontDestroyOnLoad

    本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/Unity_DontDestroyOnLoad.html 

    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
    image
    image
     
    第二种情况,start是没有执行的,只有在OnEnable时,才会在执行Start,且只执行一次
    image
    image
     
     
    本文由博主(YinaPan)原创或者转载,如若转载请务必注明出处,谢谢合作!
  • 相关阅读:
    [Objective-C语言教程]决策结构(10)
    [Objective-C语言教程]循环语句(9)
    [Objective-C语言教程]关系运算符(8)
    [Objective-C语言教程]常量(7)
    [Objective-C语言教程]变量(6)
    [Objective-C语言教程]数据类型(5)
    转 Apache Ant 实现自动化部署
    转 智能化运维最佳实践-自动化
    ANT 操控 ORACLE数据库实践
    转: Ant 脚本的结构化设计
  • 原文地址:https://www.cnblogs.com/YinaPan/p/Unity_DontDestroyOnLoad.html
Copyright © 2011-2022 走看看