123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- 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<WWebViewListener>();
- 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);
- }
- }
|