InputFieldLayoutX.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. public class InputFieldLayoutX : MonoBehaviour
  5. {
  6. public InputField inputField;//输入框组件
  7. public RectTransform moveRect;//输入框组件ui框,同时也用于移动
  8. public MaskableGraphic noTransparentOnFocused; //聚焦时,图像不透明
  9. private static Material _NoTransparentMaterial;
  10. public static Material NoTransparentMaterial {
  11. get {
  12. if (!_NoTransparentMaterial) _NoTransparentMaterial = new Material(Shader.Find("JC/UI/InputFieldLayoutX_NoTransparent"));
  13. return _NoTransparentMaterial;
  14. }
  15. }
  16. bool isFocused = false;
  17. Vector2 moveRectOriginAnchoredPos;
  18. Vector2 moveRectOriginPos;
  19. float keyboardHeight_last = 0;
  20. void Start()
  21. {
  22. if (inputField == null) {
  23. inputField = GetComponentInChildren<InputField>();
  24. }
  25. if (moveRect == null) {
  26. moveRect = GetComponent<RectTransform>();
  27. }
  28. // Debug.Log($"Screen.height:{Screen.height}");
  29. // Debug.Log($"systemHeight:{Display.main.systemHeight}");
  30. // Debug.Log($"renderingHeight:{Display.main.renderingHeight}");
  31. // Debug.Log($"TouchScreenKeyboard.isSupported:{TouchScreenKeyboard.isSupported}");
  32. // Debug.Log($"moveRect.anchoredPosition:{moveRect.anchoredPosition}"); //本地坐标
  33. // Debug.Log($"moveRect.position:{moveRect.position}"); //屏幕坐标-跟canvas-scale无关
  34. }
  35. private void Update()
  36. {
  37. if (isFocused != inputField.isFocused)
  38. {
  39. isFocused = inputField.isFocused;
  40. if (isFocused) onGetFocused();
  41. else onLostFocused();
  42. }
  43. }
  44. void onGetFocused()
  45. {
  46. moveRectOriginAnchoredPos = moveRect.anchoredPosition;
  47. moveRectOriginPos = moveRect.position;
  48. StartCoroutine(DelayGetKeyboardHeight());
  49. if (noTransparentOnFocused) noTransparentOnFocused.material = NoTransparentMaterial;
  50. }
  51. void onLostFocused()
  52. {
  53. keyboardHeight_last = 0;
  54. moveRect.anchoredPosition = moveRectOriginAnchoredPos;
  55. if (noTransparentOnFocused) noTransparentOnFocused.material = null;
  56. }
  57. IEnumerator DelayGetKeyboardHeight()
  58. {
  59. yield return null;
  60. while (inputField.isFocused)
  61. {
  62. float kb_height = GetKeyboardHeight();
  63. if(kb_height > 0 && kb_height != keyboardHeight_last)
  64. {
  65. keyboardHeight_last = kb_height;
  66. SetInputUIHeight(kb_height);
  67. }
  68. yield return new WaitForSecondsRealtime(0.333f);
  69. }
  70. }
  71. void SetInputUIHeight(float keyboardHeight)
  72. {
  73. float canvasHeight = gameObject.GetComponentInParent<CanvasScaler>().GetComponent<RectTransform>().sizeDelta.y;
  74. keyboardHeight = canvasHeight * keyboardHeight / Screen.height;
  75. Vector3 posInCanvas = moveRectOriginPos * canvasHeight / Screen.height;
  76. float moveRectHeight = posInCanvas.y - moveRect.rect.height * moveRect.pivot.y;
  77. if (keyboardHeight > moveRectHeight)
  78. {
  79. moveRect.anchoredPosition = moveRectOriginAnchoredPos + Vector2.up * (keyboardHeight - moveRectHeight);
  80. }
  81. }
  82. float GetKeyboardHeight()
  83. {
  84. #if !UNITY_EDITOR && UNITY_ANDROID
  85. int decorHeight = 0; //输入框高度
  86. int freeHeight = 0; //除去键盘后的高度
  87. using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  88. {
  89. using (var currentActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity"))
  90. {
  91. using (var unityPlayer = currentActivity.Get<AndroidJavaObject>("mUnityPlayer"))
  92. {
  93. using (var view = unityPlayer.Call<AndroidJavaObject>("getView"))
  94. {
  95. using (var rect = new AndroidJavaObject("android.graphics.Rect"))
  96. {
  97. view.Call("getWindowVisibleDisplayFrame", rect);
  98. freeHeight = rect.Call<int>("height");
  99. }
  100. }
  101. using (var dialog = unityPlayer.Get<AndroidJavaObject>("mSoftInputDialog"))
  102. {
  103. using (var window = dialog.Call<AndroidJavaObject>("getWindow"))
  104. {
  105. using (var decorView = window.Call<AndroidJavaObject>("getDecorView"))
  106. {
  107. decorHeight = decorView.Call<int>("getHeight");
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. return Screen.height - freeHeight + decorHeight;
  115. #elif !UNITY_EDITOR && UNITY_IOS
  116. return TouchScreenKeyboard.area.height;
  117. #elif UNITY_EDITOR
  118. return Screen.height * 0.7625f;
  119. #else
  120. return 0;
  121. #endif
  122. }
  123. }