WebGLWindow.cs 4.0 KB

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