WebGLWindow.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using AOT;
  3. using System.Runtime.InteropServices; // for DllImport
  4. using UnityEngine;
  5. namespace WebGLSupport
  6. {
  7. static class WebGLWindowPlugin
  8. {
  9. #if UNITY_WEBGL && !UNITY_EDITOR
  10. [DllImport("__Internal")]
  11. public static extern void WebGLWindowOnFocus(Action cb);
  12. [DllImport("__Internal")]
  13. public static extern void WebGLWindowOnBlur(Action cb);
  14. #else
  15. public static void WebGLWindowOnFocus(Action cb) { }
  16. public static void WebGLWindowOnBlur(Action cb) { }
  17. #endif
  18. }
  19. public static class WebGLWindow
  20. {
  21. public static bool Focus { get; private set; }
  22. public static event Action OnFocusEvent = () => { };
  23. public static event Action OnBlurEvent = () => { };
  24. static void Init()
  25. {
  26. Focus = true;
  27. WebGLWindowPlugin.WebGLWindowOnFocus(OnWindowFocus);
  28. WebGLWindowPlugin.WebGLWindowOnBlur(OnWindowBlur);
  29. }
  30. [MonoPInvokeCallback(typeof(Action))]
  31. static void OnWindowFocus()
  32. {
  33. Focus = true;
  34. OnFocusEvent();
  35. }
  36. [MonoPInvokeCallback(typeof(Action))]
  37. static void OnWindowBlur()
  38. {
  39. Focus = false;
  40. OnBlurEvent();
  41. }
  42. [RuntimeInitializeOnLoadMethod]
  43. static void RuntimeInitializeOnLoadMethod()
  44. {
  45. Init();
  46. }
  47. }
  48. }