-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #318 from anatawa12/apply-on-play-activator
chore: do not add GlobalActivator if there are no avatars in the scene
- Loading branch information
Showing
7 changed files
with
146 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace Anatawa12.ApplyOnPlay | ||
{ | ||
[ExecuteAlways] | ||
[AddComponentMenu("")] | ||
[DisallowMultipleComponent] | ||
public class SceneChangeReceiver : MonoBehaviour | ||
{ | ||
#if UNITY_EDITOR | ||
public Scene scene; | ||
|
||
void Update() | ||
{ | ||
if (scene.IsValid()) | ||
{ | ||
if (GlobalActivator.HasAvatarInScene(scene)) | ||
{ | ||
GlobalActivator.CreateIfNotNeeded(scene); | ||
DestroyImmediate(this); | ||
} | ||
} | ||
else | ||
{ | ||
DestroyImmediate(this); | ||
} | ||
} | ||
|
||
internal static void CreateIfNotNeeded(Scene scene) | ||
{ | ||
if (!scene.IsValid() || UnityEditor.SceneManagement.EditorSceneManager.IsPreviewScene(scene)) return; | ||
if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode) return; | ||
if (GlobalActivator.HasAvatarInScene(scene)) return; | ||
CreateIfNotExists(scene); | ||
} | ||
|
||
internal static void CreateIfNotExists(Scene scene) | ||
{ | ||
bool rootPresent = false; | ||
foreach (var root in scene.GetRootGameObjects()) | ||
{ | ||
if (root.GetComponent<SceneChangeReceiver>() != null) | ||
{ | ||
root.hideFlags = HideFlags; | ||
root.SetActive(true); | ||
if (rootPresent) DestroyImmediate(root); | ||
rootPresent = true; | ||
} | ||
} | ||
|
||
if (rootPresent) return; | ||
|
||
var oldActiveScene = SceneManager.GetActiveScene(); | ||
try | ||
{ | ||
SceneManager.SetActiveScene(scene); | ||
var gameObject = new GameObject("ApplyOnPlaySceneChangeReceiver"); | ||
var component = gameObject.AddComponent<SceneChangeReceiver>(); | ||
component.scene = scene; | ||
gameObject.hideFlags = HideFlags; | ||
component.hideFlags = HideFlags; | ||
} | ||
finally | ||
{ | ||
SceneManager.SetActiveScene(oldActiveScene); | ||
} | ||
} | ||
|
||
private const HideFlags HideFlags = UnityEngine.HideFlags.HideAndDontSave; | ||
#endif | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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