Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Getting the real TouchScreenKeyboard.area.height

Discussion in 'iOS and tvOS' started by Bersaelor, Jun 10, 2023.

  1. Bersaelor

    Bersaelor

    Joined:
    Oct 8, 2016
    Posts:
    110
    When I check TouchScreenKeyboard.area.height on an iPhone 12, I get 993, even though my screen is only 844 points high. Is the keyboard hight in pixels when everywhere else we use points?

    Code (CSharp):
    1. TouchScreenKeyboard.area: (x:0.00, y:1539.00, width:1170.00, height:993.00)
    Looks a lot like pixels and not points to me (iPhone 12 has 3 pixels per point). But EditorGUIUtility.pixelsPerPoint is not available at runtime, so now I'm a little stuck.
    The `UnityEngine.UI` system seems to use points everywhere else, why not for TouchScreenKeyboard.area ?
    It hides the retina details from us devs professionally when we lay out UI elements.

    How is everyone else calculating the real keyboard height on an iPhone?
     
  2. Bersaelor

    Bersaelor

    Joined:
    Oct 8, 2016
    Posts:
    110
    oh, ok, so what worked was to divide `TouchScreenKeyboard.area.height / Screen.height` and the multiply it by the size in pts of a RecTransform that is full screen.

    So

    Code (CSharp):
    1.    private float keyboardHeightInPoints()
    2.     {
    3.         float keyboardRatio = GetPixelKeyboardHeight(true) / Screen.height;
    4.         float rectHeight = GetComponent<RectTransform>().rect.height;
    5.         return keyboardRatio * rectHeight;
    6.     }
    7.  
    8.  
    where that method for iOS is:
    Code (CSharp):
    1.     private static int GetPixelKeyboardHeight(bool includeInput)
    2.     {
    3. #if UNITY_EDITOR
    4.         return 0;
    5. #elif UNITY_ANDROID
    6.         using (AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    7.         {
    8.              // ...
    9.         }
    10. #elif UNITY_IOS
    11.         return (int)TouchScreenKeyboard.area.height;
    12. #endif
    13.     }