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)原创或者转载,如若转载请务必注明出处,谢谢合作!
  • 相关阅读:
    python之filter()函数
    figure margins too large错误解决
    “一个字等于多少个字节?”是一个不严谨的问法
    如何区分按字节编址与按字编址
    (stm32学习总结)—LCD—液晶显示
    (stm32学习总结)—GPIO位带操作
    (stm32学习总结)—SPI-FLASH 实验
    修改寄存器的位操作方法
    (stm32学习总结)—对寄存器的理解
    (stm32f103学习总结)—初识stm32
  • 原文地址:https://www.cnblogs.com/YinaPan/p/Unity_DontDestroyOnLoad.html
Copyright © 2011-2022 走看看