using System; using System.Collections; using System.Diagnostics; using System.IO; using UnityEngine; namespace ICODES.STUDIO.WWebView { public class WWebView : MonoBehaviour { [DebuggerBrowsable(DebuggerBrowsableState.Never)] public event WWebView.ActionStartNavigation OnStartNavigation; [DebuggerBrowsable(DebuggerBrowsableState.Never)] public event WWebView.ActionNavigationCompleted OnNavigationCompleted; [DebuggerBrowsable(DebuggerBrowsableState.Never)] public event WWebView.ActionNavigationFailed OnNavigationFailed; [DebuggerBrowsable(DebuggerBrowsableState.Never)] public event WWebView.ActionReceiveMessage OnReceiveMessage; [DebuggerBrowsable(DebuggerBrowsableState.Never)] public event WWebView.ActionEvaluateJavaScript OnEvaluateJavaScript; [DebuggerBrowsable(DebuggerBrowsableState.Never)] public event WWebView.ActionClose OnClose; private void Start() { if (!string.IsNullOrEmpty(this.url)) { this.Navigate(this.url); } } private void OnDestroy() { this.Destroy(); } public void Initialize(Vector4 position, Vector2 size) { this.position = position; this.size = size; if (!WWebViewSystem.Instance.HoloLensVR) { this.Setup(); } } private void Setup() { if (!this.setup) { GameObject gameObject = new GameObject(Guid.NewGuid().ToString()); this.listener = gameObject.AddComponent(); gameObject.transform.parent = base.transform; this.listener.WebView = this; WWebViewPlugin.Init(this.listener.Name, (int)this.position.y, (int)this.position.x, (int)this.position.w, (int)this.position.z, (int)this.size.x, (int)this.size.y); this.setup = true; } } public void Destroy() { if (this.setup) { WWebViewPlugin.Destroy(this.listener.Name); UnityEngine.Object.Destroy(this.listener.gameObject); this.setup = false; } } public void Navigate(string url) { this.Setup(); WWebViewPlugin.Load(this.listener.Name, url); } public void NavigateString(string html) { this.Setup(); WWebViewPlugin.LoadHTMLString(this.listener.Name, html, string.Empty); } public void NavigateFile(string file) { this.Setup(); string text = Path.Combine("file://" + Application.streamingAssetsPath, file); WWebViewPlugin.Load(this.listener.Name, text); } public void SetHeaderField(string key, string value) { this.Setup(); WWebViewPlugin.SetHeaderField(this.listener.Name, key, value); } public void SetWindowLayout(int left, int top, int right, int bottom, int width, int height) { this.Setup(); WWebViewPlugin.ChangeInsets(this.listener.Name, top, left, bottom, right, width, height); } public void AddUrlScheme(string scheme) { this.Setup(); WWebViewPlugin.AddUrlScheme(this.listener.Name, scheme); } public void RemoveUrlScheme(string scheme) { this.Setup(); WWebViewPlugin.RemoveUrlScheme(this.listener.Name, scheme); } public void SetUserAgent(string userAgent) { this.Setup(); WWebViewPlugin.SetUserAgent(userAgent); } public string GetUserAgent() { this.Setup(); return WWebViewPlugin.GetUserAgent(this.listener.Name); } public void ClearCache() { this.Setup(); WWebViewPlugin.CleanCache(this.listener.Name); } public void ClearCookies() { this.Setup(); WWebViewPlugin.ClearCookies(); } public void SetCookie(string url, string cookie) { this.Setup(); WWebViewPlugin.SetCookie(url, cookie); } public string GetCookie(string url, string key) { this.Setup(); return WWebViewPlugin.GetCookie(url, key); } public void ShowScroll(bool show) { this.Setup(); WWebViewPlugin.ShowScroll(this.listener.Name, show); } public void ShowScrollX(bool show) { this.Setup(); WWebViewPlugin.ShowScrollX(this.listener.Name, show); } public void ShowScrollY(string name, bool show) { this.Setup(); WWebViewPlugin.ShowScrollY(this.listener.Name, show); } public void Refresh() { this.Setup(); WWebViewPlugin.Reload(this.listener.Name); } public void AddJavaScript(string script) { this.Setup(); WWebViewPlugin.AddJavaScript(this.listener.Name, script); } public void EvaluateJavaScript(string script) { this.Setup(); WWebViewPlugin.EvaluatingJavaScript(this.listener.Name, script); } public float Alpha { get { this.Setup(); return WWebViewPlugin.GetAlpha(this.listener.Name); } set { this.Setup(); WWebViewPlugin.SetAlpha(this.listener.Name, value); } } public bool CanGoBack { get { this.Setup(); return WWebViewPlugin.CanGoBack(this.listener.Name); } } public bool CanGoForward { get { this.Setup(); return WWebViewPlugin.CanGoForward(this.listener.Name); } } public void GoBack() { this.Setup(); WWebViewPlugin.GoBack(this.listener.Name); } public void GoForward() { this.Setup(); WWebViewPlugin.GoForward(this.listener.Name); } public void Stop() { this.Setup(); WWebViewPlugin.Stop(this.listener.Name); } public void Show() { this.Setup(); WWebViewPlugin.Show(this.listener.Name, false, 0, 0f); } public void Hide() { this.Setup(); WWebViewPlugin.Hide(this.listener.Name, false, 0, 0f); } public void SetTexture(Texture2D texture) { this.Setup(); WWebViewPlugin.SetTexture(this.listener.Name, texture); base.StartCoroutine("OnRenderTexture", WWebViewPlugin.GetRenderEventFunc()); } private IEnumerator OnRenderTexture(IntPtr handler) { for (;;) { yield return new WaitForEndOfFrame(); GL.IssuePluginEvent(handler, 0); } yield break; } public void InputEvent(int state, int key, int x, int y) { this.Setup(); WWebViewPlugin.InputEvent(this.listener.Name, state, key, x, y); } public int GetActualWidth() { this.Setup(); return WWebViewPlugin.GetActualWidth(this.listener.Name); } public int GetActualHeight() { this.Setup(); return WWebViewPlugin.GetActualHeight(this.listener.Name); } public void EnableContextMenu(bool enable) { this.Setup(); WWebViewPlugin.EnableContextMenu(this.listener.Name, enable); } public void SetZoom(int factor) { this.Setup(); WWebViewPlugin.SetZoom(this.listener.Name, factor); } internal void InternalOnStartNavigation(string url) { if (this.OnStartNavigation != null) { this.OnStartNavigation(this, url); } } internal void InternalOnNavigationCompleted(string data) { if (this.OnNavigationCompleted != null) { this.OnNavigationCompleted(this, data); } } internal void InternalOnNavigationFailed(int code, string url) { if (this.OnNavigationFailed != null) { this.OnNavigationFailed(this, code, url); } } internal void InternalOnReceiveMessage(string message) { if (this.OnReceiveMessage != null) { this.OnReceiveMessage(this, message); } } internal void InternalOnEvaluateJavaScript(string result) { if (this.OnEvaluateJavaScript != null) { this.OnEvaluateJavaScript(this, result); } } internal void InternalOnClose() { if (this.OnClose != null) { if (this.OnClose(this)) { UnityEngine.Object.Destroy(this); } } else { UnityEngine.Object.Destroy(this); } } private WWebViewListener listener; private bool setup; [Tooltip("Initial URL to load.\nTo change at runtime use Navigate to load a page.")] [SerializeField] private string url; [Tooltip("Initial position.\nTo change at runtime use SetWindowLayout.")] [SerializeField] private Vector4 position = Vector4.zero; [Tooltip("Initial size.\nTo change at runtime use SetWindowLayout.")] [SerializeField] private Vector2 size = Vector2.zero; public delegate void ActionStartNavigation(WWebView webView, string url); public delegate void ActionNavigationCompleted(WWebView webView, string data); public delegate void ActionNavigationFailed(WWebView webView, int code, string url); public delegate void ActionReceiveMessage(WWebView webView, string message); public delegate void ActionEvaluateJavaScript(WWebView webView, string result); public delegate bool ActionClose(WWebView webView); } }