/**
* 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
{
///
/// `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.
///
///
/// 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()`.
///
[HelpURL("https://developer.vuplex.com/webview/CanvasWebViewPrefab")]
public class CanvasWebViewPrefab : BaseWebViewPrefab
{
///
/// 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).
///
[Label("Initial Resolution (px / Unity unit)")]
[Tooltip("You can change this to make web content appear larger or smaller.")]
[HideInInspector]
public float InitialResolution = 1;
///
/// Allows the scroll sensitivity to be adjusted.
/// The default sensitivity is 15.
///
/// [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();
}
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();
}
protected override ViewportMaterialView _getView()
{
return transform.Find("CanvasWebViewPrefabView").GetComponent();
}
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