CanvasWorldSpaceDemo.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 CanvasWorldSpaceDemo scene, which displays a CanvasWebViewPrefab
  18. /// in a world space canvas.
  19. /// </summary>
  20. class CanvasWorldSpaceDemo : MonoBehaviour {
  21. CanvasWebViewPrefab _canvasWebViewPrefab;
  22. HardwareKeyboardListener _hardwareKeyboardListener;
  23. void Awake() {
  24. // Use a desktop User-Agent to request the desktop versions of websites.
  25. // https://developer.vuplex.com/webview/Web#SetUserAgent
  26. // Call this from Awake() to ensure it's called before the webview initializes.
  27. Web.SetUserAgent(false);
  28. }
  29. async void Start() {
  30. // The CanvasWebViewPrefab's InitialUrl property is set via the editor, so it
  31. // automatically loads that URL when it initializes.
  32. _canvasWebViewPrefab = GameObject.Find("CanvasWebViewPrefab").GetComponent<CanvasWebViewPrefab>();
  33. _setupKeyboards();
  34. // Wait for the CanvasWebViewPrefab to initialize, because the CanvasWebViewPrefab.WebView property
  35. // is null until the prefab has initialized.
  36. await _canvasWebViewPrefab.WaitUntilInitialized();
  37. // The CanvasWebViewPrefab has initialized, so now we can use the IWebView APIs
  38. // using its CanvasWebViewPrefab.WebView property.
  39. // https://developer.vuplex.com/webview/IWebView
  40. _canvasWebViewPrefab.WebView.UrlChanged += (sender, eventArgs) => {
  41. Debug.Log("[CanvasWorldSpaceDemo] URL changed: " + eventArgs.Url);
  42. };
  43. }
  44. void _setupKeyboards() {
  45. // Send keys from the hardware (USB or Bluetooth) keyboard to the webview.
  46. // Use separate KeyDown() and KeyUp() methods if the webview supports
  47. // it, otherwise just use IWebView.SendKey().
  48. // https://developer.vuplex.com/webview/IWithKeyDownAndUp
  49. _hardwareKeyboardListener = HardwareKeyboardListener.Instantiate();
  50. _hardwareKeyboardListener.KeyDownReceived += (sender, eventArgs) => {
  51. var webViewWithKeyDown = _canvasWebViewPrefab.WebView as IWithKeyDownAndUp;
  52. if (webViewWithKeyDown != null) {
  53. webViewWithKeyDown.KeyDown(eventArgs.Value, eventArgs.Modifiers);
  54. } else {
  55. _canvasWebViewPrefab.WebView.SendKey(eventArgs.Value);
  56. }
  57. };
  58. _hardwareKeyboardListener.KeyUpReceived += (sender, eventArgs) => {
  59. var webViewWithKeyUp = _canvasWebViewPrefab.WebView as IWithKeyDownAndUp;
  60. webViewWithKeyUp?.KeyUp(eventArgs.Value, eventArgs.Modifiers);
  61. };
  62. // Also hook up the on-screen keyboard.
  63. var keyboard = GameObject.FindObjectOfType<CanvasKeyboard>();
  64. keyboard.InputReceived += (sender, eventArgs) => {
  65. _canvasWebViewPrefab.WebView.SendKey(eventArgs.Value);
  66. };
  67. }
  68. }
  69. }