CanvasPopupDemo.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 CanvasPopupDemo scene, which demonstrates how to use
  18. /// the IWithPopups interface with CanvasWebViewPrefab.
  19. /// </summary>
  20. /// <remarks>
  21. /// Links: <br/>
  22. /// - CanvasWebViewPrefab docs: https://developer.vuplex.com/webview/CanvasWebViewPrefab <br/>
  23. /// - IWithPopups: https://developer.vuplex.com/webview/IWithPopups <br/>
  24. /// - How clicking works: https://support.vuplex.com/articles/clicking <br/>
  25. /// - Other examples: https://developer.vuplex.com/webview/overview#examples <br/>
  26. /// </remarks>
  27. class CanvasPopupDemo : MonoBehaviour {
  28. GameObject _canvas;
  29. CanvasWebViewPrefab _focusedPrefab;
  30. HardwareKeyboardListener _hardwareKeyboardListener;
  31. async void Start() {
  32. _canvas = GameObject.Find("Canvas");
  33. // Create a webview for the main content.
  34. var mainWebViewPrefab = CanvasWebViewPrefab.Instantiate();
  35. mainWebViewPrefab.Resolution = 1.5f;
  36. mainWebViewPrefab.PixelDensity = 2;
  37. mainWebViewPrefab.Native2DModeEnabled = true;
  38. mainWebViewPrefab.transform.SetParent(_canvas.transform, false);
  39. var rectTransform = mainWebViewPrefab.transform as RectTransform;
  40. rectTransform.anchoredPosition3D = Vector3.zero;
  41. rectTransform.offsetMin = Vector2.zero;
  42. rectTransform.offsetMax = Vector2.zero;
  43. mainWebViewPrefab.transform.localScale = Vector3.one;
  44. _focusedPrefab = mainWebViewPrefab;
  45. _setUpKeyboards();
  46. // Wait for the CanvasWebViewPrefab to initialize, because the CanvasWebViewPrefab.WebView property
  47. // is null until the prefab has initialized.
  48. await mainWebViewPrefab.WaitUntilInitialized();
  49. // The CanvasWebViewPrefab has initialized, so now we can use its WebViewPrefab.WebView property.
  50. var webViewWithPopups = mainWebViewPrefab.WebView as IWithPopups;
  51. if (webViewWithPopups == null) {
  52. mainWebViewPrefab.WebView.LoadHtml(NOT_SUPPORTED_HTML);
  53. return;
  54. }
  55. Debug.Log("Loading Pinterest as an example because it uses popups for third party login. Click 'Login', then select Facebook or Google to open a popup for authentication.");
  56. mainWebViewPrefab.WebView.LoadUrl("https://pinterest.com");
  57. webViewWithPopups.SetPopupMode(PopupMode.LoadInNewWebView);
  58. webViewWithPopups.PopupRequested += async (webView, eventArgs) => {
  59. Debug.Log("Popup opened with URL: " + eventArgs.Url);
  60. var popupPrefab = CanvasWebViewPrefab.Instantiate(eventArgs.WebView);
  61. popupPrefab.Resolution = mainWebViewPrefab.Resolution;
  62. _focusedPrefab = popupPrefab;
  63. popupPrefab.transform.SetParent(_canvas.transform, false);
  64. var popupRectTransform = popupPrefab.transform as RectTransform;
  65. popupRectTransform.anchoredPosition3D = Vector3.zero;
  66. popupRectTransform.offsetMin = Vector2.zero;
  67. popupRectTransform.offsetMax = Vector2.zero;
  68. popupPrefab.transform.localScale = Vector3.one;
  69. // Place the popup in front of the main webview.
  70. var localPosition = popupPrefab.transform.localPosition;
  71. localPosition.z = 0.1f;
  72. popupPrefab.transform.localPosition = localPosition;
  73. await popupPrefab.WaitUntilInitialized();
  74. popupPrefab.WebView.CloseRequested += (popupWebView, closeEventArgs) => {
  75. Debug.Log("Closing the popup");
  76. _focusedPrefab = mainWebViewPrefab;
  77. popupPrefab.Destroy();
  78. };
  79. };
  80. }
  81. void _setUpKeyboards() {
  82. // Send keys from the hardware (USB or Bluetooth) keyboard to the webview.
  83. // Use separate KeyDown() and KeyUp() methods if the webview supports
  84. // it, otherwise just use IWebView.SendKey().
  85. // https://developer.vuplex.com/webview/IWithKeyDownAndUp
  86. _hardwareKeyboardListener = HardwareKeyboardListener.Instantiate();
  87. _hardwareKeyboardListener.KeyDownReceived += (sender, eventArgs) => {
  88. var webViewWithKeyDown = _focusedPrefab.WebView as IWithKeyDownAndUp;
  89. if (webViewWithKeyDown != null) {
  90. webViewWithKeyDown.KeyDown(eventArgs.Value, eventArgs.Modifiers);
  91. } else {
  92. _focusedPrefab.WebView.SendKey(eventArgs.Value);
  93. }
  94. };
  95. _hardwareKeyboardListener.KeyUpReceived += (sender, eventArgs) => {
  96. var webViewWithKeyUp = _focusedPrefab.WebView as IWithKeyDownAndUp;
  97. webViewWithKeyUp?.KeyUp(eventArgs.Value, eventArgs.Modifiers);
  98. };
  99. }
  100. const string NOT_SUPPORTED_HTML = @"
  101. <body>
  102. <style>
  103. body {
  104. font-family: sans-serif;
  105. display: flex;
  106. justify-content: center;
  107. align-items: center;
  108. line-height: 1.25;
  109. }
  110. div {
  111. max-width: 80%;
  112. }
  113. li {
  114. margin: 10px 0;
  115. }
  116. </style>
  117. <div>
  118. <p>
  119. Sorry, but this 3D WebView package doesn't support yet the <a href='https://developer.vuplex.com/webview/IWithPopups'>IWithPopups</a> interface. Current packages that support popups:
  120. </p>
  121. <ul>
  122. <li>
  123. <a href='https://developer.vuplex.com/webview/StandaloneWebView'>3D WebView for Windows and macOS</a>
  124. </li>
  125. <li>
  126. <a href='https://developer.vuplex.com/webview/AndroidWebView'>3D WebView for Android</a>
  127. </li>
  128. <li>
  129. <a href='https://developer.vuplex.com/webview/AndroidGeckoWebView'>3D WebView for Android with Gecko Engine</a>
  130. </li>
  131. </ul>
  132. </div>
  133. </body>
  134. ";
  135. }
  136. }