Skip to content

Instantly share code, notes, and snippets.

@baba-s
Created November 10, 2020 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baba-s/b72e597b0c93c871c61e796d984264fc to your computer and use it in GitHub Desktop.
Save baba-s/b72e597b0c93c871c61e796d984264fc to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
namespace Kogane
{
[AddComponentMenu( "" )]
[DisallowMultipleComponent]
public sealed class GlobalApplicationEvent : MonoBehaviour
{
public static Action<bool> OnPause { get; set; }
public static Action<bool> OnFocus { get; set; }
public static Action OnQuit { get; set; }
private void OnApplicationPause( bool pauseStatus )
{
OnPause?.Invoke( pauseStatus );
}
private void OnApplicationFocus( bool hasFocus )
{
OnFocus?.Invoke( hasFocus );
}
private void OnApplicationQuit()
{
OnQuit?.Invoke();
}
[RuntimeInitializeOnLoadMethod( RuntimeInitializeLoadType.BeforeSceneLoad )]
private static void RuntimeInitializeOnLoadMethod()
{
if ( FindObjectOfType<GlobalApplicationEvent>() != null ) return;
var gameObject = new GameObject( nameof( GlobalApplicationEvent ) );
gameObject.AddComponent<GlobalApplicationEvent>();
//gameObject.hideFlags = HideFlags.HideAndDontSave;
DontDestroyOnLoad( gameObject );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment