How to resize view when touchscreen keyboard is visible on Android with Unity?

前端 未结 3 870
你的背包
你的背包 2020-12-19 06:55

In Unity I can\'t control the touchscreen keyboard. TouchScreenKeyboard class has only one parameters for Android.

if(TouchScreenKeyboard.visible)
{ float k         


        
3条回答
  •  天命终不由人
    2020-12-19 07:29

    It was long ago, but just in case anybody is struggling with it again, I managed to find a solution for a panel with an InputField using this MonoBehaviour class. I attached it to the InputField and linked the panel which should be resized.

    public class InputFieldForScreenKeyboardPanelAdjuster : MonoBehaviour {
    
        // Assign panel here in order to adjust its height when TouchScreenKeyboard is shown
        public GameObject panel;
    
        private InputField inputField;
        private RectTransform panelRectTrans;
        private Vector2 panelOffsetMinOriginal;
        private float panelHeightOriginal;
        private float currentKeyboardHeightRatio;
    
        public void Start() {
            inputField = transform.GetComponent();
            panelRectTrans = panel.GetComponent();
            panelOffsetMinOriginal = panelRectTrans.offsetMin;
            panelHeightOriginal = panelRectTrans.rect.height;
        }
    
        public void LateUpdate () {
            if (inputField.isFocused) {
                float newKeyboardHeightRatio = GetKeyboardHeightRatio();
                if (currentKeyboardHeightRatio != newKeyboardHeightRatio) {
                    Debug.Log("InputFieldForScreenKeyboardPanelAdjuster: Adjust to keyboard height ratio: " + newKeyboardHeightRatio);
                    currentKeyboardHeightRatio = newKeyboardHeightRatio;
                    panelRectTrans.offsetMin = new Vector2(panelOffsetMinOriginal.x, panelHeightOriginal * currentKeyboardHeightRatio);
                }
            } else if (currentKeyboardHeightRatio != 0f) {
                if (panelRectTrans.offsetMin != panelOffsetMinOriginal) {
                    SmartCoroutine.DelayedExecute(this, () => {
                        Debug.Log("InputFieldForScreenKeyboardPanelAdjuster: Revert to original");
                        panelRectTrans.offsetMin = panelOffsetMinOriginal;
                    }, 0.5f);
                }
                currentKeyboardHeightRatio = 0f;
            }
        }
    
        private float GetKeyboardHeightRatio() {
            if (Application.isEditor) {
                return 0.4f; // fake TouchScreenKeyboard height ratio for debug in editor        
            }
    
    #if UNITY_ANDROID        
            using (AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
                AndroidJavaObject View = UnityClass.GetStatic("currentActivity").Get("mUnityPlayer").Call("getView");
                using (AndroidJavaObject rect = new AndroidJavaObject("android.graphics.Rect")) {
                    View.Call("getWindowVisibleDisplayFrame", rect);
                    return (float)(Screen.height - rect.Call("height")) / Screen.height;
                }
            }
    #else
            return (float)TouchScreenKeyboard.area.height / Screen.height;
    #endif
        }
    
    }
    

提交回复
热议问题