fix: multiple bugs in Activator

* Activator tries to mark scene dirty in play mode (fixes #164)
* Activator creates multiple hidden objects which build up over time
This commit is contained in:
bd_ 2022-12-20 21:32:28 +09:00
parent 39be252f12
commit 5351b7befb

View File

@ -56,11 +56,13 @@ namespace nadena.dev.modular_avatar.core
private void OnValidate() private void OnValidate()
{ {
if (EditorApplication.isPlayingOrWillChangePlaymode) return;
EditorApplication.delayCall += () => EditorApplication.delayCall += () =>
{ {
if (this == null) return; if (this == null) return;
gameObject.hideFlags = HideFlags.HideInHierarchy; gameObject.hideFlags = HIDE_FLAGS;
if (!HasMAComponentsInScene()) if (!HasMAComponentsInScene())
{ {
var scene = gameObject.scene; var scene = gameObject.scene;
@ -75,24 +77,34 @@ namespace nadena.dev.modular_avatar.core
if (!scene.IsValid() || EditorSceneManager.IsPreviewScene(scene)) return; if (!scene.IsValid() || EditorSceneManager.IsPreviewScene(scene)) return;
if (EditorApplication.isPlayingOrWillChangePlaymode) return; if (EditorApplication.isPlayingOrWillChangePlaymode) return;
bool rootPresent = false;
foreach (var root in scene.GetRootGameObjects()) foreach (var root in scene.GetRootGameObjects())
{ {
if (root.GetComponent<Activator>() != null) return; if (root.GetComponent<Activator>() != null)
{
root.hideFlags = HIDE_FLAGS;
if (rootPresent) DestroyImmediate(root);
rootPresent = true;
}
} }
if (rootPresent) return;
var oldActiveScene = SceneManager.GetActiveScene(); var oldActiveScene = SceneManager.GetActiveScene();
try try
{ {
SceneManager.SetActiveScene(scene); SceneManager.SetActiveScene(scene);
var gameObject = new GameObject(TAG_OBJECT_NAME); var gameObject = new GameObject(TAG_OBJECT_NAME);
gameObject.AddComponent<Activator>(); gameObject.AddComponent<Activator>();
gameObject.hideFlags = HideFlags.HideInHierarchy; gameObject.hideFlags = HIDE_FLAGS;
} }
finally finally
{ {
SceneManager.SetActiveScene(oldActiveScene); SceneManager.SetActiveScene(oldActiveScene);
} }
} }
private const HideFlags HIDE_FLAGS = HideFlags.HideInHierarchy;
} }
[AddComponentMenu("")] [AddComponentMenu("")]