Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

c# - Move/Transfer GameObject to another scene

We tryed different ways to move UI-object to another scene, but we failed. Over object is in Canvas.

Method 1: we used LoadLevelAdditive, but there moved all objects from first scene without over Canvas with it's elements.

Method 2: we used DontDestroyOnLoad. We need to change our element on Canvas. DDOL save last position on the scene, but we can't change object at all.

Can you get some advice, please?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't use Application.LoadLevelXXX. These are deprecated functions. If you are using old version of Unity, please update it otherwise, you may not be able to use the solution below.

First, load scene with SceneManager.LoadSceneAsync. Set allowSceneActivation to false so that the scene won't activate automatically after loading.

The main solution to your problem is the SceneManager.MoveGameObjectToScene function which is used to transfer GameObject from one scene to another. Call that after loading the scene then call SceneManager.SetActiveScene to activate the scene. Below is an example of that.

public GameObject UIRootObject;
private AsyncOperation sceneAsync;

void Start()
{
    StartCoroutine(loadScene(2));
}

IEnumerator loadScene(int index)
{
    AsyncOperation scene = SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive);
    scene.allowSceneActivation = false;
    sceneAsync = scene;

    //Wait until we are done loading the scene
    while (scene.progress < 0.9f)
    {
        Debug.Log("Loading scene " + " [][] Progress: " + scene.progress);
        yield return null;
    }
    OnFinishedLoadingAllScene();
}

void enableScene(int index)
{
    //Activate the Scene
    sceneAsync.allowSceneActivation = true;


    Scene sceneToLoad = SceneManager.GetSceneByBuildIndex(index);
    if (sceneToLoad.IsValid())
    {
        Debug.Log("Scene is Valid");
        SceneManager.MoveGameObjectToScene(UIRootObject, sceneToLoad);
        SceneManager.SetActiveScene(sceneToLoad);
    }
}

void OnFinishedLoadingAllScene()
{
    Debug.Log("Done Loading Scene");
    enableScene(2);
    Debug.Log("Scene Activated!");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...