更改场景时的Unity重新运行启动功能

更改场景时的Unity重新运行启动功能

本文介绍了更改场景时的Unity重新运行启动功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定我能很好地解释这一点.我有一个不要破坏加载脚本,因此可以在两个场景之间移动.但是,在一个场景(最初创建的一个场景)中,每次绘制该UI时,我需要它在每次重新进入该场景时都运行start函数.这是供参考的代码:

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:

我可以尝试将其放入新脚本中,但是我担心,由于我每周只在几个小时内从事此项目,因此会有一些代码我忘了适应此更改,并且它不会更长的工作.如何重新调用启动功能,或执行类似的操作?

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);
        }

    }

推荐答案

除了可以在Start中进行操作之外,您还可以添加 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之前添加回调(因此在AwakeOnEnable中),也会在第一个场景中调用该示例.

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.path 而不是scene.name,因为path总是唯一(由于OS文件系统),而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.

这篇关于更改场景时的Unity重新运行启动功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:31