CanvasWebViewDemo.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 CanvasWebViewDemo scene, which displays a CanvasWebViewPrefab
  18. /// inside a screen space canvas. "Native 2D Mode" is enabled on the
  19. /// CanvasWebViewPrefab, so a native 2D webview is displayed on Android and iOS.
  20. /// </summary>
  21. /// <remarks>
  22. /// Links: <br/>
  23. /// - CanvasWebViewPrefab docs: https://developer.vuplex.com/webview/CanvasWebViewPrefab <br/>
  24. /// - Native 2D Mode: https://support.vuplex.com/articles/native-2d-mode <br/>
  25. /// - How clicking works: https://support.vuplex.com/articles/clicking <br/>
  26. /// - Other examples: https://developer.vuplex.com/webview/overview#examples <br/>
  27. /// </remarks>
  28. class CanvasWebViewDemo : MonoBehaviour {
  29. CanvasWebViewPrefab _canvasWebViewPrefab;
  30. HardwareKeyboardListener _hardwareKeyboardListener;
  31. async void Start() {
  32. // The CanvasWebViewPrefab's InitialUrl property is set via the editor, so it
  33. // automatically loads that URL when it initializes.
  34. _canvasWebViewPrefab = GameObject.Find("CanvasWebViewPrefab").GetComponent<CanvasWebViewPrefab>();
  35. _setUpHardwareKeyboard();
  36. // Wait for the CanvasWebViewPrefab to initialize, because the CanvasWebViewPrefab.WebView property
  37. // is null until the prefab has initialized.
  38. await _canvasWebViewPrefab.WaitUntilInitialized();
  39. // The CanvasWebViewPrefab has initialized, so now we can use the IWebView APIs
  40. // using its CanvasWebViewPrefab.WebView property.
  41. // https://developer.vuplex.com/webview/IWebView
  42. _canvasWebViewPrefab.WebView.UrlChanged += (sender, eventArgs) => {
  43. Debug.Log("[CanvasWebViewDemo] URL changed: " + eventArgs.Url);
  44. };
  45. }
  46. void _setUpHardwareKeyboard() {
  47. // Send keys from the hardware (USB or Bluetooth) keyboard to the webview.
  48. // Use separate KeyDown() and KeyUp() methods if the webview supports
  49. // it, otherwise just use IWebView.SendKey().
  50. // https://developer.vuplex.com/webview/IWithKeyDownAndUp
  51. _hardwareKeyboardListener = HardwareKeyboardListener.Instantiate();
  52. _hardwareKeyboardListener.KeyDownReceived += (sender, eventArgs) => {
  53. var webViewWithKeyDown = _canvasWebViewPrefab.WebView as IWithKeyDownAndUp;
  54. if (webViewWithKeyDown != null) {
  55. webViewWithKeyDown.KeyDown(eventArgs.Value, eventArgs.Modifiers);
  56. } else {
  57. _canvasWebViewPrefab.WebView.SendKey(eventArgs.Value);
  58. }
  59. };
  60. _hardwareKeyboardListener.KeyUpReceived += (sender, eventArgs) => {
  61. var webViewWithKeyUp = _canvasWebViewPrefab.WebView as IWithKeyDownAndUp;
  62. webViewWithKeyUp?.KeyUp(eventArgs.Value, eventArgs.Modifiers);
  63. };
  64. }
  65. }
  66. }