| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- 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<InputField>();
- }
- if (moveRect == null) {
- moveRect = GetComponent<RectTransform>();
- }
- // 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<CanvasScaler>().GetComponent<RectTransform>().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<AndroidJavaObject>("currentActivity"))
- {
- using (var unityPlayer = currentActivity.Get<AndroidJavaObject>("mUnityPlayer"))
- {
-
- using (var view = unityPlayer.Call<AndroidJavaObject>("getView"))
- {
- using (var rect = new AndroidJavaObject("android.graphics.Rect"))
- {
- view.Call("getWindowVisibleDisplayFrame", rect);
- freeHeight = rect.Call<int>("height");
- }
- }
- using (var dialog = unityPlayer.Get<AndroidJavaObject>("mSoftInputDialog"))
- {
- using (var window = dialog.Call<AndroidJavaObject>("getWindow"))
- {
- using (var decorView = window.Call<AndroidJavaObject>("getDecorView"))
- {
- decorHeight = decorView.Call<int>("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
- }
- }
|