CanvasKeyboard.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 System;
  15. using UnityEngine;
  16. using UnityEngine.Serialization;
  17. using UnityEngine.UI;
  18. using Vuplex.WebView.Internal;
  19. namespace Vuplex.WebView {
  20. /// <summary>
  21. /// Like the Keyboard prefab, except optimized for use in a Canvas.
  22. /// You can add a CanvasKeyboard to your scene either by dragging the CanvasKeyboard.prefab file
  23. /// into a Canvas via the editor or by programmatically calling CanvasKeyboard.Instantiate().
  24. /// For an example, please see 3D WebView's CanvasWorldSpaceDemo scene.
  25. /// </summary>
  26. /// <remarks>
  27. /// Important note: 2D WebView for WebGL doesn't support CanvasKeyboard due to a browser limitation
  28. /// where clicking on the keyboard causes it to steal focus from webviews.
  29. /// </remarks>
  30. public class CanvasKeyboard : BaseKeyboard {
  31. /// <summary>
  32. /// Sets the keyboard's initial resolution in pixels per Unity unit.
  33. /// You can change the resolution to make the keyboard's content appear larger or smaller.
  34. /// For more information on scaling web content, see
  35. /// [this support article](https://support.vuplex.com/articles/how-to-scale-web-content).
  36. /// </summary>
  37. [Label("Resolution (px / Unity unit)")]
  38. [Tooltip("You can change this to make web content appear larger or smaller.")]
  39. [FormerlySerializedAs("InitialResolution")]
  40. public float Resolution = 1;
  41. /// <summary>
  42. /// Gets the WebViewPrefab used for the keyboard UI, or `null` if
  43. /// the keyboard hasn't finished initializing yet.
  44. /// You can use WaitUntilInitialized() to detect when the WebViewPrefab property is ready to use.
  45. /// </summary>
  46. /// <example>
  47. /// <code>
  48. /// await keyboard.WaitUntilInitialized();
  49. /// keyboard.WebViewPrefab.Clicked += (sender, eventArgs) => {
  50. /// Debug.Log("Keyboard was clicked");
  51. /// };
  52. /// </code>
  53. /// </example>
  54. public CanvasWebViewPrefab WebViewPrefab { get => (CanvasWebViewPrefab)_webViewPrefab; }
  55. /// <summary>
  56. /// Creates a new instance.
  57. /// </summary>
  58. /// <example>
  59. /// <code>
  60. /// // Create a CanvasKeyboard.
  61. /// var keyboard = CanvasKeyboard.Instantiate();
  62. /// keyboard.transform.SetParent(canvas.transform, false);
  63. /// var rectTransform = keyboard.transform as RectTransform;
  64. /// rectTransform.anchoredPosition3D = Vector3.zero;
  65. /// rectTransform.offsetMin = Vector2.zero;
  66. /// rectTransform.offsetMax = Vector2.zero;
  67. /// rectTransform.sizeDelta = new Vector2(650, 162);
  68. /// </code>
  69. /// </example>
  70. public static CanvasKeyboard Instantiate() {
  71. var prefabPrototype = (GameObject)Resources.Load("CanvasKeyboard");
  72. var gameObject = (GameObject)Instantiate(prefabPrototype);
  73. return gameObject.GetComponent<CanvasKeyboard>();
  74. }
  75. void _initCanvasKeyboard() {
  76. var canvasWebViewPrefab = CanvasWebViewPrefab.Instantiate(_webViewOptions);
  77. _webViewPrefab = canvasWebViewPrefab;
  78. _webViewPrefab.transform.SetParent(transform, false);
  79. _setLayerRecursively(_webViewPrefab.gameObject, gameObject.layer);
  80. var rectTransform = _webViewPrefab.transform as RectTransform;
  81. rectTransform.anchoredPosition3D = Vector3.zero;
  82. rectTransform.offsetMin = Vector2.zero;
  83. rectTransform.offsetMax = Vector2.zero;
  84. _webViewPrefab.transform.localScale = Vector3.one;
  85. canvasWebViewPrefab.Resolution = Resolution;
  86. _init();
  87. // Disable the image, which is just used as a placeholder in the editor.
  88. var image = GetComponent<Image>();
  89. if (image != null) {
  90. image.enabled = false;
  91. }
  92. }
  93. void Start() => _initCanvasKeyboard();
  94. // Added in v3.12, deprecated in v4.0.
  95. [Obsolete("CanvasKeyboard.InitialResolution is now deprecated. Please use CanvasKeyboard.Resolution instead.")]
  96. public float InitialResolution {
  97. get => Resolution;
  98. set => Resolution = value;
  99. }
  100. }
  101. }