WWebView.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using System;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using UnityEngine;
  6. namespace ICODES.STUDIO.WWebView
  7. {
  8. public class WWebView : MonoBehaviour
  9. {
  10. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  11. public event WWebView.ActionStartNavigation OnStartNavigation;
  12. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  13. public event WWebView.ActionNavigationCompleted OnNavigationCompleted;
  14. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  15. public event WWebView.ActionNavigationFailed OnNavigationFailed;
  16. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  17. public event WWebView.ActionReceiveMessage OnReceiveMessage;
  18. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  19. public event WWebView.ActionEvaluateJavaScript OnEvaluateJavaScript;
  20. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  21. public event WWebView.ActionClose OnClose;
  22. private void Start()
  23. {
  24. if (!string.IsNullOrEmpty(this.url))
  25. {
  26. this.Navigate(this.url);
  27. }
  28. }
  29. private void OnDestroy()
  30. {
  31. this.Destroy();
  32. }
  33. public void Initialize(Vector4 position, Vector2 size)
  34. {
  35. this.position = position;
  36. this.size = size;
  37. if (!WWebViewSystem.Instance.HoloLensVR)
  38. {
  39. this.Setup();
  40. }
  41. }
  42. private void Setup()
  43. {
  44. if (!this.setup)
  45. {
  46. GameObject gameObject = new GameObject(Guid.NewGuid().ToString());
  47. this.listener = gameObject.AddComponent<WWebViewListener>();
  48. gameObject.transform.parent = base.transform;
  49. this.listener.WebView = this;
  50. 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);
  51. this.setup = true;
  52. }
  53. }
  54. public void Destroy()
  55. {
  56. if (this.setup)
  57. {
  58. WWebViewPlugin.Destroy(this.listener.Name);
  59. UnityEngine.Object.Destroy(this.listener.gameObject);
  60. this.setup = false;
  61. }
  62. }
  63. public void Navigate(string url)
  64. {
  65. this.Setup();
  66. WWebViewPlugin.Load(this.listener.Name, url);
  67. }
  68. public void NavigateString(string html)
  69. {
  70. this.Setup();
  71. WWebViewPlugin.LoadHTMLString(this.listener.Name, html, string.Empty);
  72. }
  73. public void NavigateFile(string file)
  74. {
  75. this.Setup();
  76. string text = Path.Combine("file://" + Application.streamingAssetsPath, file);
  77. WWebViewPlugin.Load(this.listener.Name, text);
  78. }
  79. public void SetHeaderField(string key, string value)
  80. {
  81. this.Setup();
  82. WWebViewPlugin.SetHeaderField(this.listener.Name, key, value);
  83. }
  84. public void SetWindowLayout(int left, int top, int right, int bottom, int width, int height)
  85. {
  86. this.Setup();
  87. WWebViewPlugin.ChangeInsets(this.listener.Name, top, left, bottom, right, width, height);
  88. }
  89. public void AddUrlScheme(string scheme)
  90. {
  91. this.Setup();
  92. WWebViewPlugin.AddUrlScheme(this.listener.Name, scheme);
  93. }
  94. public void RemoveUrlScheme(string scheme)
  95. {
  96. this.Setup();
  97. WWebViewPlugin.RemoveUrlScheme(this.listener.Name, scheme);
  98. }
  99. public void SetUserAgent(string userAgent)
  100. {
  101. this.Setup();
  102. WWebViewPlugin.SetUserAgent(userAgent);
  103. }
  104. public string GetUserAgent()
  105. {
  106. this.Setup();
  107. return WWebViewPlugin.GetUserAgent(this.listener.Name);
  108. }
  109. public void ClearCache()
  110. {
  111. this.Setup();
  112. WWebViewPlugin.CleanCache(this.listener.Name);
  113. }
  114. public void ClearCookies()
  115. {
  116. this.Setup();
  117. WWebViewPlugin.ClearCookies();
  118. }
  119. public void SetCookie(string url, string cookie)
  120. {
  121. this.Setup();
  122. WWebViewPlugin.SetCookie(url, cookie);
  123. }
  124. public string GetCookie(string url, string key)
  125. {
  126. this.Setup();
  127. return WWebViewPlugin.GetCookie(url, key);
  128. }
  129. public void ShowScroll(bool show)
  130. {
  131. this.Setup();
  132. WWebViewPlugin.ShowScroll(this.listener.Name, show);
  133. }
  134. public void ShowScrollX(bool show)
  135. {
  136. this.Setup();
  137. WWebViewPlugin.ShowScrollX(this.listener.Name, show);
  138. }
  139. public void ShowScrollY(string name, bool show)
  140. {
  141. this.Setup();
  142. WWebViewPlugin.ShowScrollY(this.listener.Name, show);
  143. }
  144. public void Refresh()
  145. {
  146. this.Setup();
  147. WWebViewPlugin.Reload(this.listener.Name);
  148. }
  149. public void AddJavaScript(string script)
  150. {
  151. this.Setup();
  152. WWebViewPlugin.AddJavaScript(this.listener.Name, script);
  153. }
  154. public void EvaluateJavaScript(string script)
  155. {
  156. this.Setup();
  157. WWebViewPlugin.EvaluatingJavaScript(this.listener.Name, script);
  158. }
  159. public float Alpha
  160. {
  161. get
  162. {
  163. this.Setup();
  164. return WWebViewPlugin.GetAlpha(this.listener.Name);
  165. }
  166. set
  167. {
  168. this.Setup();
  169. WWebViewPlugin.SetAlpha(this.listener.Name, value);
  170. }
  171. }
  172. public bool CanGoBack
  173. {
  174. get
  175. {
  176. this.Setup();
  177. return WWebViewPlugin.CanGoBack(this.listener.Name);
  178. }
  179. }
  180. public bool CanGoForward
  181. {
  182. get
  183. {
  184. this.Setup();
  185. return WWebViewPlugin.CanGoForward(this.listener.Name);
  186. }
  187. }
  188. public void GoBack()
  189. {
  190. this.Setup();
  191. WWebViewPlugin.GoBack(this.listener.Name);
  192. }
  193. public void GoForward()
  194. {
  195. this.Setup();
  196. WWebViewPlugin.GoForward(this.listener.Name);
  197. }
  198. public void Stop()
  199. {
  200. this.Setup();
  201. WWebViewPlugin.Stop(this.listener.Name);
  202. }
  203. public void Show()
  204. {
  205. this.Setup();
  206. WWebViewPlugin.Show(this.listener.Name, false, 0, 0f);
  207. }
  208. public void Hide()
  209. {
  210. this.Setup();
  211. WWebViewPlugin.Hide(this.listener.Name, false, 0, 0f);
  212. }
  213. public void SetTexture(Texture2D texture)
  214. {
  215. this.Setup();
  216. WWebViewPlugin.SetTexture(this.listener.Name, texture);
  217. base.StartCoroutine("OnRenderTexture", WWebViewPlugin.GetRenderEventFunc());
  218. }
  219. private IEnumerator OnRenderTexture(IntPtr handler)
  220. {
  221. for (;;)
  222. {
  223. yield return new WaitForEndOfFrame();
  224. GL.IssuePluginEvent(handler, 0);
  225. }
  226. yield break;
  227. }
  228. public void InputEvent(int state, int key, int x, int y)
  229. {
  230. this.Setup();
  231. WWebViewPlugin.InputEvent(this.listener.Name, state, key, x, y);
  232. }
  233. public int GetActualWidth()
  234. {
  235. this.Setup();
  236. return WWebViewPlugin.GetActualWidth(this.listener.Name);
  237. }
  238. public int GetActualHeight()
  239. {
  240. this.Setup();
  241. return WWebViewPlugin.GetActualHeight(this.listener.Name);
  242. }
  243. public void EnableContextMenu(bool enable)
  244. {
  245. this.Setup();
  246. WWebViewPlugin.EnableContextMenu(this.listener.Name, enable);
  247. }
  248. public void SetZoom(int factor)
  249. {
  250. this.Setup();
  251. WWebViewPlugin.SetZoom(this.listener.Name, factor);
  252. }
  253. internal void InternalOnStartNavigation(string url)
  254. {
  255. if (this.OnStartNavigation != null)
  256. {
  257. this.OnStartNavigation(this, url);
  258. }
  259. }
  260. internal void InternalOnNavigationCompleted(string data)
  261. {
  262. if (this.OnNavigationCompleted != null)
  263. {
  264. this.OnNavigationCompleted(this, data);
  265. }
  266. }
  267. internal void InternalOnNavigationFailed(int code, string url)
  268. {
  269. if (this.OnNavigationFailed != null)
  270. {
  271. this.OnNavigationFailed(this, code, url);
  272. }
  273. }
  274. internal void InternalOnReceiveMessage(string message)
  275. {
  276. if (this.OnReceiveMessage != null)
  277. {
  278. this.OnReceiveMessage(this, message);
  279. }
  280. }
  281. internal void InternalOnEvaluateJavaScript(string result)
  282. {
  283. if (this.OnEvaluateJavaScript != null)
  284. {
  285. this.OnEvaluateJavaScript(this, result);
  286. }
  287. }
  288. internal void InternalOnClose()
  289. {
  290. if (this.OnClose != null)
  291. {
  292. if (this.OnClose(this))
  293. {
  294. UnityEngine.Object.Destroy(this);
  295. }
  296. }
  297. else
  298. {
  299. UnityEngine.Object.Destroy(this);
  300. }
  301. }
  302. private WWebViewListener listener;
  303. private bool setup;
  304. [Tooltip("Initial URL to load.\nTo change at runtime use Navigate to load a page.")]
  305. [SerializeField]
  306. private string url;
  307. [Tooltip("Initial position.\nTo change at runtime use SetWindowLayout.")]
  308. [SerializeField]
  309. private Vector4 position = Vector4.zero;
  310. [Tooltip("Initial size.\nTo change at runtime use SetWindowLayout.")]
  311. [SerializeField]
  312. private Vector2 size = Vector2.zero;
  313. public delegate void ActionStartNavigation(WWebView webView, string url);
  314. public delegate void ActionNavigationCompleted(WWebView webView, string data);
  315. public delegate void ActionNavigationFailed(WWebView webView, int code, string url);
  316. public delegate void ActionReceiveMessage(WWebView webView, string message);
  317. public delegate void ActionEvaluateJavaScript(WWebView webView, string result);
  318. public delegate bool ActionClose(WWebView webView);
  319. }
  320. }