using System; using System.Collections.Generic; using System.Linq; using Duality; using Duality.Resources; using Duality.Components.Renderers; using Duality.Drawing; namespace SceneTransitions { /// /// This static class provides functions related to scenes, such as switching, /// dispose then switch, scene reloading and scene saving. /// public static class SceneSwitcher { // We are going to use ContentRefs instead of using Scene Resources directly /// /// Function to switch to another scene. /// /// The ContentRef of the scene to switch to. public static void Switch(ContentRef scene) { // Note that we are not doing any scene disposal here. This means that // the current scene will not be removed from memory, and that it will // retain changes made to it. Scene.SwitchTo(scene); } /// /// Function to switch to another scene after disposing the specified scene. /// /// The ContentRef of the scene to dispose. /// The ContentRef of the scene to switch to. public static void DisposeAndSwitch(ContentRef disposeScene, ContentRef nextScene) { // In this function, the current scene will be disposed, or removed // from memory, before the switch to the next scene commences. // We are using DisposeLater() for safety, it will only dispose the // scene after the current update cycle is over. disposeScene.Res.DisposeLater(); Scene.SwitchTo(nextScene); } /// /// Function to reload the current scene. /// public static void Reload() { Scene.Reload(); } /// /// Function to save a copy of the specified scene. /// /// The scene to be saved. public static void SaveSceneCopy(ContentRef scene) { // This is the path to which the file will be saved. It is constructed by // combining the Duality Data directory path with the sample name, which // results in the actual directory the file will be saved to. // This result is concatenated with the actual file name. // The file name is constructed by concatenating the specified scene's name, // along with "_Copy" and the Scene Resource file extension. string filePath = Duality.IO.PathOp.Combine(DualityApp.DataDirectory, "SceneTransitions") + (scene.Name + "_Copy" + Resource.GetFileExtByType()); // Here we save the scene. scene.Res.Save(filePath); // The "Press to save" object's TextRenderer. TextRenderer textRenderer = scene.Res.FindGameObject("Text5").GetComponent(); // Set the "Press to save" object's TextRenderer's source text and color tint, // if the TextRenderer was found. if (textRenderer != null) { textRenderer.Text.ApplySource("The saved scene can be found in the Duality Data directory."); textRenderer.ColorTint = ColorRgba.Green; } } } }