简体   繁体   English

在Unity中,我想改变场景,然后改变显示的文字,但场景改变总是最后发生

[英]In Unity, I want to change the scene and then change the text displayed, but the scene change always happens last

I'm a beginner and this is my first post here so please let me know if I could do anything better.我是初学者,这是我在这里的第一篇文章,所以如果我能做得更好,请告诉我。

Using PUN 2 in Unity, I'm trying to return a connection error message when the user attempts to connect to a Photon server but fails.在 Unity 中使用 PUN 2,当用户尝试连接到 Photon 服务器但失败时,我试图返回连接错误消息。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; //Library that provides utilities for scene management
using Photon.Pun; //Library for Photon, provides networking utilities 
using Photon.Realtime; //Helps solve problems relating to matchmaking and fast communication
using UnityEngine.UI; // 

public class MenuMan : MonoBehaviourPunCallbacks
{

    public override void OnDisconnected(DisconnectCause cause) //Callback for when device fails to connect to server. Parameter 'cause' is the cause of this failure
    {
        Debug.Log("failed :("); // FOR DEBUGGING 
        Debug.Log(cause); //Prints in the console the cause of this connection failure
        DisplayErrorMessage();
    }

    public Text Message;
    public string MessageValue = " ";



    public void DisplayErrorMessage() //Method that displays a connection error message to the user
    {
        SceneManager.LoadScene("Character Select Menu"); //Ensures user is on the Character Select menu

        MessageValue = "AAAAAAA";
        //Message.text = MessageValue;
        Debug.Log(Message.text);
        Debug.Log(MessageValue);

    }

}

When I run this code, the text "AAAAA" flashes for a second, then disappears.当我运行此代码时,文本“AAAAA”会闪烁一秒钟,然后消失。 Through testing I found out this is because the message displays first for some reason, and only after does the scene change thus resetting the text.通过测试我发现这是因为某些原因首先显示消息,并且只有在场景发生变化从而重置文本之后才会显示。

I tried using coroutines to delay MessageValue from being altered until the scene changed:我尝试使用协程来延迟MessageValue被改变,直到场景改变:

   public override void OnDisconnected(DisconnectCause cause) //Callback for when device fails to connect to server. Parameter 'cause' is the cause of this failure
   {

       StartCoroutine(GoToCSM());
       DisplayErrorMessage();

   }


   IEnumerator GoToCSM()
   {
       Debug.Log("cor started");
       SceneManager.LoadScene("Character Select Menu")
       yield return new WaitForSeconds(3);
       DisplayErrorMessage();
       Debug.Log("Done");

   }


   public Text Message; //Initialises a 'Text' type object, which will be set to the Connection fail message
   static string MessageValue = " "; //Initialises a string which will be written to the 'text' component of the above object

   public void DisplayErrorMessage() //Method that displays a connection error message to the user
   {
       MessageValue = "AAAAAAA"; //Writes the string to be displayed to MessageValue 
       Message.text = MessageValue; //Sets the above text to the 'text' component of the Message object, thus displaying it on the screen
   }

However the coroutine never goes past the yield statement.然而,协程永远不会超过 yield 语句。 It just stops at the yield statement and doesn't continue (even the Debug.Log("Done") doesn't get logged).它只是在 yield 语句处停止并且不会继续(即使 Debug.Log("Done") 也没有被记录)。

But when I tried switching some things round and put SceneManager.LoadScene("Character Select Menu") beneath the yield statement, that was executed just fine, as well as the debug statement below.但是当我尝试切换一些东西并将 SceneManager.LoadScene("Character Select Menu") 放在 yield 语句下方时,执行得很好,以及下面的调试语句。 I have no idea why this could be, and am very confused.我不知道为什么会这样,我很困惑。

This was meant to be an extremely simple 10 minute task and I've wasted days trying to figure out what to do now.这本来是一个非常简单的 10 分钟任务,我已经浪费了好几天试图弄清楚现在该做什么。 Any help would be extremely greatly appreciated.任何帮助将不胜感激。 Thank you!谢谢!

When switching scenes the object running the coroutines gets destroyed.切换场景时,运行协程的 object 会被破坏。 Which results in it never executing the delayed code in the coroutines, this also applies when loading the same scene as you have currently loaded.这导致它永远不会执行协程中的延迟代码,这也适用于加载与当前加载的相同场景时。 Unity reloads the whole scene, it resets everything. Unity 会重新加载整个场景,它会重置一切。 This is also the reason why you see the text flashing for a few seconds, You set the text of some component in your scene.这也是为什么您看到文本闪烁几秒钟的原因,您在场景中设置了某些组件的文本。 then unity reloads it and its reset.然后统一重新加载它并重置它。 SceneManager.LoadScene is not instant it just tells unity to start loading the Scene and then switching to it when ready. SceneManager.LoadScene 不是即时的,它只是告诉 unity 开始加载场景,然后在准备好时切换到它。 Which by the way is not a very performant thing to do and should only be done when you need to really reset it.顺便说一句,这不是一件非常高效的事情,只有在您需要真正重置它时才应该这样做。

If you want to pass values such as cause of disconnection from one scene to another you should use static variables.如果要将值(例如从一个场景断开连接的原因)传递到另一个场景,则应使用 static 变量。 These are not stored in objects(that get deleted on scene load).这些不存储在对象中(在场景加载时被删除)。 They are static and get saved when switching scenes.它们是 static 并在切换场景时被保存。

It would help if you shared some more details about the structure of your project.如果您分享有关项目结构的更多详细信息,将会有所帮助。 That can help us find a solution that suits your needs.这可以帮助我们找到适合您需求的解决方案。

I hope this helps.我希望这有帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM