Skip to content

Instantly share code, notes, and snippets.

@kyubuns
Created October 15, 2022 12:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyubuns/bc71092fcef01b2b849ae79ce921e1cd to your computer and use it in GitHub Desktop.
Save kyubuns/bc71092fcef01b2b849ae79ce921e1cd to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using Unity.RuntimeSceneSerialization;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
public static class MenuItem
{
[UnityEditor.MenuItem("Sandbox/CreateAssetPack")]
public static void CreateAssetPack()
{
if (Application.isPlaying)
{
Debug.LogError($"Application.isPlaying");
return;
}
Debug.Log("CreateAssetPack");
var activeScene = SceneManager.GetActiveScene();
if (!activeScene.IsValid()) return;
var assetPackPath = "Assets/Scenes/Game.asset";
var assetPack = AssetDatabase.LoadAssetAtPath<AssetPack>(assetPackPath);
var created = false;
if (assetPack == null)
{
created = true;
assetPack = ScriptableObject.CreateInstance<AssetPack>();
}
else
{
assetPack.Clear();
}
var renderSettings = SerializedRenderSettings.CreateFromActiveScene();
var sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(activeScene.path);
if (sceneAsset != null) assetPack.SceneAsset = sceneAsset;
SceneSerialization.SerializeScene(activeScene, renderSettings, assetPack);
if (created)
{
AssetDatabase.CreateAsset(assetPack, assetPackPath);
}
else
{
EditorUtility.SetDirty(assetPack);
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log($"Complete! {assetPack.AssetCount}");
}
}
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