Legacy Documentation: Version 2018.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

SceneManager.MoveGameObjectToScene

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static method MoveGameObjectToScene(go: GameObject, scene: SceneManagement.Scene): void;
public static void MoveGameObjectToScene(GameObject go, SceneManagement.Scene scene);

Parameters

goGameObject to move.
sceneScene to move into.

Description

Move a GameObject from its current Scene to a new Scene.

You can only move root GameObjects from one Scene to another. This means the GameObject to move must not be a child of any other GameObject in its Scene. This only works on GameObjects being moved to a Scene that is already loaded (additive). If you want to load single Scenes, make sure to use DontDestroyOnLoad on the GameObject you would like to move to a new Scene, otherwise Unity deletes it when it loads a new Scene.

#pragma strict
// This script moves the GameObject you attach in the Inspector to a Scene you specify in the Inspector.
// Attach this script to an empty GameObject.
// Click on the GameObject, go to its Inspector and type the name of the Scene you would like to load in the Scene field.
// Attach the GameObject you would like to move to a new Scene in the "My Game Object" field
// Make sure your Scenes are in your build (File>Build Settings).
public class Example extends MonoBehaviour {
	public var m_Scene: String;
	public var m_MyGameObject: GameObject;
	function Update() {
		// Press the space key to add the Scene additively and move the GameObject to that Scene
		if (Input.GetKeyDown(KeyCode.Space)) {
			StartCoroutine(LoadYourAsyncScene());
		}
	}
	function LoadYourAsyncScene() {
		// Set the current Scene to be able to unload it later
		var currentScene: Scene = SceneManager.GetActiveScene();
		// The Application loads the Scene in the background at the same time as the current Scene.
		var asyncLoad: AsyncOperation = SceneManager.LoadSceneAsync(m_Scene, LoadSceneMode.Additive);
		while ( !asyncLoad.isDone ) {
			null}
		// Move the GameObject (you attach this in the Inspector) to the newly loaded Scene
		SceneManager.MoveGameObjectToScene(m_MyGameObject, SceneManager.GetSceneByName(m_Scene));
		// Unload the previous Scene
		SceneManager.UnloadSceneAsync(currentScene);
	}
}
// This script moves the GameObject you attach in the Inspector to a Scene you specify in the Inspector.
// Attach this script to an empty GameObject.
// Click on the GameObject, go to its Inspector and type the name of the Scene you would like to load in the Scene field.
// Attach the GameObject you would like to move to a new Scene in the "My Game Object" field

// Make sure your Scenes are in your build (File>Build Settings).

using System.Collections; using UnityEngine; using UnityEngine.SceneManagement;

public class Example : MonoBehaviour { // Type in the name of the Scene you would like to load in the Inspector public string m_Scene;

// Assign your GameObject you want to move Scene in the Inspector public GameObject m_MyGameObject;

void Update() { // Press the space key to add the Scene additively and move the GameObject to that Scene if (Input.GetKeyDown(KeyCode.Space)) { StartCoroutine(LoadYourAsyncScene()); } }

IEnumerator LoadYourAsyncScene() { // Set the current Scene to be able to unload it later Scene currentScene = SceneManager.GetActiveScene();

// The Application loads the Scene in the background at the same time as the current Scene. AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(m_Scene, LoadSceneMode.Additive);

// Wait until the last operation fully loads to return anything while (!asyncLoad.isDone) { yield return null; }

// Move the GameObject (you attach this in the Inspector) to the newly loaded Scene SceneManager.MoveGameObjectToScene(m_MyGameObject, SceneManager.GetSceneByName(m_Scene)); // Unload the previous Scene SceneManager.UnloadSceneAsync(currentScene); } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答