项目作者: willnode

项目描述 :
Exposed Unity Editor WebView API
高级语言: C#
项目地址: git://github.com/willnode/WebViewHook.git
创建时间: 2018-01-23T11:54:41Z
项目社区:https://github.com/willnode/WebViewHook

开源协议:MIT License

下载


WebViewHook

Warning: WebViewHook is obsolete for Unity 2020 and Above due to removal of Asset Store Browsing in Unity Editor. This project is soon to be archived.

This is a code snippet to access a hidden WebView API from Unity Editor, and load it to EditorWindow.

Screenshot

Code

Download WebViewHook. See usage example in WebWindow or WebEditor. You may download the whole project if necessary.

Usage

Minimum code to get WebView working:

  1. class WebViewDemo : EditorWindow
  2. {
  3. WebViewHook webView;
  4. void OnEnable()
  5. {
  6. if (!webView)
  7. {
  8. // create webView
  9. webView = CreateInstance<WebViewHook>();
  10. }
  11. }
  12. public void OnBecameInvisible()
  13. {
  14. if (webView)
  15. {
  16. // signal the browser to unhook
  17. webView.Detach();
  18. }
  19. }
  20. void OnDestroy()
  21. {
  22. //Destroy web view
  23. DestroyImmediate(webView);
  24. }
  25. void OnGUI()
  26. {
  27. // hook to this window
  28. if (webView.Hook(this))
  29. // do the first thing to do
  30. webView.LoadURL("https://google.com");
  31. if (ev.type == EventType.Repaint)
  32. {
  33. // keep the browser aware with resize
  34. webView.OnGUI(new Rect(Vector2.zero, this.position.size));
  35. }
  36. }
  37. }

Two Way Communication

Using LoadURL, LoadHTML and ExecuteJavascript you only can send information to WebView.

To extract informations from WebView you need external communication, and the easiest way to do this is by using Web Socket.

To get web socket server running you need to download WebSocket-Sharp and a script utility from this repo.

See WebData for minimal working example of using WebSocketHook to hook C# into javascript variable.

More Information and Caveats

This is an editor only solution. If you’re looking to add WebView to a game build then this is not the repo you’re looking for.

The technology behind WebView is Chrome Embedded Framework, version 37. In Windows it’s contained inside the gigantic libcef.dll.

WebView in Unity is just like Chrome, with background-color default to darkgrey and no plugins (Flash/Java/PDF) allowed.

I solving bugs as I can, but of course limited. If you can’t open a specific website, mostly comes down to the fact that Unity haven’t upgrade their CEF browser.