WebBrowser.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.Reflection;
  5. namespace Unity3D2Babylon
  6. {
  7. public sealed class WebBrowser : ScriptableObject
  8. {
  9. public static Type WebViewType = Tools.GetTypeFromAllAssemblies("WebView");
  10. public static Type WebScriptObjectType = Tools.GetTypeFromAllAssemblies("WebScriptObject");
  11. private ScriptableObject internalWebView;
  12. private MethodInfo dockedGetterMethod;
  13. private EditorWindow parentWin;
  14. private object hostView;
  15. public WebBrowser()
  16. {
  17. this.hostView = null;
  18. this.parentWin = null;
  19. this.internalWebView = null;
  20. this.dockedGetterMethod = null;
  21. }
  22. public bool CreateView(EditorWindow parent)
  23. {
  24. return AttachView(parent, ScriptableObject.CreateInstance(WebBrowser.WebViewType), true);
  25. }
  26. public bool AttachView(EditorWindow parent, ScriptableObject webView, bool initialize = false)
  27. {
  28. this.parentWin = parent;
  29. this.internalWebView = webView;
  30. if (this.internalWebView != null)
  31. {
  32. this.hostView = Tools.GetReflectionField<object>(parent, "m_Parent");
  33. this.dockedGetterMethod = this.parentWin.GetType().GetProperty("docked", Tools.FullBinding).GetGetMethod(true);
  34. if (this.hostView != null && dockedGetterMethod != null)
  35. {
  36. if (initialize)
  37. {
  38. Rect initViewRect = new Rect(0, 20, this.parentWin.position.width, this.parentWin.position.height - ((this.IsDocked()) ? 20 : 40));
  39. this.InitWebView(this.hostView, (int)initViewRect.x, (int)initViewRect.y, (int)initViewRect.width, (int)initViewRect.height, false);
  40. this.SetHideFlags(HideFlags.HideAndDontSave);
  41. this.AllowRightClickMenu(true);
  42. }
  43. }
  44. else
  45. {
  46. throw new Exception("Failed to get parent window or docked property");
  47. }
  48. }
  49. return (this.internalWebView != null);
  50. }
  51. public bool IsValid()
  52. {
  53. return (this.internalWebView != null);
  54. }
  55. public bool IsDocked()
  56. {
  57. return (this.parentWin != null && dockedGetterMethod != null) ? (bool)(dockedGetterMethod.Invoke(this.parentWin, null)) : false;
  58. }
  59. public bool HasFocus()
  60. {
  61. return (this.parentWin != null) ? Tools.GetReflectionProperty<bool>(this.parentWin, "hasFocus") : false;
  62. }
  63. public ScriptableObject GetWebView()
  64. {
  65. return this.internalWebView;
  66. }
  67. public object GetHostView()
  68. {
  69. return this.hostView;
  70. }
  71. public Type GetViewType()
  72. {
  73. return this.internalWebView.GetType();
  74. }
  75. public void OnDestroy()
  76. {
  77. UnityEngine.Object.DestroyImmediate(this.internalWebView, true);
  78. }
  79. public void SetHideFlags(HideFlags flags)
  80. {
  81. this.internalWebView.hideFlags = flags;
  82. }
  83. public void DestroyWebView()
  84. {
  85. Tools.CallReflectionMethod(this.internalWebView, "DestroyWebView");
  86. }
  87. public void InitWebView(object host, int x, int y, int width, int height, bool showResizeHandle)
  88. {
  89. Tools.CallReflectionMethod(this.internalWebView, "InitWebView", host, x, y, width, height, showResizeHandle);
  90. }
  91. public void ExecuteJavascript(string scriptCode)
  92. {
  93. Tools.CallReflectionMethod(this.internalWebView, "ExecuteJavascript", scriptCode);
  94. }
  95. public void LoadURL(string url)
  96. {
  97. Tools.CallReflectionMethod(this.internalWebView, "LoadURL", url);
  98. }
  99. public void LoadFile(string path)
  100. {
  101. Tools.CallReflectionMethod(this.internalWebView, "LoadFile", path);
  102. }
  103. public bool DefineScriptObject(string path, ScriptableObject obj)
  104. {
  105. return Tools.CallReflectionMethod<bool>(this.internalWebView, "DefineScriptObject", path, obj);
  106. }
  107. public void SetDelegateObject(ScriptableObject value)
  108. {
  109. Tools.CallReflectionMethod(this.internalWebView, "SetDelegateObject", value);
  110. }
  111. public void SetHostView(object view)
  112. {
  113. Tools.CallReflectionMethod(this.internalWebView, "SetHostView", view);
  114. }
  115. public void SetSizeAndPosition(int x, int y, int width, int height)
  116. {
  117. Tools.CallReflectionMethod(this.internalWebView, "SetSizeAndPosition", x, y, width, height);
  118. }
  119. public void SetFocus(bool value)
  120. {
  121. Tools.CallReflectionMethod(this.internalWebView, "SetFocus", value);
  122. }
  123. public bool HasApplicationFocus()
  124. {
  125. return Tools.CallReflectionMethod<bool>(this.internalWebView, "HasApplicationFocus");
  126. }
  127. public void SetApplicationFocus(bool applicationFocus)
  128. {
  129. Tools.CallReflectionMethod(this.internalWebView, "SetApplicationFocus", applicationFocus);
  130. }
  131. public void Show()
  132. {
  133. Tools.CallReflectionMethod(this.internalWebView, "Show");
  134. }
  135. public void Hide()
  136. {
  137. Tools.CallReflectionMethod(this.internalWebView, "Hide");
  138. }
  139. public void Back()
  140. {
  141. Tools.CallReflectionMethod(this.internalWebView, "Back");
  142. }
  143. public void Forward()
  144. {
  145. Tools.CallReflectionMethod(this.internalWebView, "Forward");
  146. }
  147. public void SendOnEvent(string jsonStr)
  148. {
  149. Tools.CallReflectionMethod(this.internalWebView, "SendOnEvent", jsonStr);
  150. }
  151. public void Reload()
  152. {
  153. Tools.CallReflectionMethod(this.internalWebView, "Reload");
  154. }
  155. public void AllowRightClickMenu(bool allowRightClickMenu)
  156. {
  157. Tools.CallReflectionMethod(this.internalWebView, "AllowRightClickMenu", allowRightClickMenu);
  158. }
  159. public void ShowDevTools()
  160. {
  161. Tools.CallReflectionMethod(this.internalWebView, "ShowDevTools");
  162. }
  163. public void ToggleMaximize()
  164. {
  165. Tools.CallReflectionMethod(this.internalWebView, "ToggleMaximize");
  166. }
  167. public static void OnDomainReload()
  168. {
  169. Tools.CallStaticReflectionMethod(WebBrowser.WebViewType, "OnDomainReload");
  170. }
  171. }
  172. }