using UnityEngine;
using UnityEngine.ProBuilder;
using UnityEngine.SceneManagement;
namespace UnityEditor.ProBuilder
{
///
/// Responsible for managing the visibility of entity types in the scene.
///
[InitializeOnLoad]
internal static class EntityVisibility
{
static EntityVisibility()
{
#if UNITY_2017_2_OR_NEWER
EditorApplication.playModeStateChanged += x => { OnPlayModeStateChanged(); };
#else
EditorApplication.playmodeStateChanged += OnPlayModeStateChanged;
#endif
SceneManager.sceneLoaded += OnSceneLoaded;
}
private static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (EditorApplication.isPlaying)
{
foreach (var rootGameObject in scene.GetRootGameObjects())
{
foreach (var entityBehaviour in rootGameObject.GetComponentsInChildren())
{
if (entityBehaviour.manageVisibility)
entityBehaviour.OnSceneLoaded(scene, mode);
}
}
}
}
///
/// Set the visibility of an entity type in the Scene view.
///
///
///
public static void SetEntityVisibility(EntityType entityType, bool isVisible)
{
foreach (var entity in Object.FindObjectsOfType())
if (entity.entityType == entityType)
{
var mr = entity.GetComponent();
if (mr != null) mr.enabled = isVisible;
}
}
///
/// Registered to EditorApplication.onPlaymodeStateChanged
///
private static void OnPlayModeStateChanged()
{
var isPlaying = EditorApplication.isPlaying;
var orWillPlay = EditorApplication.isPlayingOrWillChangePlaymode;
// if these two don't match, that means it's the call prior to actually engaging
// whatever state. when entering play mode it doesn't make a difference, but on
// exiting it's the difference between a scene reload and the reloaded scene.
if (isPlaying != orWillPlay)
return;
var isEntering = isPlaying && orWillPlay;
foreach (var entityBehaviour in Resources.FindObjectsOfTypeAll())
if (entityBehaviour.manageVisibility)
if (isEntering)
entityBehaviour.OnEnterPlayMode();
if (!isEntering)
return;
}
}
}