CanvasWebViewPrefab.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Copyright (c) 2021 Vuplex Inc. All rights reserved.
  3. *
  4. * Licensed under the Vuplex Commercial Software Library License, you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * https://vuplex.com/commercial-library-license
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using UnityEngine;
  18. using UnityEngine.UI;
  19. #if NET_4_6 || NET_STANDARD_2_0
  20. using System.Threading.Tasks;
  21. #endif
  22. namespace Vuplex.WebView
  23. {
  24. /// <summary>
  25. /// `CanvasWebViewPrefab` is a prefab that makes it easy to view and interact with web content in a Canvas.
  26. /// It takes care of creating an `IWebView`, displaying its texture, and handling pointer interactions
  27. /// from the user (i.e. clicking, dragging, and scrolling). So, all you need to do is specify a URL or HTML to load,
  28. /// and then the user can view and interact with it. For use outside of a Canvas, see `WebViewPrefab` instead.
  29. /// </summary>
  30. /// <remarks>
  31. /// There are two ways to create a `CanvasWebViewPrefab`:
  32. /// 1. By dragging CanvasWebViewPrefab.prefab into your scene via the editor and then setting its "Initial URL" property.
  33. /// 2. Or by creating an instance programmatically with `CanvasWebViewPrefab.Instantiate()`, waiting for
  34. /// it to initialize, and then calling methods on its `WebView` property (like `canvasWebViewPrefab.WebView.LoadUrl("https://vuplex.com")`).
  35. ///
  36. /// `CanvasWebViewPrefab` handles standard events from Unity's input event system
  37. /// (like `IPointerDownHandler` and `IScrollHandler`), so it works with input modules that plug into the event system,
  38. /// like Unity's `StandaloneInputModule` and the Oculus `OVRInputModule`.
  39. ///
  40. /// If your use case requires a high degree of customization, you can instead create an `IWebView`
  41. /// outside of the prefab with `Web.CreateWebView()`.
  42. /// </remarks>
  43. [HelpURL("https://developer.vuplex.com/webview/CanvasWebViewPrefab")]
  44. public class CanvasWebViewPrefab : BaseWebViewPrefab
  45. {
  46. /// <summary>
  47. /// Sets the webview's initial resolution in pixels per Unity unit.
  48. /// You can change the resolution to make web content appear larger or smaller.
  49. /// For more information on scaling web content, see
  50. /// [this support article](https://support.vuplex.com/articles/how-to-scale-web-content).
  51. /// </summary>
  52. [Label("Initial Resolution (px / Unity unit)")]
  53. [Tooltip("You can change this to make web content appear larger or smaller.")]
  54. [HideInInspector]
  55. public float InitialResolution = 1;
  56. /// <summary>
  57. /// Allows the scroll sensitivity to be adjusted.
  58. /// The default sensitivity is 15.
  59. /// </summary>
  60. /// [HideInInspector]
  61. public float ScrollingSensitivity = 15;
  62. [Obsolete("CanvasWebViewPrefab.Init() has been removed. The CanvasWebViewPrefab script now initializes itself automatically, so Init() no longer needs to be called.", true)]
  63. public void Init() { }
  64. [Obsolete("CanvasWebViewPrefab.Init() has been removed. The CanvasWebViewPrefab script now initializes itself automatically, so Init() no longer needs to be called.", true)]
  65. public void Init(WebViewOptions options) { }
  66. [Obsolete("CanvasWebViewPrefab.Init() has been removed. The CanvasWebViewPrefab script now initializes itself automatically, so Init() no longer needs to be called.", true)]
  67. public void Init(IWebView webView) { }
  68. RectTransform _cachedRectTransform;
  69. WebViewOptions _optionsForInitialization;
  70. RectTransform _rectTransform
  71. {
  72. get
  73. {
  74. if (_cachedRectTransform == null)
  75. {
  76. _cachedRectTransform = GetComponent<RectTransform>();
  77. }
  78. return _cachedRectTransform;
  79. }
  80. }
  81. bool _setCustomPointerInputDetector;
  82. IWebView _webViewForInitialization;
  83. protected override Vector2 _convertRatioPointToUnityUnits(Vector2 point)
  84. {
  85. // Use Vector2.Scale() because Vector2 * Vector2 isn't supported in Unity 2017.
  86. return Vector2.Scale(point, _rectTransform.rect.size);
  87. }
  88. protected override float _getInitialResolution()
  89. {
  90. return InitialResolution;
  91. }
  92. protected override float _getScrollingSensitivity()
  93. {
  94. return ScrollingSensitivity;
  95. }
  96. protected override ViewportMaterialView _getVideoLayer()
  97. {
  98. return transform.Find("VideoLayer").GetComponent<ViewportMaterialView>();
  99. }
  100. protected override ViewportMaterialView _getView()
  101. {
  102. return transform.Find("CanvasWebViewPrefabView").GetComponent<ViewportMaterialView>();
  103. }
  104. void _initCanvasPrefab()
  105. {
  106. _logMacWarningIfNeeded();
  107. var rect = _rectTransform.rect;
  108. _init(rect.width, rect.height, _optionsForInitialization, _webViewForInitialization);
  109. }
  110. void _logMacWarningIfNeeded()
  111. {
  112. if (Web.DefaultPluginType != WebPluginType.Mac)
  113. {
  114. return;
  115. }
  116. var canvas = GetComponentInParent<Canvas>();
  117. if (canvas == null)
  118. {
  119. return;
  120. }
  121. if (canvas.renderMode == RenderMode.ScreenSpaceOverlay)
  122. {
  123. WebViewLogger.LogWarning("Unity's macOS player currently has a bug that sometimes prevents 3D WebView's external textures from appearing properly in a \"Screen Space - Overlay\" Canvas (https://issuetracker.unity3d.com/issues/external-texture-is-not-visible-in-player-slash-build-when-canvas-render-mode-is-set-to-screen-space-overlay). If you encounter this issue, please either switch the Canvas's render mode to \"Screen Space - Camera\" or add a script to temporarily resize the player's window with Screen.SetResolution().");
  124. }
  125. }
  126. protected override void _setVideoLayerPosition(Rect videoRect)
  127. {
  128. var videoRectTransform = _videoLayer.transform as RectTransform;
  129. // Use Vector2.Scale() because Vector2 * Vector2 isn't supported in Unity 2017.
  130. videoRectTransform.anchoredPosition = Vector2.Scale(Vector2.Scale(videoRect.position, _rectTransform.rect.size), new Vector2(1, -1));
  131. videoRectTransform.sizeDelta = Vector2.Scale(videoRect.size, _rectTransform.rect.size);
  132. }
  133. void Start()
  134. {
  135. _initCanvasPrefab();
  136. }
  137. void Update()
  138. {
  139. var rect = _rectTransform.rect;
  140. var size = _webView.Size;
  141. if (!(rect.width == size.x && rect.height == size.y))
  142. {
  143. _webView.Resize(rect.width, rect.height);
  144. }
  145. }
  146. }
  147. }