Created
November 7, 2017 10:14
-
-
Save yagero/58e949611a21ffe259503f1c76343b7f to your computer and use it in GitHub Desktop.
EventSystemManaged
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
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.EventSystems; | |
public class EventSystemManaged : EventSystem | |
{ | |
static Stack<EventSystem> ms_PrevEventSystem = new Stack<EventSystem>(); | |
static bool ms_Forced = false; | |
protected override void OnEnable() | |
{ | |
if(!ms_Forced) | |
SaveAndDisableCurrent(); | |
base.OnEnable(); | |
} | |
protected override void OnDisable() | |
{ | |
base.OnDisable(); | |
if (!ms_Forced) | |
RestorePrevious(); | |
} | |
static void SaveAndDisableCurrent() | |
{ | |
var current = EventSystem.current; | |
ms_PrevEventSystem.Push(current); | |
if (current) | |
{ | |
ms_Forced = true; | |
current.enabled = false; | |
ms_Forced = false; | |
} | |
} | |
static void RestorePrevious() | |
{ | |
Debug.Assert(ms_PrevEventSystem.Count > 0); | |
var prev = ms_PrevEventSystem.Pop(); | |
if (prev) | |
{ | |
ms_Forced = true; | |
prev.enabled = true; | |
ms_Forced = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment