Created
October 15, 2022 12:38
-
-
Save kyubuns/bc71092fcef01b2b849ae79ce921e1cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Text; | |
using Cysharp.Threading.Tasks; | |
using Unity.RuntimeSceneSerialization; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
using UnityEngine.UI; | |
public class Sandbox : MonoBehaviour | |
{ | |
private Scene _bootScene; | |
private Scene _gameScene; | |
public void Start() | |
{ | |
Debug.Log("Sandbox.Start"); | |
_bootScene = SceneManager.GetActiveScene(); | |
_gameScene = SceneManager.LoadScene("Game", new LoadSceneParameters(LoadSceneMode.Additive)); | |
} | |
public void Update() | |
{ | |
void SaveOrLoad(int index) | |
{ | |
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) Save(index).Forget(); | |
else Load(index).Forget(); | |
} | |
if (Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1)) SaveOrLoad(1); | |
if (Input.GetKeyDown(KeyCode.Alpha2) || Input.GetKeyDown(KeyCode.Keypad2)) SaveOrLoad(2); | |
if (Input.GetKeyDown(KeyCode.Alpha3) || Input.GetKeyDown(KeyCode.Keypad3)) SaveOrLoad(3); | |
if (Input.GetKeyDown(KeyCode.Alpha4) || Input.GetKeyDown(KeyCode.Keypad4)) SaveOrLoad(4); | |
} | |
public void OnClickSave(int index) => Save(index).Forget(); | |
public void OnClickLoad(int index) => Load(index).Forget(); | |
private static string GetPath(int index) => $"Saved/{index}.json"; | |
private static AssetPack GetAssetPack() | |
{ | |
var assetPack = AssetDatabase.LoadAssetAtPath<AssetPack>("Assets/Scenes/Game.asset"); | |
if (assetPack == null) throw new Exception("assetPack == null"); | |
return assetPack; | |
} | |
private async UniTaskVoid Save(int index) | |
{ | |
Debug.Log($"Save{index}"); | |
var serializedString = SceneSerialization.SerializeScene(_gameScene, new SerializedRenderSettings(), GetAssetPack()); | |
await File.WriteAllTextAsync(GetPath(index), serializedString, Encoding.UTF8); | |
Debug.Log($"Complete!"); | |
} | |
private async UniTaskVoid Load(int index) | |
{ | |
Debug.Log($"UnloadScene"); | |
await SceneManager.UnloadSceneAsync(_gameScene); | |
await SceneManager.LoadSceneAsync("Load", new LoadSceneParameters(LoadSceneMode.Additive)); | |
_gameScene = SceneManager.GetSceneAt(1); | |
SceneManager.SetActiveScene(_gameScene); | |
Debug.Log($"Load{index}"); | |
var serializedString = await File.ReadAllTextAsync(GetPath(index), Encoding.UTF8); | |
SceneSerialization.ImportScene(serializedString, GetAssetPack()); | |
Debug.Log($"Complete!"); | |
SceneManager.SetActiveScene(_bootScene); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment