簡體   English   中英

在Unity中,我想改變場景,然后改變顯示的文字,但場景改變總是最后發生

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

我是初學者,這是我在這里的第一篇文章,所以如果我能做得更好,請告訴我。

在 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 分鍾任務,我已經浪費了好幾天試圖弄清楚現在該做什么。 任何幫助將不勝感激。 謝謝!

切換場景時,運行協程的 object 會被破壞。 這導致它永遠不會執行協程中的延遲代碼,這也適用於加載與當前加載的相同場景時。 Unity 會重新加載整個場景,它會重置一切。 這也是為什么您看到文本閃爍幾秒鍾的原因,您在場景中設置了某些組件的文本。 然后統一重新加載它並重置它。 SceneManager.LoadScene 不是即時的,它只是告訴 unity 開始加載場景,然后在准備好時切換到它。 順便說一句,這不是一件非常高效的事情,只有在您需要真正重置它時才應該這樣做。

如果要將值(例如從一個場景斷開連接的原因)傳遞到另一個場景,則應使用 static 變量。 這些不存儲在對象中(在場景加載時被刪除)。 它們是 static 並在切換場景時被保存。

如果您分享有關項目結構的更多詳細信息,將會有所幫助。 這可以幫助我們找到適合您需求的解決方案。

我希望這有幫助。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM