CanvasKeyboard.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. namespace Vuplex.WebView
  20. {
  21. /// <summary>
  22. /// Like the Keyboard component, except optimized for use in a Canvas.
  23. /// You can add a CanvasKeyboard to your scene by dragging CanvasKeyboard.prefab
  24. /// into a Canvas via the editor or by programmatically calling `CanvasKeyboard.Instantiate()`.
  25. /// For an example, please see the CanvasWorldSpaceDemo scene.
  26. /// </summary>
  27. /// <example>
  28. /// // Create a CanvasKeyboard.
  29. /// var keyboard = CanvasKeyboard.Instantiate();
  30. /// keyboard.transform.SetParent(canvas.transform, false);
  31. /// var rectTransform = keyboard.transform as RectTransform;
  32. /// rectTransform.anchoredPosition3D = Vector3.zero;
  33. /// rectTransform.offsetMin = Vector2.zero;
  34. /// rectTransform.offsetMax = Vector2.zero;
  35. /// rectTransform.sizeDelta = new Vector2(650, 162);
  36. /// // Hook up the keyboard so that characters are routed to a CanvasWebViewPrefab in the scene.
  37. /// keyboard.InputReceived += (sender, eventArgs) => {
  38. /// canvasWebViewPrefab.WebView.HandleKeyboardInput(eventArgs.Value);
  39. /// };
  40. /// </example>
  41. public class CanvasKeyboard : BaseKeyboard
  42. {
  43. /// <summary>
  44. /// Sets the keyboard's initial resolution in pixels per Unity unit.
  45. /// You can change the resolution to make the keyboard's content appear larger or smaller.
  46. /// For more information on scaling web content, see
  47. /// [this support article](https://support.vuplex.com/articles/how-to-scale-web-content).
  48. /// </summary>
  49. [Label("Initial Resolution (px / Unity unit)")]
  50. [Tooltip("You can change this to make web content appear larger or smaller.")]
  51. public float InitialResolution = 1;
  52. /// <summary>
  53. /// The `CanvasWebViewPrefab` instance used for the keyboard UI.
  54. /// </summary>
  55. public CanvasWebViewPrefab WebViewPrefab
  56. {
  57. get
  58. {
  59. return (CanvasWebViewPrefab)_webViewPrefab;
  60. }
  61. }
  62. void _initCanvasKeyboard()
  63. {
  64. var canvasWebViewPrefab = gameObject.GetComponentInChildren<CanvasWebViewPrefab>();
  65. if (!canvasWebViewPrefab) return;
  66. _webViewPrefab = canvasWebViewPrefab;
  67. _webViewPrefab.transform.SetParent(transform, false);
  68. _webViewPrefab.gameObject.layer = gameObject.layer;
  69. var rectTransform = _webViewPrefab.transform as RectTransform;
  70. rectTransform.anchoredPosition3D = Vector3.zero;
  71. rectTransform.offsetMin = Vector2.zero;
  72. rectTransform.offsetMax = Vector2.zero;
  73. _webViewPrefab.transform.localScale = Vector3.one;
  74. canvasWebViewPrefab.InitialResolution = InitialResolution;
  75. _init();
  76. // Disable the image, which is just used as a placeholder in the editor.
  77. var image = GetComponent<Image>();
  78. if (image != null)
  79. {
  80. image.enabled = false;
  81. }
  82. }
  83. void Start()
  84. {
  85. _initCanvasKeyboard();
  86. }
  87. }
  88. }