Skip to content

Instantly share code, notes, and snippets.

@KzoNag KzoNag/SceneSetupObject.cs
Last active Mar 11, 2016

Embed
What would you like to do?
[Unity][マルチシーン]シーン構成保存・復元エディタスクリプト
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;
using UnityEditor.SceneManagement;
using System.Linq;
using System.Collections;
public static class SceneSetupTool
{
[MenuItem("Tool/Scene/Save Current Setup")]
public static void SaveSetup()
{
// 現在のシーン状態インスタンスを生成
var obj = SceneSetupObject.CreateCurrent();
// 保存
AssetDatabase.CreateAsset(obj, "Assets/SceneSetup.asset");
}
[MenuItem("Tool/Scene/Load Setup")]
public static void LoadSetup()
{
// シーン状態情報を読み込み
var obj = AssetDatabase.LoadAssetAtPath<SceneSetupObject>("Assets/SceneSetup.asset");
// それをもとにシーン再構築
EditorSceneManager.RestoreSceneManagerSetup(obj.GetSceneManagerSetup());
}
}
/// <summary>
/// シーンのロード状態保持アセットクラス
/// </summary>
public class SceneSetupObject : ScriptableObject
{
/// <summary>
/// SceneSetupクラスをシリアライズするためだけのクラス
/// SceneSetup自体がSerializableであれば不要
/// </summary>
[System.Serializable]
public class SerializableSetup
{
public string path;
public bool isActive;
public bool isLoaded;
}
/// <summary>
/// シリアライズされるSceneSetup配列
/// </summary>
[SerializeField]
private SerializableSetup[] serializables;
/// <summary>
/// シリアライズ情報からSceneSetup配列を取得
/// </summary>
public SceneSetup[] GetSceneManagerSetup()
{
if (serializables == null)
{
return null;
}
return serializables
.Select(_info => SerializableToSetup(_info))
.ToArray();
}
/// <summary>
/// 現在のシーンロード状態を保持するインスタンスを生成
/// </summary>
public static SceneSetupObject CreateCurrent()
{
var setups = EditorSceneManager.GetSceneManagerSetup();
var obj = ScriptableObject.CreateInstance<SceneSetupObject>();
obj.serializables = setups
.Select(_setup => SetupToSerializable(_setup))
.ToArray();
return obj;
}
private static SceneSetup SerializableToSetup(SerializableSetup info)
{
return new SceneSetup()
{
path = info.path,
isActive = info.isActive,
isLoaded = info.isLoaded
};
}
private static SerializableSetup SetupToSerializable(SceneSetup setup)
{
return new SerializableSetup()
{
path = setup.path,
isActive = setup.isActive,
isLoaded = setup.isLoaded
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.