Last active
December 6, 2019 05:22
-
-
Save nenjiru/24bfeef209e26919e4c4794a6ca44970 to your computer and use it in GitHub Desktop.
This file contains 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 UnityEngine; | |
using UnityEditor; | |
/// <summary> | |
/// このコンポーネントはMainシーンから加算ロードされた場合、非アクティブになる | |
/// </summary> | |
public class PartialSceneObject : MonoBehaviour | |
{ | |
// 単体シーンのロード時に SceneLoader.cs から呼ばれる | |
public void Init() | |
{ | |
} | |
} | |
#if UNITY_EDITOR | |
[CustomEditor(typeof(PartialSceneObject))] | |
[CanEditMultipleObjects] | |
public class PartialSceneObjectEditor : Editor | |
{ | |
public override void OnInspectorGUI() | |
{ | |
base.OnInspectorGUI(); | |
EditorGUILayout.HelpBox("このオブジェクトはMainシーンから加算ロードされた場合、非アクティブになる", MessageType.Info); | |
} | |
} | |
#endif |
This file contains 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.Linq; | |
using UnityEditor; | |
using UnityEditor.SceneManagement; | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
/// <summary> | |
/// シーンローダー | |
/// </summary> | |
[InitializeOnLoad] | |
public static class SceneLeader | |
{ | |
static string currentMainScene = ""; | |
static SceneLeader() | |
{ | |
EditorApplication.update += disablePSObject; | |
EditorSceneManager.sceneOpened += (scene, mode) => | |
{ | |
if (mode == OpenSceneMode.Single) | |
{ | |
// 追加で必要なシーンをロード | |
// switch (scene.name) | |
// { | |
// case "Autumn": addScenes("_Lava"); break; | |
// } | |
// Main以外の単体シーンだった場合 | |
if (scene.name != currentMainScene) | |
{ | |
enablePartialSceneObject(); | |
} | |
} | |
}; | |
} | |
#region PUBLIC_METHODS | |
[MenuItem("Scene/Disable Partial Objects", false, 0)] | |
public static void DisablePartialSceneObjects() { disablePartialSceneObjects(); } | |
[MenuItem("Scene/Main Autumn", false, 20)] | |
public static void OpenScene1() { loadScene("Autumn"); } | |
[MenuItem("Scene/Lava", false, 32)] | |
public static void OpenScene2() { loadScene("_Lava"); } | |
[MenuItem("Scene/Fall", false, 33)] | |
public static void OpenScene3() { loadScene("_Fall"); } | |
[MenuItem("Scene/Flow", false, 34)] | |
public static void OpenScene4() { loadScene("_Flow"); } | |
#endregion | |
#region PRIVATE_METHODS | |
static void loadScene(string scene) | |
{ | |
currentMainScene = (scene[0] != '_') ? scene : ""; | |
EditorSceneManager.OpenScene("Assets/_Project/" + scene + ".unity"); | |
} | |
static void addScenes(params string[] scenes) | |
{ | |
for (int i = 0; i < scenes.Length; i++) | |
{ | |
EditorSceneManager.OpenScene("Assets/_Project/" + scenes[i] + ".unity", OpenSceneMode.Additive); | |
} | |
disablePartialSceneObject(scenes); | |
} | |
static void enablePartialSceneObject() | |
{ | |
GameObject[] gameObjectArray = SceneManager.GetActiveScene().GetRootGameObjects(); | |
foreach (GameObject go in gameObjectArray) | |
{ | |
if (go.GetComponent<PartialSceneObject>() != null) | |
{ | |
go.GetComponent<PartialSceneObject>().Init(); | |
go.SetActive(true); | |
} | |
} | |
} | |
static void disablePartialSceneObject(string[] scenes) | |
{ | |
foreach (PartialSceneObject partial in UnityEngine.Object.FindObjectsOfType(typeof(PartialSceneObject))) | |
{ | |
if (scenes.Contains(partial.gameObject.scene.name)) | |
{ | |
partial.gameObject.SetActive(false); | |
} | |
} | |
} | |
static void disablePartialSceneObjects() | |
{ | |
for (int i = 0; i < EditorSceneManager.sceneCount; i++) | |
{ | |
Scene scene = EditorSceneManager.GetSceneAt(i); | |
foreach (var go in scene.GetRootGameObjects()) | |
{ | |
if (go.GetComponent<PartialSceneObject>() != null) | |
{ | |
go.SetActive(false); | |
} | |
} | |
} | |
} | |
private static void disablePSObject() | |
{ | |
for (int i = 0; i < EditorSceneManager.sceneCount; i++) | |
{ | |
if (EditorSceneManager.GetSceneAt(i).name == currentMainScene) | |
{ | |
disablePartialSceneObjects(); | |
EditorApplication.update -= disablePSObject; | |
Debug.Log("Disabled Partial Objects"); | |
} | |
} | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment