WebGLWindow.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 WebGLWindowInit();
  12. [DllImport("__Internal")]
  13. public static extern void WebGLWindowOnFocus(Action cb);
  14. [DllImport("__Internal")]
  15. public static extern void WebGLWindowOnBlur(Action cb);
  16. [DllImport("__Internal")]
  17. public static extern void WebGLWindowOnResize(Action cb);
  18. [DllImport("__Internal")]
  19. public static extern void WebGLWindowInjectFullscreen();
  20. [DllImport("__Internal")]
  21. public static extern string WebGLWindowGetCanvasName();
  22. [DllImport("__Internal")]
  23. public static extern void MakeFullscreen(string str);
  24. [DllImport("__Internal")]
  25. public static extern void ExitFullscreen();
  26. [DllImport("__Internal")]
  27. public static extern bool IsFullscreen();
  28. #else
  29. public static void WebGLWindowInit() { }
  30. public static void WebGLWindowOnFocus(Action cb) { }
  31. public static void WebGLWindowOnBlur(Action cb) { }
  32. public static void WebGLWindowOnResize(Action cb) { }
  33. public static void WebGLWindowInjectFullscreen() { }
  34. public static string WebGLWindowGetCanvasName() { return ""; }
  35. public static void MakeFullscreen(string str) { }
  36. public static void ExitFullscreen() { }
  37. public static bool IsFullscreen() { return false; }
  38. #endif
  39. }
  40. public static class WebGLWindow
  41. {
  42. static WebGLWindow()
  43. {
  44. WebGLWindowPlugin.WebGLWindowInit();
  45. }
  46. public static bool Focus { get; private set; }
  47. public static event Action OnFocusEvent = () => { };
  48. public static event Action OnBlurEvent = () => { };
  49. public static event Action OnResizeEvent = () => { };
  50. static string ViewportContent;
  51. static void Init()
  52. {
  53. Focus = true;
  54. WebGLWindowPlugin.WebGLWindowOnFocus(OnWindowFocus);
  55. WebGLWindowPlugin.WebGLWindowOnBlur(OnWindowBlur);
  56. WebGLWindowPlugin.WebGLWindowOnResize(OnWindowResize);
  57. WebGLWindowPlugin.WebGLWindowInjectFullscreen();
  58. }
  59. [MonoPInvokeCallback(typeof(Action))]
  60. static void OnWindowFocus()
  61. {
  62. Focus = true;
  63. OnFocusEvent();
  64. }
  65. [MonoPInvokeCallback(typeof(Action))]
  66. static void OnWindowBlur()
  67. {
  68. Focus = false;
  69. OnBlurEvent();
  70. }
  71. [MonoPInvokeCallback(typeof(Action))]
  72. static void OnWindowResize()
  73. {
  74. OnResizeEvent();
  75. }
  76. [RuntimeInitializeOnLoadMethod]
  77. static void RuntimeInitializeOnLoadMethod()
  78. {
  79. Init();
  80. }
  81. public static string GetCanvasName()
  82. {
  83. return WebGLWindowPlugin.WebGLWindowGetCanvasName();
  84. }
  85. public static void MakeFullscreen(string fullscreenElementName = null)
  86. {
  87. WebGLWindowPlugin.MakeFullscreen(fullscreenElementName ?? GetCanvasName());
  88. }
  89. public static void ExitFullscreen()
  90. {
  91. WebGLWindowPlugin.ExitFullscreen();
  92. }
  93. public static bool IsFullscreen()
  94. {
  95. return WebGLWindowPlugin.IsFullscreen();
  96. }
  97. public static void SwitchFullscreen()
  98. {
  99. if (IsFullscreen())
  100. {
  101. ExitFullscreen();
  102. }
  103. else
  104. {
  105. MakeFullscreen();
  106. }
  107. }
  108. }
  109. }