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