当场景改变时如何运行一些代码? [英] How can I run some code when the scene changes?

查看:21
本文介绍了当场景改变时如何运行一些代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道我能解释得有多好.我有一个 do not destroy on load 脚本,所以它可以在两个场景之间移动.但是,在一个场景(它最初创建的场景)中,每次重新进入该场景时,我都需要它运行 start 函数,因为它绘制了我的一些 UI.下面是代码供参考:

Not sure how well I can explain this. I have a a do not destroy on load script, so it can be moved between two scenes. However, in one scene (the one it's originally created) I need it to run the start function every time it re-enters this scene, as it draws out some of my UI. Here is the code for reference:

我可以尝试把它放到一个新的脚本中,但我担心因为我每周只在这个项目上工作几个小时,我会忘记适应这个变化的一些代码,它不会更长的工作.我怎样才能重新调用 start 函数,或者做类似的事情?

I could try to put it into a new script, but I am worried that since I only work on this project a few hours a week, there will be bits of code I forget to adapt for this change, and it will no longer work. How can I re-call the start function, or do something similar?

int spriteIndex = 0;
    foreach (Sprite texture in spriteImages) {

        GameObject button = Instantiate (shopButtonPrefab) as GameObject;
        Image buttonImage = button.GetComponent<Image> ();
        Image[] images = button.GetComponentsInChildren<Image>();

        int newIndex = spriteIndex;
        button.GetComponent<Button> ().onClick.AddListener (() => ChangePlayerSkin (newIndex));
        spriteIndex++;
        foreach (Image image in images) {

            if (image != buttonImage) {
                //button.GetComponentInChildren<Image>().sprite = texture;
                //button.transform.SetParent (shopButtonContrainer.transform, false);
                image.sprite = texture;


                break;
            }
            button.transform.SetParent (shopButtonContrainer.transform, false);
        }

    }

推荐答案

您可以为 SceneManager.sceneLoaded

Instead of doing it in Start you can add a listener for SceneManager.sceneLoaded

只有在加载初始场景时才执行这些操作,您可以使用 SceneManager.GetActiveScene() 用于存储并稍后将初始场景与加载的场景进行比较.

For doing the stuff only if the initial scene is loaded you can use SceneManager.GetActiveScene() to store and later compare the initial scene to the loaded scene.

// Store the scene that should trigger start
private Scene scene;

private void Awake()
{
    // It is save to remove listeners even if they
    // didn't exist so far.
    // This makes sure it is added only once
    SceneManager.sceneLoaded -= OnsceneLoaded;

    // Add the listener to be called when a scene is loaded
    SceneManager.sceneLoaded += OnSceneLoaded;

    DontDestroyOnLoad(gameObject);

    // Store the creating scene as the scene to trigger start
    scene = SceneManager.GetActiveScene();
}

private void OnDestroy()
{
    // Always clean up your listeners when not needed anymore
    SceneManager.sceneLoaded -= OnSceneLoaded;
}

// Listener for sceneLoaded
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
    // return if not the start calling scene
    if(!string.Equals(scene.path, this.scene.path) return;

    Debug.Log("Re-Initializing", this);
    // do your "Start" stuff here
}

Afaik/我如何理解链接中的示例 OnSceneLoaded 也将在第一个场景中调用,只要您在 Start 之前添加回调(所以在 >唤醒OnEnable).

Afaik / How I understand the example in the link OnSceneLoaded will also be called in the first scene as long a you add the callback before Start (so in Awake or OnEnable).

注意我使用了 Scene.paths 而不是 scene.name 因为 path 总是唯一的(由于操作系统文件系统)而 name 可能不是.

Note I used the Scene.paths instead of scene.name because the path is always unique (due to the OS filesystem) while the name might not be.

这篇关于当场景改变时如何运行一些代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆