unity输入法 遮挡输入框

参考

https://blog.csdn.net/linxinfa/article/details/106537858
https://my.oschina.net/u/698044/blog/4319949

代码

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class InputFieldLayoutX : MonoBehaviour
{
    public InputField inputField;//输入框本身
    public RectTransform inputFieldRect;//输入框外围
    public RectTransform moveRect;//要抬升的对象
    bool isFocused = false;
    Vector2 moveRectOriginPos;
    Vector2 screenPos;
    int keyboardHeight_last = 0;
    // Start is called before the first frame update
    void Start()
    {
        moveRectOriginPos = moveRect.anchoredPosition;
        //Deug.Log($"systemHeight:{Display.main.systemHeight} renderingHeight:{Display.main.renderingHeight}");
        //Deug.Log($"Screen.height:{Screen.height}");
        //Deug.Log($"TouchScreenKeyboard.isSupported:{TouchScreenKeyboard.isSupported}");
        //Deug.Log($"moveRect.anchoredPosition:{moveRectOriginPos}");
    }
    private void Update()
    {
        if (isFocused != inputField.isFocused)
        {
            isFocused = inputField.isFocused;
            if (isFocused)
            {
                onGetFocused();
            }
            else
            {
                onLostFocused(); 
                isFocused = inputField.isFocused;
            }
        }
    }

    IEnumerator DelayGetKeyboardHeight()
    {
        yield return null;
        while (inputField.isFocused)
        {
            int keyboardHeight_local = GetKeyboardHeight();
            if(keyboardHeight_local != 0 )
            {
                if(keyboardHeight_local != keyboardHeight_last)
                {
                    keyboardHeight_last = keyboardHeight_local;
                    SetInputUIHeight(keyboardHeight_local);
                }
                else
                {
                    yield break;
                }
            }
            yield return null;
        }
    }

    void SetInputUIHeight(int _keyboardHeight)
    {
        float keyboardHeight = ((float)_keyboardHeight) * Display.main.systemHeight / Screen.height;
        float k = gameObject.GetComponentInParent<CanvasScaler>().GetComponent<RectTransform>().sizeDelta.y;
        //Deug.Log($"SetInputUIHeight k:{k}");
        float keyboardHeightUi = keyboardHeight * k / Display.main.systemHeight;
        //pirntLog($"SetInputUIHeight keyboardHeightUi:{keyboardHeightUi}");

        screenPos = RectTransformUtility.WorldToScreenPoint(Camera.main, inputFieldRect.transform.position);
        screenPos.y -= inputFieldRect.GetComponent<RectTransform>().rect.height / 2;
        //Deug.Log($"SetInputUIHeight screenPos.y:{screenPos.y}");
        if (keyboardHeightUi > screenPos.y)
        {
            keyboardHeightUi = keyboardHeightUi - screenPos.y;
            moveRect.anchoredPosition = moveRectOriginPos+Vector2.up * keyboardHeightUi;
        }
    }

    void onGetFocused(int _keyboardHeight_local=0)
    {
        //Deug.Log($"InputField.onGetFocused");
        StartCoroutine(DelayGetKeyboardHeight());
    }

    void onLostFocused()
    {
        //Deug.Log($"InputField.onLostFocused");
        keyboardHeight_last = 0;
        moveRect.anchoredPosition = moveRectOriginPos;
    }
       
    int GetKeyboardHeight()
    {
#if !UNITY_EDITOR && UNITY_ANDROID
        using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                var unityPlayer = unityClass.GetStatic<AndroidJavaObject>("currentActivity").Get<AndroidJavaObject>("mUnityPlayer");
                var view = unityPlayer.Call<AndroidJavaObject>("getView");
                var dialog = unityPlayer.Get<AndroidJavaObject>("b");

                if (view == null || dialog == null)
                {
                    return 0;
                }

                var decorHeight = 0;

                if (true)//includeInput
                {
                    var decorView = dialog.Call<AndroidJavaObject>("getWindow").Call<AndroidJavaObject>("getDecorView");

                    if (decorView != null)
                        decorHeight = decorView.Call<int>("getHeight");
                }

                using (var rect = new AndroidJavaObject("android.graphics.Rect"))
                {
                    view.Call("getWindowVisibleDisplayFrame", rect);
                    return Display.main.systemHeight - rect.Call<int>("height") + decorHeight;
                }
            }
#elif !UNITY_EDITOR && UNITY_IOS
        return TouchScreenKeyboard.area.height
#else
        return 0;
#endif
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿海-程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值