SimpleWebViewDemo.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2022 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using UnityEngine;
  15. namespace Vuplex.WebView.Demos {
  16. /// <summary>
  17. /// Sets up the SimpleWebViewDemo scene, which displays a WebViewPrefab
  18. /// with an on-screen keyboard in world space.
  19. /// </summary>
  20. /// <remarks>
  21. /// Links: <br/>
  22. /// - WebViewPrefab docs: https://developer.vuplex.com/webview/WebViewPrefab <br/>
  23. /// - How clicking works: https://support.vuplex.com/articles/clicking <br/>
  24. /// - Other examples: https://developer.vuplex.com/webview/overview#examples <br/>
  25. /// </remarks>
  26. class SimpleWebViewDemo : MonoBehaviour {
  27. HardwareKeyboardListener _hardwareKeyboardListener;
  28. WebViewPrefab _webViewPrefab;
  29. void Awake() {
  30. // Use a desktop User-Agent to request the desktop versions of websites.
  31. // https://developer.vuplex.com/webview/Web#SetUserAgent
  32. // Call this from Awake() to ensure it's called before the webview initializes.
  33. Web.SetUserAgent(false);
  34. }
  35. async void Start() {
  36. // The WebViewPrefab's InitialUrl property is set via the editor, so it
  37. // automatically loads that URL when it initializes.
  38. _webViewPrefab = GameObject.Find("WebViewPrefab").GetComponent<WebViewPrefab>();
  39. _setUpKeyboards();
  40. // Wait for the WebViewPrefab to initialize, because the WebViewPrefab.WebView property
  41. // is null until the prefab has initialized.
  42. await _webViewPrefab.WaitUntilInitialized();
  43. // The WebViewPrefab has initialized, so now we can use the IWebView APIs
  44. // using its WebViewPrefab.WebView property.
  45. // https://developer.vuplex.com/webview/IWebView
  46. _webViewPrefab.WebView.UrlChanged += (sender, eventArgs) => {
  47. Debug.Log("[SimpleWebViewDemo] URL changed: " + eventArgs.Url);
  48. };
  49. }
  50. void _setUpKeyboards() {
  51. // Send keys from the hardware (USB or Bluetooth) keyboard to the webview.
  52. // Use separate KeyDown() and KeyUp() methods if the webview supports
  53. // it, otherwise just use IWebView.SendKey().
  54. // https://developer.vuplex.com/webview/IWithKeyDownAndUp
  55. _hardwareKeyboardListener = HardwareKeyboardListener.Instantiate();
  56. _hardwareKeyboardListener.KeyDownReceived += (sender, eventArgs) => {
  57. var webViewWithKeyDown = _webViewPrefab.WebView as IWithKeyDownAndUp;
  58. if (webViewWithKeyDown != null) {
  59. webViewWithKeyDown.KeyDown(eventArgs.Value, eventArgs.Modifiers);
  60. } else {
  61. _webViewPrefab.WebView.SendKey(eventArgs.Value);
  62. }
  63. };
  64. _hardwareKeyboardListener.KeyUpReceived += (sender, eventArgs) => {
  65. var webViewWithKeyUp = _webViewPrefab.WebView as IWithKeyDownAndUp;
  66. webViewWithKeyUp?.KeyUp(eventArgs.Value, eventArgs.Modifiers);
  67. };
  68. // Also hook up the on-screen keyboard.
  69. var keyboard = GameObject.FindObjectOfType<Keyboard>();
  70. keyboard.InputReceived += (sender, eventArgs) => {
  71. _webViewPrefab.WebView.SendKey(eventArgs.Value);
  72. };
  73. }
  74. }
  75. }