【问题标题】:In Unity, I want to change the scene and then change the text displayed, but the scene change always happens last在Unity中,我想改变场景,然后改变显示的文字,但场景改变总是最后发生
【发布时间】:2022-01-28 02:59:36
【问题描述】:

我是初学者,这是我在这里的第一篇文章,如果我能做得更好,请告诉我。

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

    }

}

当我运行此代码时,文本“AAAAA”会闪烁一秒钟,然后消失。通过测试我发现这是由于某些原因首先显示的消息,并且只有在场景发生变化从而重置文本之后才会显示。

我尝试使用协程来延迟 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
   }

但是协程永远不会超过 yield 语句。它只是在 yield 语句处停止并且不会继续(甚至 Debug.Log("Done") 也没有被记录)。

但是当我尝试切换一些东西并将 SceneManager.LoadScene("Character Select Menu") 放在 yield 语句下方时,它执行得很好,以及下面的调试语句。我不知道为什么会这样,而且很困惑。

这本来是一个非常简单的 10 分钟任务,我浪费了好几天试图弄清楚现在该做什么。任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: c# visual-studio unity3d photon


    【解决方案1】:

    当切换场景时,运行协程的对象被破坏。这导致它永远不会执行协程中的延迟代码,这也适用于加载与当前加载的相同场景时。 Unity 重新加载整个场景,它重置了一切! 这也是您看到文字闪烁几秒钟的原因。您在场景中设置某些组件的文本,然后统一重新加载它并重置它。 SceneManager.LoadScene 不是即时的,它只是告诉 unity 开始加载场景,然后在准备好时切换到它。 顺便说一句,这不是一件非常高效的事情,只有在您需要真正重置它时才应该这样做。

    如果您想将诸如导致从一个场景断开的原因传递到另一个场景,您应该使用静态变量。 这些不存储在对象中(在场景加载时被删除)。它们是静态的,在切换场景时会被保存。

    如果您能分享一些有关项目结构的更多详细信息,将会有所帮助。这可以帮助我们找到适合您需求的解决方案。

    【讨论】:

    • 啊,我明白了,非常感谢。我决定改变我现在的处理方式,但是这段代码应该做的是
      1)转到字符选择菜单
      2)向用户显示错误消息(在这种情况只是'AAAA')
      问题是消息'AAAAA'在消失之前只显示片刻,多亏了你的解释,我现在明白为什么了。我该如何纠正呢?我应该把什么设为静态?
    • 您需要创建一个新的静态变量,将断开连接消息状态存储为字符串。然后,您可以在场景中的 Start() 上读取该变量并设置文本渲染器来显示它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    相关资源
    最近更新 更多