Skip to content

Commit

Permalink
The embedded EventSystem is activated only when needed to avoid dupli…
Browse files Browse the repository at this point in the history
…cate EventSystem warnings in the Console
  • Loading branch information
yasirkula committed Jul 25, 2022
1 parent 8713598 commit bf5a60e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
15 changes: 14 additions & 1 deletion Plugins/ImageCropper/Resources/ImageCropper.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ GameObject:
- component: {fileID: 114447286773665142}
- component: {fileID: 114788079095688046}
- component: {fileID: 114323362291179536}
- component: {fileID: 114327519071772764}
m_Layer: 0
m_Name: ImageCropper
m_TagString: Untagged
Expand Down Expand Up @@ -638,7 +639,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!1 &1718562304412798
GameObject:
m_ObjectHideFlags: 1
Expand Down Expand Up @@ -1341,6 +1342,18 @@ MonoBehaviour:
detail: 84
mode: 0
edgeThickness: 1
--- !u!114 &114327519071772764
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1448628512259654}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5a39d7b3bb056d340842cf64a2bdf500, type: 3}
m_Name:
m_EditorClassIdentifier:
embeddedEventSystem: {fileID: 1713518909072214}
--- !u!114 &114346701098374660
MonoBehaviour:
m_ObjectHideFlags: 1
Expand Down
57 changes: 57 additions & 0 deletions Plugins/ImageCropper/Scripts/EventSystemHandler.cs
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 );
}
}
}
12 changes: 12 additions & 0 deletions Plugins/ImageCropper/Scripts/EventSystemHandler.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.yasirkula.imagecropper",
"displayName": "Image Cropper",
"version": "1.0.4",
"version": "1.0.5",
"documentationUrl": "https://github.com/yasirkula/UnityImageCropper",
"changelogUrl": "https://github.com/yasirkula/UnityImageCropper/releases",
"licensesUrl": "https://github.com/yasirkula/UnityImageCropper/blob/master/LICENSE.txt",
Expand Down

0 comments on commit bf5a60e

Please sign in to comment.