WWebViewPlugin.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using UnityEngine;
  4. using Win32;
  5. namespace ICODES.STUDIO.WWebView
  6. {
  7. public sealed class WWebViewPlugin
  8. {
  9. public static bool Init(string name, int top, int left, int bottom, int right, int width, int height)
  10. {
  11. if (!WWebViewPlugin.initialize)
  12. {
  13. IntPtr moduleHandle = Kernel32.GetModuleHandle(null);
  14. if (moduleHandle == IntPtr.Zero)
  15. {
  16. Debug.LogError("Can't find process module.");
  17. return false;
  18. }
  19. IntPtr intPtr = WWebViewWin32.FindUnityPlayerWindow();
  20. if (intPtr == IntPtr.Zero)
  21. {
  22. Debug.LogError("Can't find Unity player window handle.");
  23. return false;
  24. }
  25. WWebViewWin32.Initialize(moduleHandle, IntPtr.Zero, null, 0, intPtr, Screen.width, Screen.height, 11000);
  26. WWebViewSystem.Instance.Initialize();
  27. WWebViewPlugin.documentComplete = new WWebViewWin32.ActionDocumentComplete(WWebViewPlugin.OnDocumentComplete);
  28. WWebViewPlugin.beforeNavigate = new WWebViewWin32.ActionBeforeNavigate(WWebViewPlugin.OnBeforeNavigate);
  29. WWebViewPlugin.windowClosing = new WWebViewWin32.ActionWindowClosing(WWebViewPlugin.OnWindowClosing);
  30. WWebViewPlugin.titleChange = new WWebViewWin32.ActionTitleChange(WWebViewPlugin.OnTitleChange);
  31. WWebViewPlugin.newWindow = new WWebViewWin32.ActionNewWindow(WWebViewPlugin.OnNewWindow);
  32. WWebViewPlugin.navigateComplete = new WWebViewWin32.ActionNavigateComplete(WWebViewPlugin.OnNavigateComplete);
  33. WWebViewPlugin.initialize = true;
  34. }
  35. WWebViewWin32.Create(name, WWebViewPlugin.documentComplete, WWebViewPlugin.beforeNavigate, WWebViewPlugin.windowClosing, WWebViewPlugin.titleChange, WWebViewPlugin.newWindow, WWebViewPlugin.navigateComplete, left, top, right, bottom, width, height, false);
  36. WWebViewWin32.AddUrlScheme(name, "wwebview");
  37. return true;
  38. }
  39. public static bool Init(string name, int top, int left, int bottom, int right)
  40. {
  41. return WWebViewPlugin.Init(name, top, left, bottom, right, 0, 0);
  42. }
  43. public static void Release()
  44. {
  45. WWebViewWin32.Release();
  46. }
  47. public static void ChangeInsets(string name, int top, int left, int bottom, int right, int width, int height)
  48. {
  49. WWebViewWin32.ChangeLayout(name, left, top, right, bottom, width, height);
  50. }
  51. public static void ChangeInsets(string name, int top, int left, int bottom, int right)
  52. {
  53. WWebViewPlugin.ChangeInsets(name, top, left, bottom, right, 0, 0);
  54. }
  55. public static void SetFrame(string name, int x, int y, int width, int height)
  56. {
  57. WWebViewPlugin.ChangeInsets(name, y, x, -1, -1, width, height);
  58. }
  59. public static void SetPosition(string name, int x, int y)
  60. {
  61. WWebViewPlugin.ChangeInsets(name, y, x, -1, -1, -1, -1);
  62. }
  63. public static void SetSize(string name, int width, int height)
  64. {
  65. WWebViewPlugin.ChangeInsets(name, -1, -1, -1, -1, width, height);
  66. }
  67. public static void EnableContextMenu(string name, bool enable)
  68. {
  69. WWebViewWin32.EnableContextMenu(name, enable);
  70. }
  71. public static void SetTexture(string name, Texture texture)
  72. {
  73. WWebViewWin32.SetTexture(name, texture.GetNativeTexturePtr(), texture.width, texture.height);
  74. }
  75. public static void InputEvent(string name, int state, int key, int x, int y)
  76. {
  77. WWebViewWin32.InputEvent(name, state, key, x, y);
  78. }
  79. public static void SetZoom(string name, int factor)
  80. {
  81. WWebViewWin32.SetZoom(name, factor);
  82. }
  83. public static IntPtr GetRenderEventFunc()
  84. {
  85. return WWebViewWin32.GetRenderEventFunc();
  86. }
  87. public static void ShowScroll(string name, bool show)
  88. {
  89. WWebViewWin32.ShowScroll(name, show);
  90. }
  91. public static void ShowScrollX(string name, bool show)
  92. {
  93. WWebViewWin32.ShowScrollX(name, show);
  94. }
  95. public static bool SetHorizontalScrollBarShow(string name, bool show)
  96. {
  97. WWebViewWin32.ShowScrollX(name, show);
  98. return true;
  99. }
  100. public static void SetHorizontalScrollBarEnabled(string name, bool enabled)
  101. {
  102. WWebViewPlugin.ShowScrollX(name, enabled);
  103. }
  104. public static void ShowScrollY(string name, bool show)
  105. {
  106. WWebViewWin32.ShowScrollY(name, show);
  107. }
  108. public static bool SetVerticalScrollBarShow(string name, bool show)
  109. {
  110. WWebViewWin32.ShowScrollY(name, show);
  111. return true;
  112. }
  113. public static void SetVerticalScrollBarEnabled(string name, bool enabled)
  114. {
  115. WWebViewPlugin.ShowScrollY(name, enabled);
  116. }
  117. public static void AddUrlScheme(string name, string scheme)
  118. {
  119. WWebViewWin32.AddUrlScheme(name, scheme);
  120. }
  121. public static void RemoveUrlScheme(string name, string scheme)
  122. {
  123. WWebViewWin32.RemoveUrlScheme(name, scheme);
  124. }
  125. public static void Load(string name, string url)
  126. {
  127. WWebViewWin32.Navigate(name, url);
  128. }
  129. public static void Load(string name, string url, bool skipEncoding)
  130. {
  131. WWebViewPlugin.Load(name, url);
  132. }
  133. public static void LoadHTMLString(string name, string html, string baseUrl)
  134. {
  135. WWebViewWin32.NavigateToString(name, html);
  136. }
  137. public static void LoadHTMLString(string name, string html, string baseUrl, bool skipEncoding)
  138. {
  139. WWebViewPlugin.LoadHTMLString(name, html, baseUrl);
  140. }
  141. public static void Reload(string name)
  142. {
  143. WWebViewWin32.Refresh(name);
  144. }
  145. public static void AddJavaScript(string name, string script)
  146. {
  147. WWebViewWin32.AddJavaScript(name, script);
  148. }
  149. public static void AddJavaScript(string name, string script, string identifier)
  150. {
  151. WWebViewPlugin.AddJavaScript(name, script);
  152. }
  153. public static void EvaluatingJavaScript(string name, string script)
  154. {
  155. GameObject gameObject = GameObject.Find(name);
  156. if (gameObject != null)
  157. {
  158. string value = Marshal.PtrToStringAuto(WWebViewWin32.EvaluateJavaScript(name, script));
  159. gameObject.SendMessage("EvalJavaScriptFinished", value, SendMessageOptions.DontRequireReceiver);
  160. }
  161. }
  162. public static void EvaluateJavaScript(string name, string script, string identifier)
  163. {
  164. WWebViewPlugin.EvaluatingJavaScript(name, script);
  165. }
  166. public static void CleanCache(string name)
  167. {
  168. WWebViewWin32.CleanCache();
  169. }
  170. public static void CleanCookie(string name, string key)
  171. {
  172. WWebViewWin32.CleanCookie(name);
  173. }
  174. public static void ClearCookies()
  175. {
  176. WWebViewPlugin.CleanCookie(string.Empty, string.Empty);
  177. }
  178. public static string GetCookie(string url, string key)
  179. {
  180. return Marshal.PtrToStringAuto(WWebViewWin32.GetCookie(url, key));
  181. }
  182. public static string GetCookie(string url, string key, bool skipEncoding)
  183. {
  184. return WWebViewPlugin.GetCookie(url, key);
  185. }
  186. public static void SetCookie(string url, string cookie)
  187. {
  188. WWebViewWin32.SetCookie(url, cookie);
  189. }
  190. public static void SetCookie(string url, string cookie, bool skipEncoding)
  191. {
  192. WWebViewPlugin.SetCookie(url, cookie);
  193. }
  194. public static void Destroy(string name)
  195. {
  196. WWebViewWin32.Destroy(name);
  197. }
  198. public static void GoBack(string name)
  199. {
  200. WWebViewWin32.GoBack(name);
  201. }
  202. public static void GoForward(string name)
  203. {
  204. WWebViewWin32.GoForward(name);
  205. }
  206. public static void Stop(string name)
  207. {
  208. WWebViewWin32.Stop(name);
  209. }
  210. public static string GetCurrentUrl(string name)
  211. {
  212. return Marshal.PtrToStringAuto(WWebViewWin32.CurrentUrl(name));
  213. }
  214. public static string GetUrl(string name)
  215. {
  216. return WWebViewPlugin.GetCurrentUrl(name);
  217. }
  218. public static bool Show(string name, bool fade, int direction, float duration)
  219. {
  220. WWebViewWin32.Show(name, true);
  221. return true;
  222. }
  223. public static bool Show(string name, bool fade, int edge, float duration, string identifier)
  224. {
  225. return WWebViewPlugin.Show(name, fade, edge, duration);
  226. }
  227. public static bool Hide(string name, bool fade, int direction, float duration)
  228. {
  229. WWebViewWin32.Show(name, false);
  230. return true;
  231. }
  232. public static bool Hide(string name, bool fade, int edge, float duration, string identifier)
  233. {
  234. return WWebViewPlugin.Hide(name, fade, edge, duration);
  235. }
  236. public static void SetZoomEnable(string name, bool enable)
  237. {
  238. }
  239. public static void SetZoomEnabled(string name, bool enabled)
  240. {
  241. }
  242. public static string GetUserAgent(string name)
  243. {
  244. return Marshal.PtrToStringAuto(WWebViewWin32.GetUserAgent(name));
  245. }
  246. public static void SetUserAgent(string userAgent)
  247. {
  248. WWebViewWin32.SetUserAgent(null, userAgent);
  249. }
  250. public static void SetUserAgent(string name, string userAgent)
  251. {
  252. WWebViewWin32.SetUserAgent(name, userAgent);
  253. }
  254. public static void TransparentBackground(string name, bool transparent)
  255. {
  256. WWebViewWin32.Transparent(name, transparent);
  257. }
  258. public static float GetAlpha(string name)
  259. {
  260. return WWebViewWin32.GetAlpha(name);
  261. }
  262. public static float GetWebViewAlpha(string name)
  263. {
  264. return WWebViewPlugin.GetAlpha(name);
  265. }
  266. public static void SetAlpha(string name, float alpha)
  267. {
  268. WWebViewWin32.SetAlpha(name, alpha);
  269. }
  270. public static void SetWebViewAlpha(string name, float alpha)
  271. {
  272. WWebViewPlugin.SetAlpha(name, alpha);
  273. }
  274. public static int GetActualWidth(string name)
  275. {
  276. return WWebViewWin32.GetActualWidth(name);
  277. }
  278. public static int GetActualHeight(string name)
  279. {
  280. return WWebViewWin32.GetActualHeight(name);
  281. }
  282. public static bool CanGoBack(string name)
  283. {
  284. return WWebViewWin32.CanGoBack(name);
  285. }
  286. public static bool CanGoForward(string name)
  287. {
  288. return WWebViewWin32.CanGoForward(name);
  289. }
  290. public static void SetHeaderField(string name, string key, string value)
  291. {
  292. WWebViewWin32.SetHeaderField(name, key, value);
  293. }
  294. public static void SetSpinnerShowWhenLoading(string name, bool show)
  295. {
  296. }
  297. public static void SetShowSpinnerWhileLoading(string name, bool show)
  298. {
  299. }
  300. public static void SetSpinnerText(string name, string text)
  301. {
  302. }
  303. public static void SetBounces(string name, bool enable)
  304. {
  305. }
  306. public static bool GetOpenLinksInExternalBrowser(string name)
  307. {
  308. return false;
  309. }
  310. public static void SetOpenLinksInExternalBrowser(string name, bool value)
  311. {
  312. }
  313. public static void SetBackgroundColor(string name, float r, float g, float b, float a)
  314. {
  315. }
  316. public static void SetAllowAutoPlay(string name, bool value)
  317. {
  318. }
  319. public static void SetAllowAutoPlay(bool flag)
  320. {
  321. }
  322. public static void SetAllowInlinePlay(bool flag)
  323. {
  324. }
  325. public static void SetAllowThirdPartyCookies(bool allowed)
  326. {
  327. }
  328. public static void SetLogLevel(int level)
  329. {
  330. }
  331. public static bool AnimateTo(string name, int x, int y, int width, int height, float duration, float delay, string identifier)
  332. {
  333. return false;
  334. }
  335. public static void AddSslExceptionDomain(string name, string domain)
  336. {
  337. }
  338. public static void RemoveSslExceptionDomain(string name, string domain)
  339. {
  340. }
  341. public static void SetAllowJavaScriptOpenWindow(bool flag)
  342. {
  343. }
  344. public static void SetJavaScriptEnabled(bool flag)
  345. {
  346. }
  347. public static void ClearHttpAuthUsernamePassword(string host, string realm)
  348. {
  349. }
  350. public static void SetBouncesEnabled(string name, bool enabled)
  351. {
  352. }
  353. public static void SetShowToolbar(string name, bool show, bool animated, bool onTop, bool adjustInset)
  354. {
  355. }
  356. public static void SetToolbarDoneButtonText(string name, string text)
  357. {
  358. }
  359. public static void SetWebContentsDebuggingEnabled(bool enabled)
  360. {
  361. }
  362. public static void Print(string name)
  363. {
  364. }
  365. private static void OnBeforeNavigate(IntPtr name, IntPtr url, IntPtr message, ref bool cancel)
  366. {
  367. GameObject gameObject = GameObject.Find(Marshal.PtrToStringAuto(name));
  368. if (gameObject != null)
  369. {
  370. string value = Marshal.PtrToStringAuto(url);
  371. string text = Marshal.PtrToStringAuto(message);
  372. if (!string.IsNullOrEmpty(text))
  373. {
  374. gameObject.SendMessage("ReceivedMessage", value, SendMessageOptions.DontRequireReceiver);
  375. if (text == "close" || text == "close/")
  376. {
  377. gameObject.SendMessage("WebViewDone", string.Empty, SendMessageOptions.DontRequireReceiver);
  378. }
  379. }
  380. else
  381. {
  382. gameObject.SendMessage("LoadBegin", value, SendMessageOptions.DontRequireReceiver);
  383. }
  384. }
  385. }
  386. private static void OnDocumentComplete(IntPtr name, IntPtr url)
  387. {
  388. GameObject gameObject = GameObject.Find(Marshal.PtrToStringAuto(name));
  389. if (gameObject != null)
  390. {
  391. gameObject.SendMessage("LoadComplete", Marshal.PtrToStringAuto(url), SendMessageOptions.DontRequireReceiver);
  392. }
  393. }
  394. private static void OnWindowClosing(IntPtr name, bool childWindow, ref bool cancel)
  395. {
  396. GameObject gameObject = GameObject.Find(Marshal.PtrToStringAuto(name));
  397. if (gameObject != null && !childWindow)
  398. {
  399. gameObject.SendMessage("WebViewDone", string.Empty, SendMessageOptions.DontRequireReceiver);
  400. }
  401. }
  402. private static void OnTitleChange(IntPtr name, IntPtr title)
  403. {
  404. }
  405. private static void OnNewWindow(IntPtr name, ref bool cancel)
  406. {
  407. }
  408. private static void OnNavigateComplete(IntPtr name, IntPtr url)
  409. {
  410. }
  411. private static WWebViewWin32.ActionDocumentComplete documentComplete;
  412. private static WWebViewWin32.ActionBeforeNavigate beforeNavigate;
  413. private static WWebViewWin32.ActionWindowClosing windowClosing;
  414. private static WWebViewWin32.ActionTitleChange titleChange;
  415. private static WWebViewWin32.ActionNewWindow newWindow;
  416. private static WWebViewWin32.ActionNavigateComplete navigateComplete;
  417. private static bool initialize;
  418. }
  419. }