Object.DontDestroyOnLoad 加载时不销毁

static function DontDestroyOnLoad (target : Object) : void

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层次将不会被销毁,全部保留下来。

using UnityEngine;

using System.Collections;



public class example : MonoBehaviour {

	void Awake() {

		DontDestroyOnLoad(transform.gameObject);

	}

}
// Make this game object and all its transform children

// survive when loading a new scene.

//当加载新场景的时候,使游戏物体和它所有的transform子物体存活下来

function Awake () {

	DontDestroyOnLoad (transform.gameObject);

}
Page last updated: 2010-12-9