[英]SceneManager.LoadScene(index) doesn't work when called in an async method. unity3d
[英]SceneManager.LoadScene doesn't work on development mobile build
我正在嘗試通過檢查開發構建選項(以便我可以使用分析器進行調試)在 android/iOS 上構建 Unity 游戲,但是 SceneManager.LoadScene 函數不起作用。 我嘗試將場景名稱和索引作為函數參數,但都不起作用。
重要的是要注意,如果我取消選中開發構建選項,游戲就可以正常運行。 我目前使用的是 Unity 2019.3.5.f1。
編輯:
我注意到當我從Start()
函數運行SceneManager.LoadScene
時,場景加載沒有任何問題。 但是,在我的大部分代碼中,我從其他函數(例如事件處理程序)內部運行SceneManager.LoadScene
。
下面是一些代碼示例:
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.
}
});
}
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);
}
}
}
筆記:
FindGameObjectWithTag can only be called from the main thread
謝謝!
FindGameObjectWithTag 只能從主線程調用
這確實有關系! 現在你發布了你的代碼:
ContinueWith
可能會在后台線程上被調用。 大多數 Unity API 可能只能從主線程調用,否則它可能會顯示諸如您提到的錯誤,或者有時它只是默默地失敗。
一般來說,這通常通過諸如UnityMainThreadDispatcher 之類的東西來解決。
但是,特別是對於 Firebase,在Firebase.Extensions
命名空間中已經存在一個名為ContinueWithOnMainThread
的擴展,它確保回調代碼實際在 Unity API 工作的主線程中執行。
System.Threading.Tasks.Task
和System.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.