You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4 KiB
C#

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
namespace IngameDebugConsole
{
// Avoid multiple EventSystems in the scene by activating the embedded EventSystem only if one doesn't already exist in the scene
[DefaultExecutionOrder( 1000 )]
public class EventSystemHandler : MonoBehaviour
{
#pragma warning disable 0649
[SerializeField]
private GameObject embeddedEventSystem;
#pragma warning restore 0649
private void OnEnable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
SceneManager.sceneLoaded += OnSceneLoaded;
SceneManager.sceneUnloaded -= OnSceneUnloaded;
SceneManager.sceneUnloaded += OnSceneUnloaded;
ActivateEventSystemIfNeeded();
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
SceneManager.sceneUnloaded -= OnSceneUnloaded;
DeactivateEventSystem();
}
private void OnSceneLoaded( Scene scene, LoadSceneMode mode )
{
ActivateEventSystemIfNeeded();
}
private void OnSceneUnloaded( Scene current )
{
// Deactivate the embedded EventSystem before changing scenes because the new scene might have its own EventSystem
DeactivateEventSystem();
}
private void ActivateEventSystemIfNeeded()
{
if( embeddedEventSystem && !EventSystem.current )
embeddedEventSystem.SetActive( true );
}
private void DeactivateEventSystem()
{
if( embeddedEventSystem )
embeddedEventSystem.SetActive( false );
}
}
}