-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The embedded EventSystem is activated only when needed to avoid dupli…
…cate EventSystem warnings in the Console
- Loading branch information
Showing
4 changed files
with
84 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using UnityEngine; | ||
using UnityEngine.EventSystems; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace ImageCropperNamespace | ||
{ | ||
// 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 ); | ||
} | ||
} | ||
} |
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