Создание префаба и использование SceneManager.MoveGameObjectToScene
для перемещения объекта из старой сцены в новую. Из документов:
MoveGameObjectToScene
Перемещает GameObject из текущей сцены в новую сцену.
Вы можете перемещать только корневые объекты GameObjects из одной сцены в другую. Это означает, что
GameObject для перемещения не должен быть дочерним по отношению к любому другому GameObject в его
Сцена. Это работает только при перемещении GameObjects в сцену, которая
уже загружен (добавка). Если вы хотите загрузить отдельные сцены, сделайте
обязательно используйте DontDestroyOnLoad на GameObject, который вы хотели бы переместить
на новую сцену, в противном случае Unity удаляет ее при загрузке новой сцены.
И пример:
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);
}
}