/// <summary> /// If a lighting scene is being used, this ensures that at least one lighting scene is loaded in editor. /// </summary> private void EditorUpdateLightingScene(bool heirarchyDirty) { if (!profile.UseLightingScene || !profile.EditorManageLoadedScenes) { return; } if (string.IsNullOrEmpty(ActiveLightingScene)) { ActiveLightingScene = profile.DefaultLightingScene.Name; } else { foreach (SceneInfo lightingScene in profile.LightingScenes) { if (lightingScene.Name == ActiveLightingScene) { Scene scene; if (EditorSceneUtils.LoadScene(lightingScene, false, out scene)) { EditorSceneUtils.CopyLightingSettingsToActiveScene(scene); if (profile.EditorEnforceLightingSceneTypes && heirarchyDirty) { EditorEnforceLightingSceneTypes(scene); } } if (profile.EditorEnforceSceneOrder) { // If we're enforcing scene order, make sure this scene comes after the current scene Scene currentFirstScene = EditorSceneManager.GetSceneAt(0); EditorSceneManager.MoveSceneAfter(scene, currentFirstScene); } } else { EditorSceneUtils.UnloadScene(lightingScene, true); } } } }
/// <summary> /// Singly loads next content scene (if available) and unloads all other content scenes. /// Useful for inspectors. /// </summary> public void EditorLoadNextContent(bool wrap = false) { string contentSceneName; if (contentTracker.GetNextContent(wrap, out contentSceneName)) { foreach (SceneInfo contentScene in ContentScenes) { if (contentScene.Name == contentSceneName) { EditorSceneUtils.LoadScene(contentScene, false, out Scene scene); } else { EditorSceneUtils.UnloadScene(contentScene, false); } } } contentTracker.RefreshLoadedContent(); }
/// <summary> /// If a manager scene is being used, this loads the scene in editor and ensures that an instance of the MRTK has been added to it. /// </summary> private void EditorUpdateManagerScene() { if (!profile.UseManagerScene || !profile.EditorManageLoadedScenes) { // Nothing to do here. return; } if (EditorSceneUtils.LoadScene(profile.ManagerScene, true, out Scene scene)) { // If we're managing scene hierarchy, move this to the front if (profile.EditorEnforceSceneOrder) { Scene currentFirstScene = EditorSceneManager.GetSceneAt(0); if (currentFirstScene.name != scene.name) { EditorSceneManager.MoveSceneBefore(scene, currentFirstScene); } } if (Time.realtimeSinceStartup > managerSceneInstanceCheckTime) { managerSceneInstanceCheckTime = Time.realtimeSinceStartup + managerSceneInstanceCheckInterval; // Check for an MRTK instance bool foundToolkitInstance = false; try { foreach (GameObject rootGameObject in scene.GetRootGameObjects()) { MixedRealityToolkit instance = rootGameObject.GetComponent <MixedRealityToolkit>(); if (instance != null) { foundToolkitInstance = true; // If we found an instance, and it's not the active instance, we probably want to activate it if (instance != MixedRealityToolkit.Instance) { // The only exception would be if the new instance has a different profile than the current instance // If that's the case, we could end up ping-ponging between two sets of manager scenes if (!instance.HasActiveProfile) { // If it doesn't have a profile, set it to our current profile instance.ActiveProfile = MixedRealityToolkit.Instance.ActiveProfile; } else if (instance.ActiveProfile != MixedRealityToolkit.Instance.ActiveProfile) { Debug.LogWarning("The active profile of the instance in your manager scene is different from the profile that loaded your scene. This is not recommended."); } else { Debug.LogWarning("Setting the manager scene MixedRealityToolkit instance to the active instance."); MixedRealityToolkit.SetActiveInstance(instance); } } break; } } } catch (Exception) { // This can happen if the scene isn't valid // Not an issue - we'll take care of it on the next update. return; } if (!foundToolkitInstance) { GameObject mrtkGo = new GameObject("MixedRealityToolkit"); MixedRealityToolkit toolkitInstance = mrtkGo.AddComponent <MixedRealityToolkit>(); try { SceneManager.MoveGameObjectToScene(mrtkGo, scene); // Set the scene as dirty EditorSceneManager.MarkSceneDirty(scene); } catch (Exception) { // This can happen if the scene isn't valid // Not an issue - we'll take care of it on the next update. // Destroy the new manager GameObject.DestroyImmediate(mrtkGo); return; } MixedRealityToolkit.SetActiveInstance(toolkitInstance); Debug.LogWarning("Didn't find a MixedRealityToolkit instance in your manager scene. Creating one now."); } } } else { Debug.Log("Couldn't load manager scene!"); } }
/// <summary> /// Loads all lighting scenes, extracts their lighting data, then caches that data in the profile. /// </summary> private async Task EditorUpdateCachedLighting() { // Clear out our lighting cache profile.ClearLightingCache(); profile.EditorLightingCacheUpdateRequested = false; SceneInfo defaultLightingScene = profile.DefaultLightingScene; foreach (SceneInfo lightingScene in profile.LightingScenes) { // Load all our lighting scenes Scene scene; EditorSceneUtils.LoadScene(lightingScene, false, out scene); } // Wait for a moment so all loaded scenes have time to get set up await Task.Delay(100); foreach (SceneInfo lightingScene in profile.LightingScenes) { Scene scene; EditorSceneUtils.GetSceneIfLoaded(lightingScene, out scene); EditorSceneUtils.SetActiveScene(scene); SerializedObject lightingSettingsObject; SerializedObject renderSettingsObject; EditorSceneUtils.GetLightingAndRenderSettings(out lightingSettingsObject, out renderSettingsObject); // Copy the serialized objects into new structs RuntimeLightingSettings lightingSettings = default(RuntimeLightingSettings); RuntimeRenderSettings renderSettings = default(RuntimeRenderSettings); RuntimeSunlightSettings sunlightSettings = default(RuntimeSunlightSettings); lightingSettings = SerializedObjectUtils.CopySerializedObjectToStruct <RuntimeLightingSettings>(lightingSettingsObject, lightingSettings, "m_"); renderSettings = SerializedObjectUtils.CopySerializedObjectToStruct <RuntimeRenderSettings>(renderSettingsObject, renderSettings, "m_"); // Extract sunlight settings based on sunlight object SerializedProperty sunProperty = renderSettingsObject.FindProperty("m_Sun"); if (sunProperty == null) { Debug.LogError("Sun settings may not be available in this version of Unity."); } else { Light sunLight = (Light)sunProperty.objectReferenceValue; if (sunLight != null) { sunlightSettings.UseSunlight = true; sunlightSettings.Color = sunLight.color; sunlightSettings.Intensity = sunLight.intensity; Vector3 eulerAngles = sunLight.transform.eulerAngles; sunlightSettings.XRotation = eulerAngles.x; sunlightSettings.YRotation = eulerAngles.y; sunlightSettings.ZRotation = eulerAngles.z; } } profile.SetLightingCache(lightingScene, lightingSettings, renderSettings, sunlightSettings); } }