CanvasWebViewDemo.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (c) 2024 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. using Vuplex.WebView;
  16. namespace Vuplex.Demos {
  17. /// <summary>
  18. /// Provides a simple example of using 3D WebView's scripting APIs
  19. /// with a CanvasWebViewPrefab. Native 2D Mode is enabled on the CanvasWebViewPrefab,
  20. /// so a native 2D webview is displayed on Android and iOS.
  21. /// </summary>
  22. /// <remarks>
  23. /// Links: <br/>
  24. /// - CanvasWebViewPrefab docs: https://developer.vuplex.com/webview/CanvasWebViewPrefab <br/>
  25. /// - Native 2D Mode: https://support.vuplex.com/articles/native-2d-mode <br/>
  26. /// - How clicking works: https://support.vuplex.com/articles/clicking <br/>
  27. /// - Other examples: https://developer.vuplex.com/webview/overview#examples <br/>
  28. /// </remarks>
  29. class CanvasWebViewDemo : MonoBehaviour {
  30. CanvasWebViewPrefab canvasWebViewPrefab;
  31. async void Start() {
  32. // Get a reference to the CanvasWebViewPrefab.
  33. // https://support.vuplex.com/articles/how-to-reference-a-webview
  34. canvasWebViewPrefab = GameObject.Find("CanvasWebViewPrefab").GetComponent<CanvasWebViewPrefab>();
  35. // Wait for the prefab to initialize because its WebView property is null until then.
  36. // https://developer.vuplex.com/webview/WebViewPrefab#WaitUntilInitialized
  37. await canvasWebViewPrefab.WaitUntilInitialized();
  38. // After the prefab has initialized, you can use the IWebView APIs via its WebView property.
  39. // https://developer.vuplex.com/webview/IWebView
  40. canvasWebViewPrefab.WebView.UrlChanged += (sender, eventArgs) => {
  41. Debug.Log("[CanvasWebViewDemo] URL changed: " + eventArgs.Url);
  42. };
  43. }
  44. }
  45. }