using System; using System.Runtime.InteropServices; using UnityEngine; using Win32; namespace ICODES.STUDIO.WWebView { public sealed class WWebViewPlugin { public static bool Init(string name, int top, int left, int bottom, int right, int width, int height) { if (!WWebViewPlugin.initialize) { IntPtr moduleHandle = Kernel32.GetModuleHandle(null); if (moduleHandle == IntPtr.Zero) { Debug.LogError("Can't find process module."); return false; } IntPtr intPtr = WWebViewWin32.FindUnityPlayerWindow(); if (intPtr == IntPtr.Zero) { Debug.LogError("Can't find Unity player window handle."); return false; } WWebViewWin32.Initialize(moduleHandle, IntPtr.Zero, null, 0, intPtr, Screen.width, Screen.height, 11000); WWebViewSystem.Instance.Initialize(); WWebViewPlugin.documentComplete = new WWebViewWin32.ActionDocumentComplete(WWebViewPlugin.OnDocumentComplete); WWebViewPlugin.beforeNavigate = new WWebViewWin32.ActionBeforeNavigate(WWebViewPlugin.OnBeforeNavigate); WWebViewPlugin.windowClosing = new WWebViewWin32.ActionWindowClosing(WWebViewPlugin.OnWindowClosing); WWebViewPlugin.titleChange = new WWebViewWin32.ActionTitleChange(WWebViewPlugin.OnTitleChange); WWebViewPlugin.newWindow = new WWebViewWin32.ActionNewWindow(WWebViewPlugin.OnNewWindow); WWebViewPlugin.navigateComplete = new WWebViewWin32.ActionNavigateComplete(WWebViewPlugin.OnNavigateComplete); WWebViewPlugin.initialize = true; } WWebViewWin32.Create(name, WWebViewPlugin.documentComplete, WWebViewPlugin.beforeNavigate, WWebViewPlugin.windowClosing, WWebViewPlugin.titleChange, WWebViewPlugin.newWindow, WWebViewPlugin.navigateComplete, left, top, right, bottom, width, height, false); WWebViewWin32.AddUrlScheme(name, "wwebview"); return true; } public static bool Init(string name, int top, int left, int bottom, int right) { return WWebViewPlugin.Init(name, top, left, bottom, right, 0, 0); } public static void Release() { WWebViewWin32.Release(); } public static void ChangeInsets(string name, int top, int left, int bottom, int right, int width, int height) { WWebViewWin32.ChangeLayout(name, left, top, right, bottom, width, height); } public static void ChangeInsets(string name, int top, int left, int bottom, int right) { WWebViewPlugin.ChangeInsets(name, top, left, bottom, right, 0, 0); } public static void SetFrame(string name, int x, int y, int width, int height) { WWebViewPlugin.ChangeInsets(name, y, x, -1, -1, width, height); } public static void SetPosition(string name, int x, int y) { WWebViewPlugin.ChangeInsets(name, y, x, -1, -1, -1, -1); } public static void SetSize(string name, int width, int height) { WWebViewPlugin.ChangeInsets(name, -1, -1, -1, -1, width, height); } public static void EnableContextMenu(string name, bool enable) { WWebViewWin32.EnableContextMenu(name, enable); } public static void SetTexture(string name, Texture texture) { WWebViewWin32.SetTexture(name, texture.GetNativeTexturePtr(), texture.width, texture.height); } public static void InputEvent(string name, int state, int key, int x, int y) { WWebViewWin32.InputEvent(name, state, key, x, y); } public static void SetZoom(string name, int factor) { WWebViewWin32.SetZoom(name, factor); } public static IntPtr GetRenderEventFunc() { return WWebViewWin32.GetRenderEventFunc(); } public static void ShowScroll(string name, bool show) { WWebViewWin32.ShowScroll(name, show); } public static void ShowScrollX(string name, bool show) { WWebViewWin32.ShowScrollX(name, show); } public static bool SetHorizontalScrollBarShow(string name, bool show) { WWebViewWin32.ShowScrollX(name, show); return true; } public static void SetHorizontalScrollBarEnabled(string name, bool enabled) { WWebViewPlugin.ShowScrollX(name, enabled); } public static void ShowScrollY(string name, bool show) { WWebViewWin32.ShowScrollY(name, show); } public static bool SetVerticalScrollBarShow(string name, bool show) { WWebViewWin32.ShowScrollY(name, show); return true; } public static void SetVerticalScrollBarEnabled(string name, bool enabled) { WWebViewPlugin.ShowScrollY(name, enabled); } public static void AddUrlScheme(string name, string scheme) { WWebViewWin32.AddUrlScheme(name, scheme); } public static void RemoveUrlScheme(string name, string scheme) { WWebViewWin32.RemoveUrlScheme(name, scheme); } public static void Load(string name, string url) { WWebViewWin32.Navigate(name, url); } public static void Load(string name, string url, bool skipEncoding) { WWebViewPlugin.Load(name, url); } public static void LoadHTMLString(string name, string html, string baseUrl) { WWebViewWin32.NavigateToString(name, html); } public static void LoadHTMLString(string name, string html, string baseUrl, bool skipEncoding) { WWebViewPlugin.LoadHTMLString(name, html, baseUrl); } public static void Reload(string name) { WWebViewWin32.Refresh(name); } public static void AddJavaScript(string name, string script) { WWebViewWin32.AddJavaScript(name, script); } public static void AddJavaScript(string name, string script, string identifier) { WWebViewPlugin.AddJavaScript(name, script); } public static void EvaluatingJavaScript(string name, string script) { GameObject gameObject = GameObject.Find(name); if (gameObject != null) { string value = Marshal.PtrToStringAuto(WWebViewWin32.EvaluateJavaScript(name, script)); gameObject.SendMessage("EvalJavaScriptFinished", value, SendMessageOptions.DontRequireReceiver); } } public static void EvaluateJavaScript(string name, string script, string identifier) { WWebViewPlugin.EvaluatingJavaScript(name, script); } public static void CleanCache(string name) { WWebViewWin32.CleanCache(); } public static void CleanCookie(string name, string key) { WWebViewWin32.CleanCookie(name); } public static void ClearCookies() { WWebViewPlugin.CleanCookie(string.Empty, string.Empty); } public static string GetCookie(string url, string key) { return Marshal.PtrToStringAuto(WWebViewWin32.GetCookie(url, key)); } public static string GetCookie(string url, string key, bool skipEncoding) { return WWebViewPlugin.GetCookie(url, key); } public static void SetCookie(string url, string cookie) { WWebViewWin32.SetCookie(url, cookie); } public static void SetCookie(string url, string cookie, bool skipEncoding) { WWebViewPlugin.SetCookie(url, cookie); } public static void Destroy(string name) { WWebViewWin32.Destroy(name); } public static void GoBack(string name) { WWebViewWin32.GoBack(name); } public static void GoForward(string name) { WWebViewWin32.GoForward(name); } public static void Stop(string name) { WWebViewWin32.Stop(name); } public static string GetCurrentUrl(string name) { return Marshal.PtrToStringAuto(WWebViewWin32.CurrentUrl(name)); } public static string GetUrl(string name) { return WWebViewPlugin.GetCurrentUrl(name); } public static bool Show(string name, bool fade, int direction, float duration) { WWebViewWin32.Show(name, true); return true; } public static bool Show(string name, bool fade, int edge, float duration, string identifier) { return WWebViewPlugin.Show(name, fade, edge, duration); } public static bool Hide(string name, bool fade, int direction, float duration) { WWebViewWin32.Show(name, false); return true; } public static bool Hide(string name, bool fade, int edge, float duration, string identifier) { return WWebViewPlugin.Hide(name, fade, edge, duration); } public static void SetZoomEnable(string name, bool enable) { } public static void SetZoomEnabled(string name, bool enabled) { } public static string GetUserAgent(string name) { return Marshal.PtrToStringAuto(WWebViewWin32.GetUserAgent(name)); } public static void SetUserAgent(string userAgent) { WWebViewWin32.SetUserAgent(null, userAgent); } public static void SetUserAgent(string name, string userAgent) { WWebViewWin32.SetUserAgent(name, userAgent); } public static void TransparentBackground(string name, bool transparent) { WWebViewWin32.Transparent(name, transparent); } public static float GetAlpha(string name) { return WWebViewWin32.GetAlpha(name); } public static float GetWebViewAlpha(string name) { return WWebViewPlugin.GetAlpha(name); } public static void SetAlpha(string name, float alpha) { WWebViewWin32.SetAlpha(name, alpha); } public static void SetWebViewAlpha(string name, float alpha) { WWebViewPlugin.SetAlpha(name, alpha); } public static int GetActualWidth(string name) { return WWebViewWin32.GetActualWidth(name); } public static int GetActualHeight(string name) { return WWebViewWin32.GetActualHeight(name); } public static bool CanGoBack(string name) { return WWebViewWin32.CanGoBack(name); } public static bool CanGoForward(string name) { return WWebViewWin32.CanGoForward(name); } public static void SetHeaderField(string name, string key, string value) { WWebViewWin32.SetHeaderField(name, key, value); } public static void SetSpinnerShowWhenLoading(string name, bool show) { } public static void SetShowSpinnerWhileLoading(string name, bool show) { } public static void SetSpinnerText(string name, string text) { } public static void SetBounces(string name, bool enable) { } public static bool GetOpenLinksInExternalBrowser(string name) { return false; } public static void SetOpenLinksInExternalBrowser(string name, bool value) { } public static void SetBackgroundColor(string name, float r, float g, float b, float a) { } public static void SetAllowAutoPlay(string name, bool value) { } public static void SetAllowAutoPlay(bool flag) { } public static void SetAllowInlinePlay(bool flag) { } public static void SetAllowThirdPartyCookies(bool allowed) { } public static void SetLogLevel(int level) { } public static bool AnimateTo(string name, int x, int y, int width, int height, float duration, float delay, string identifier) { return false; } public static void AddSslExceptionDomain(string name, string domain) { } public static void RemoveSslExceptionDomain(string name, string domain) { } public static void SetAllowJavaScriptOpenWindow(bool flag) { } public static void SetJavaScriptEnabled(bool flag) { } public static void ClearHttpAuthUsernamePassword(string host, string realm) { } public static void SetBouncesEnabled(string name, bool enabled) { } public static void SetShowToolbar(string name, bool show, bool animated, bool onTop, bool adjustInset) { } public static void SetToolbarDoneButtonText(string name, string text) { } public static void SetWebContentsDebuggingEnabled(bool enabled) { } public static void Print(string name) { } private static void OnBeforeNavigate(IntPtr name, IntPtr url, IntPtr message, ref bool cancel) { GameObject gameObject = GameObject.Find(Marshal.PtrToStringAuto(name)); if (gameObject != null) { string value = Marshal.PtrToStringAuto(url); string text = Marshal.PtrToStringAuto(message); if (!string.IsNullOrEmpty(text)) { gameObject.SendMessage("ReceivedMessage", value, SendMessageOptions.DontRequireReceiver); if (text == "close" || text == "close/") { gameObject.SendMessage("WebViewDone", string.Empty, SendMessageOptions.DontRequireReceiver); } } else { gameObject.SendMessage("LoadBegin", value, SendMessageOptions.DontRequireReceiver); } } } private static void OnDocumentComplete(IntPtr name, IntPtr url) { GameObject gameObject = GameObject.Find(Marshal.PtrToStringAuto(name)); if (gameObject != null) { gameObject.SendMessage("LoadComplete", Marshal.PtrToStringAuto(url), SendMessageOptions.DontRequireReceiver); } } private static void OnWindowClosing(IntPtr name, bool childWindow, ref bool cancel) { GameObject gameObject = GameObject.Find(Marshal.PtrToStringAuto(name)); if (gameObject != null && !childWindow) { gameObject.SendMessage("WebViewDone", string.Empty, SendMessageOptions.DontRequireReceiver); } } private static void OnTitleChange(IntPtr name, IntPtr title) { } private static void OnNewWindow(IntPtr name, ref bool cancel) { } private static void OnNavigateComplete(IntPtr name, IntPtr url) { } private static WWebViewWin32.ActionDocumentComplete documentComplete; private static WWebViewWin32.ActionBeforeNavigate beforeNavigate; private static WWebViewWin32.ActionWindowClosing windowClosing; private static WWebViewWin32.ActionTitleChange titleChange; private static WWebViewWin32.ActionNewWindow newWindow; private static WWebViewWin32.ActionNavigateComplete navigateComplete; private static bool initialize; } }