InputFieldLayoutX.cs 4.3 KB

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