簡體   English   中英

SceneManager.LoadScene 不適用於開發移動構建

[英]SceneManager.LoadScene doesn't work on development mobile build

我正在嘗試通過檢查開發構建選項(以便我可以使用分析器進行調試)在 android/iOS 上構建 Unity 游戲,但是 SceneManager.LoadScene 函數不起作用。 我嘗試將場景名稱和索引作為函數參數,但都不起作用。

重要的是要注意,如果我取消選中開發構建選項,游戲就可以正常運行。 我目前使用的是 Unity 2019.3.5.f1。

在此處輸入圖片說明

編輯:
我注意到當我從Start()函數運行SceneManager.LoadScene時,場景加載沒有任何問題。 但是,在我的大部分代碼中,我從其他函數(例如事件處理程序)內部運行SceneManager.LoadScene

下面是一些代碼示例:

  1. 我在Awake()運行這個函數。 它到達SceneManager.LoadScene("Credentials"); 然后停止:
private void ConfirmGooglePlayerServicesRequirements()
    {
        Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
            var dependencyStatus = task.Result;
            if (dependencyStatus == Firebase.DependencyStatus.Available)
            {
                // Create and hold a reference to your FirebaseApp,
                // where app is a Firebase.FirebaseApp property of your application class.
                app = Firebase.FirebaseApp.DefaultInstance;

                InitializeFirebase();

                if (!signedIn)
                    SceneManager.LoadScene("Credentials");

            }
            else
            {
                Debug.LogError(System.String.Format(
                  "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
                // Firebase Unity SDK is not safe to use here.
            }
        });
    }
  1. 這是一個 Firebase 函數,每當用戶登錄/退出時都會調用它。 它調用一個名為signedInEvent的事件,該事件由另一個運行SceneManager.LoadScene腳本中的函數處理。
void AuthStateChanged(object sender, System.EventArgs eventArgs)
    {
        if (auth.CurrentUser != user)
        {
            signedIn = user != auth.CurrentUser && auth.CurrentUser != null;
            if (!signedIn && user != null)
            {
                Debug.Log("Signed out " + user.UserId);
            }
            user = auth.CurrentUser;
            if (signedIn)
            {
                Debug.Log("Signed in " + user.UserId);

                displayName = user.DisplayName ?? "";
                emailAddress = user.Email ?? "";
                userId = user.UserId ?? "";
                signedInEvent.Invoke(displayName, emailAddress, userId);
            }
        }
    }

筆記:

  • Scenes/FirstScene 已啟用。 當我截取上面的截圖時它被禁用了,因為我正在做一些測試。
  • 不久前,當我在開發模式下運行游戲時,我使用 adb(android 調試橋)遇到了這個錯誤。 可能與這個問題有關: FindGameObjectWithTag can only be called from the main thread

謝謝!

FindGameObjectWithTag 只能從主線程調用

這確實有關系! 現在你發布了你的代碼:

ContinueWith可能會在后台線程上被調用。 大多數 Unity API 可能只能從主線程調用,否則它可能會顯示諸如您提到的錯誤,或者有時它只是默默地失敗。


一般來說,這通常通過諸如UnityMainThreadDispatcher 之類的東西來解決。

但是,特別是對於 Firebase,在Firebase.Extensions命名空間中已經存在一個名為ContinueWithOnMainThread的擴展,它確保回調代碼實際在 Unity API 工作的主線程中執行。

System.Threading.Tasks.TaskSystem.Threading.Tasks.Task<T>擴展方法,允許在 Unity 的主線程上執行延續函數。

using Firebase.Extensions;

private void ConfirmGooglePlayerServicesRequirements()
{
    Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
        var dependencyStatus = task.Result;
        if (dependencyStatus == Firebase.DependencyStatus.Available)
        {
            // Create and hold a reference to your FirebaseApp,
            // where app is a Firebase.FirebaseApp property of your application class.
            app = Firebase.FirebaseApp.DefaultInstance;

            InitializeFirebase();

            if (!signedIn)
            {
                SceneManager.LoadScene("Credentials");
            }
        }
        else
        {
            Debug.LogError($"Could not resolve all Firebase dependencies: {dependencyStatus}", this);
            // Firebase Unity SDK is not safe to use here.
        }
    });
}

暫無
暫無

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

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