| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 | using System;using AOT;using System.Runtime.InteropServices; // for DllImportusing UnityEngine;namespace WebGLSupport{    static class WebGLWindowPlugin    {#if UNITY_WEBGL && !UNITY_EDITOR        [DllImport("__Internal")]        public static extern void WebGLWindowInit();        [DllImport("__Internal")]        public static extern void WebGLWindowUninit();        [DllImport("__Internal")]        public static extern void WebGLWindowOnFocus(Action cb);        [DllImport("__Internal")]        public static extern void WebGLWindowOnBlur(Action cb);        [DllImport("__Internal")]        public static extern void WebGLWindowOnResize(Action cb);        [DllImport("__Internal")]        public static extern void WebGLWindowInjectFullscreen();        [DllImport("__Internal")]        public static extern string WebGLWindowGetCanvasName();        [DllImport("__Internal")]        public static extern void MakeFullscreen(string str);        [DllImport("__Internal")]        public static extern void ExitFullscreen();        [DllImport("__Internal")]        public static extern bool IsFullscreen();#else        public static void WebGLWindowInit() { }        public static void WebGLWindowUninit() { }        public static void WebGLWindowOnFocus(Action cb) { }        public static void WebGLWindowOnBlur(Action cb) { }        public static void WebGLWindowOnResize(Action cb) { }        public static void WebGLWindowInjectFullscreen() { }        public static string WebGLWindowGetCanvasName() { return ""; }        public static void MakeFullscreen(string str) { }        public static void ExitFullscreen() { }        public static bool IsFullscreen() { return false; }#endif    }    public static class WebGLWindow    {        static WebGLWindow()        {            WebGLWindowPlugin.WebGLWindowInit();        }        public static bool Focus { get; private set; }        public static event Action OnFocusEvent = () => { };        public static event Action OnBlurEvent = () => { };        public static event Action OnResizeEvent = () => { };        static string ViewportContent;        static void Init()        {            Focus = true;            WebGLWindowPlugin.WebGLWindowOnFocus(OnWindowFocus);            WebGLWindowPlugin.WebGLWindowOnBlur(OnWindowBlur);            WebGLWindowPlugin.WebGLWindowOnResize(OnWindowResize);            WebGLWindowPlugin.WebGLWindowInjectFullscreen();            Application.quitting += Uninit;        }        static void Uninit()        {            WebGLWindowPlugin.WebGLWindowUninit();            Application.quitting -= Uninit;        }        [MonoPInvokeCallback(typeof(Action))]        static void OnWindowFocus()        {            Focus = true;            OnFocusEvent();        }        [MonoPInvokeCallback(typeof(Action))]        static void OnWindowBlur()        {            Focus = false;            OnBlurEvent();        }        [MonoPInvokeCallback(typeof(Action))]        static void OnWindowResize()        {            OnResizeEvent();        }        [RuntimeInitializeOnLoadMethod]        static void RuntimeInitializeOnLoadMethod()        {            Init();        }        public static string GetCanvasName()        {            return WebGLWindowPlugin.WebGLWindowGetCanvasName();        }        public static void MakeFullscreen(string fullscreenElementName = null)        {            WebGLWindowPlugin.MakeFullscreen(fullscreenElementName ?? GetCanvasName());        }        public static void ExitFullscreen()        {            WebGLWindowPlugin.ExitFullscreen();        }        public static bool IsFullscreen()        {            return WebGLWindowPlugin.IsFullscreen();        }        public static void SwitchFullscreen()        {            if (IsFullscreen())            {                ExitFullscreen();            }            else            {                MakeFullscreen();            }        }    }}
 |