go | 要移动的游戏对象。 |
scene | 要移入游戏对象的场景。 |
将游戏对象从当前场景移至新场景。
您可以仅将根游戏对象从一个场景移到另一个场景。这意味着要移动的游戏对象不得是同一场景中任何其他游戏对象的子项。 仅在游戏对象要移动到某个已加载的场景中时,才可以执行此操作(叠加式)。 如果您想加载单个场景,请确保使用您想移至新场景的游戏对象上的 DontDestroyOnLoad,否则 Unity 会在游戏对象移至新场景时将其删除。
// 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); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.