void FindAndSetupState() { // Start off just picking a sibling component // if present currentAppState = GetComponent <AppState>(); // Then find all in the scene AppState[] statesInScene = FindObjectsOfType <AppState>(); // If a state is found that is not the sibling // prioritize that one for (int i = 0; i < statesInScene.Length; ++i) { var state = statesInScene[i]; if (state != currentAppState) { currentAppState = state; break; } } if (currentAppState == null) { Dbg.LogError(this, "APP: Unable to find an AppState in scene or as sibling component"); return; } Dbg.Log(this, "APP: Starting with state {0}", currentAppState); if (currentAppState.transform != transform) { currentAppState.transform.parent = transform; } PreinitializeState(currentAppState); currentAppState.AtSetup(); }
IEnumerator OnSceneLoadedRoutine() { Dbg.LogRelease(this, "APP: Scene loaded"); while (!readyForSceneLoad) { yield return(null); } Scene activeScene = SceneManager.GetActiveScene(); if (activeScene.name == splashSceneName) { Dbg.LogRelease(this, "APP: Scene was splash"); // Null here already because after this we're done setupRoutine = null; for (int buildIndex = 0; buildIndex < SceneManager.sceneCountInBuildSettings; ++buildIndex) { string sceneName = Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(buildIndex)); bool validSceneName = !string.IsNullOrEmpty(sceneName) && sceneName != loadingSceneName && sceneName != splashSceneName ; if (validSceneName) { Dbg.LogRelease(this, "APP: Found first scene \"{0}\"", sceneName); LoadScene(sceneName); yield break; } } Dbg.LogErrorRelease(this, "APP: Did not find a first scene to load"); yield break; } Dbg.LogRelease(this, "APP: Disabling Gravity"); Vector3 gravityToRestore = Physics.gravity; Physics.gravity = Vector3.zero; AppState appState = FindAndInitializeAppState(); if (appState != null) { while (!appState.readyForSetup) { yield return(null); } currentAppState = appState; try { currentAppState.AtSetup(); } catch (Exception exc) { Dbg.LogExcRelease(exc); Dbg.LogErrorRelease(this, "APP: State setup failed"); UE.Debug.Break(); } } Dbg.LogRelease(this, "APP: Restoring Gravity"); Physics.gravity = gravityToRestore; setupRoutine = null; }