| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- /**
- * Copyright (c) 2021 Vuplex Inc. All rights reserved.
- *
- * Licensed under the Vuplex Commercial Software Library License, you may
- * not use this file except in compliance with the License. You may obtain
- * a copy of the License at
- *
- * https://vuplex.com/commercial-library-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- using System;
- using UnityEngine;
- using UnityEngine.UI;
- #if NET_4_6 || NET_STANDARD_2_0
- using System.Threading.Tasks;
- #endif
- namespace Vuplex.WebView
- {
- /// <summary>
- /// `CanvasWebViewPrefab` is a prefab that makes it easy to view and interact with web content in a Canvas.
- /// It takes care of creating an `IWebView`, displaying its texture, and handling pointer interactions
- /// from the user (i.e. clicking, dragging, and scrolling). So, all you need to do is specify a URL or HTML to load,
- /// and then the user can view and interact with it. For use outside of a Canvas, see `WebViewPrefab` instead.
- /// </summary>
- /// <remarks>
- /// There are two ways to create a `CanvasWebViewPrefab`:
- /// 1. By dragging CanvasWebViewPrefab.prefab into your scene via the editor and then setting its "Initial URL" property.
- /// 2. Or by creating an instance programmatically with `CanvasWebViewPrefab.Instantiate()`, waiting for
- /// it to initialize, and then calling methods on its `WebView` property (like `canvasWebViewPrefab.WebView.LoadUrl("https://vuplex.com")`).
- ///
- /// `CanvasWebViewPrefab` handles standard events from Unity's input event system
- /// (like `IPointerDownHandler` and `IScrollHandler`), so it works with input modules that plug into the event system,
- /// like Unity's `StandaloneInputModule` and the Oculus `OVRInputModule`.
- ///
- /// If your use case requires a high degree of customization, you can instead create an `IWebView`
- /// outside of the prefab with `Web.CreateWebView()`.
- /// </remarks>
- [HelpURL("https://developer.vuplex.com/webview/CanvasWebViewPrefab")]
- public class CanvasWebViewPrefab : BaseWebViewPrefab
- {
- /// <summary>
- /// Sets the webview's initial resolution in pixels per Unity unit.
- /// You can change the resolution to make web content appear larger or smaller.
- /// For more information on scaling web content, see
- /// [this support article](https://support.vuplex.com/articles/how-to-scale-web-content).
- /// </summary>
- [Label("Initial Resolution (px / Unity unit)")]
- [Tooltip("You can change this to make web content appear larger or smaller.")]
- [HideInInspector]
- public float InitialResolution = 1;
- /// <summary>
- /// Allows the scroll sensitivity to be adjusted.
- /// The default sensitivity is 15.
- /// </summary>
- /// [HideInInspector]
- public float ScrollingSensitivity = 15;
- [Obsolete("CanvasWebViewPrefab.Init() has been removed. The CanvasWebViewPrefab script now initializes itself automatically, so Init() no longer needs to be called.", true)]
- public void Init() { }
- [Obsolete("CanvasWebViewPrefab.Init() has been removed. The CanvasWebViewPrefab script now initializes itself automatically, so Init() no longer needs to be called.", true)]
- public void Init(WebViewOptions options) { }
- [Obsolete("CanvasWebViewPrefab.Init() has been removed. The CanvasWebViewPrefab script now initializes itself automatically, so Init() no longer needs to be called.", true)]
- public void Init(IWebView webView) { }
- RectTransform _cachedRectTransform;
- WebViewOptions _optionsForInitialization;
- RectTransform _rectTransform
- {
- get
- {
- if (_cachedRectTransform == null)
- {
- _cachedRectTransform = GetComponent<RectTransform>();
- }
- return _cachedRectTransform;
- }
- }
- bool _setCustomPointerInputDetector;
- IWebView _webViewForInitialization;
- protected override Vector2 _convertRatioPointToUnityUnits(Vector2 point)
- {
- // Use Vector2.Scale() because Vector2 * Vector2 isn't supported in Unity 2017.
- return Vector2.Scale(point, _rectTransform.rect.size);
- }
- protected override float _getInitialResolution()
- {
- return InitialResolution;
- }
- protected override float _getScrollingSensitivity()
- {
- return ScrollingSensitivity;
- }
- protected override ViewportMaterialView _getVideoLayer()
- {
- return transform.Find("VideoLayer").GetComponent<ViewportMaterialView>();
- }
- protected override ViewportMaterialView _getView()
- {
- return transform.Find("CanvasWebViewPrefabView").GetComponent<ViewportMaterialView>();
- }
- void _initCanvasPrefab()
- {
- _logMacWarningIfNeeded();
- var rect = _rectTransform.rect;
- _init(rect.width, rect.height, _optionsForInitialization, _webViewForInitialization);
- }
- void _logMacWarningIfNeeded()
- {
- if (Web.DefaultPluginType != WebPluginType.Mac)
- {
- return;
- }
- var canvas = GetComponentInParent<Canvas>();
- if (canvas == null)
- {
- return;
- }
- if (canvas.renderMode == RenderMode.ScreenSpaceOverlay)
- {
- 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().");
- }
- }
- protected override void _setVideoLayerPosition(Rect videoRect)
- {
- var videoRectTransform = _videoLayer.transform as RectTransform;
- // Use Vector2.Scale() because Vector2 * Vector2 isn't supported in Unity 2017.
- videoRectTransform.anchoredPosition = Vector2.Scale(Vector2.Scale(videoRect.position, _rectTransform.rect.size), new Vector2(1, -1));
- videoRectTransform.sizeDelta = Vector2.Scale(videoRect.size, _rectTransform.rect.size);
- }
- void Start()
- {
- _initCanvasPrefab();
- }
- void Update()
- {
- var rect = _rectTransform.rect;
- var size = _webView.Size;
- if (!(rect.width == size.x && rect.height == size.y))
- {
- _webView.Resize(rect.width, rect.height);
- }
- }
- }
- }
|