Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
EventSystemManaged
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