using System.Collections; using UnityEngine; using UnityEngine.UI; public class InputFieldLayoutX : MonoBehaviour { public InputField inputField;//输入框组件 public RectTransform moveRect;//输入框组件ui框,同时也用于移动 public MaskableGraphic noTransparentOnFocused; //聚焦时,图像不透明 private static Material _NoTransparentMaterial; public static Material NoTransparentMaterial { get { if (!_NoTransparentMaterial) _NoTransparentMaterial = new Material(Shader.Find("JC/UI/InputFieldLayoutX_NoTransparent")); return _NoTransparentMaterial; } } bool isFocused = false; Vector2 moveRectOriginAnchoredPos; Vector2 moveRectOriginPos; float keyboardHeight_last = 0; void Start() { if (inputField == null) { inputField = GetComponentInChildren(); } if (moveRect == null) { moveRect = GetComponent(); } // Debug.Log($"Screen.height:{Screen.height}"); // Debug.Log($"systemHeight:{Display.main.systemHeight}"); // Debug.Log($"renderingHeight:{Display.main.renderingHeight}"); // Debug.Log($"TouchScreenKeyboard.isSupported:{TouchScreenKeyboard.isSupported}"); // Debug.Log($"moveRect.anchoredPosition:{moveRect.anchoredPosition}"); //本地坐标 // Debug.Log($"moveRect.position:{moveRect.position}"); //屏幕坐标-跟canvas-scale无关 } private void Update() { if (isFocused != inputField.isFocused) { isFocused = inputField.isFocused; if (isFocused) onGetFocused(); else onLostFocused(); } } void onGetFocused() { moveRectOriginAnchoredPos = moveRect.anchoredPosition; moveRectOriginPos = moveRect.position; StartCoroutine(DelayGetKeyboardHeight()); if (noTransparentOnFocused) noTransparentOnFocused.material = NoTransparentMaterial; } void onLostFocused() { keyboardHeight_last = 0; moveRect.anchoredPosition = moveRectOriginAnchoredPos; if (noTransparentOnFocused) noTransparentOnFocused.material = null; } IEnumerator DelayGetKeyboardHeight() { yield return null; while (inputField.isFocused) { float kb_height = GetKeyboardHeight(); if(kb_height > 0 && kb_height != keyboardHeight_last) { keyboardHeight_last = kb_height; SetInputUIHeight(kb_height); } yield return new WaitForSecondsRealtime(0.333f); } } void SetInputUIHeight(float keyboardHeight) { float canvasHeight = gameObject.GetComponentInParent().GetComponent().sizeDelta.y; keyboardHeight = canvasHeight * keyboardHeight / Screen.height; Vector3 posInCanvas = moveRectOriginPos * canvasHeight / Screen.height; float moveRectHeight = posInCanvas.y - moveRect.rect.height * moveRect.pivot.y; if (keyboardHeight > moveRectHeight) { moveRect.anchoredPosition = moveRectOriginAnchoredPos + Vector2.up * (keyboardHeight - moveRectHeight); } } float GetKeyboardHeight() { #if !UNITY_EDITOR && UNITY_ANDROID int decorHeight = 0; //输入框高度 int freeHeight = 0; //除去键盘后的高度 using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { using (var currentActivity = unityClass.GetStatic("currentActivity")) { using (var unityPlayer = currentActivity.Get("mUnityPlayer")) { using (var view = unityPlayer.Call("getView")) { using (var rect = new AndroidJavaObject("android.graphics.Rect")) { view.Call("getWindowVisibleDisplayFrame", rect); freeHeight = rect.Call("height"); } } using (var dialog = unityPlayer.Get("mSoftInputDialog")) { using (var window = dialog.Call("getWindow")) { using (var decorView = window.Call("getDecorView")) { decorHeight = decorView.Call("getHeight"); } } } } } } return Screen.height - freeHeight + decorHeight; #elif !UNITY_EDITOR && UNITY_IOS return TouchScreenKeyboard.area.height; #elif UNITY_EDITOR return Screen.height * 0.7625f; #else return 0; #endif } }