Skip to content

Instantly share code, notes, and snippets.

@yagero
Created November 7, 2017 10:14
Show Gist options
  • Save yagero/58e949611a21ffe259503f1c76343b7f to your computer and use it in GitHub Desktop.
Save yagero/58e949611a21ffe259503f1c76343b7f to your computer and use it in GitHub Desktop.
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