当触摸屏键盘在具有 Unity 的 Android 上可见时,如何调整视图大小
本文关键字:何调整 调整 视图 键盘 触摸屏 Unity Android | 更新日期: 2023-09-27 18:32:25
在Unity中,我无法控制触摸屏键盘。TouchScreenKeyboard 类只有一个 Android 参数。
if(TouchScreenKeyboard.visible)
{ float keyboardHeight = TouchScreenKeyboard.area.height;
// will resize the view here! But this return zero!
}
有没有其他方法可以知道安卓键盘的高度?
这应该可以解决问题(在这里找到):
public int GetKeyboardSize()
{
using(AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
AndroidJavaObject View = UnityClass.GetStatic<AndroidJavaObject>("currentActivity").Get<AndroidJavaObject>("mUnityPlayer").Call<AndroidJavaObject>("getView");
using(AndroidJavaObject Rct = new AndroidJavaObject("android.graphics.Rect"))
{
View.Call("getWindowVisibleDisplayFrame", Rct);
return Screen.height - Rct.Call<int>("height");
}
}
}
那是很久以前的事了,但以防万一有人再次为此苦苦挣扎,我设法为面板找到了解决方案一个使用此 MonoBehavior 类的 InputField。我将其附加到输入字段并链接了应调整大小的面板。
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<InputField>();
panelRectTrans = panel.GetComponent<RectTransform>();
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<AndroidJavaObject>("currentActivity").Get<AndroidJavaObject>("mUnityPlayer").Call<AndroidJavaObject>("getView");
using (AndroidJavaObject rect = new AndroidJavaObject("android.graphics.Rect")) {
View.Call("getWindowVisibleDisplayFrame", rect);
return (float)(Screen.height - rect.Call<int>("height")) / Screen.height;
}
}
#else
return (float)TouchScreenKeyboard.area.height / Screen.height;
#endif
}
}
我让它相对于画布大小工作,这是代码......您所需要的只是使用画布 RectTransform 引用来调用它。
public static int GetRelativeKeyboardHeight(RectTransform rectTransform, bool includeInput)
{
int keyboardHeight = GetKeyboardHeight(includeInput);
float screenToRectRatio = Screen.height / rectTransform.rect.height;
float keyboardHeightRelativeToRect = keyboardHeight / screenToRectRatio;
return (int) keyboardHeightRelativeToRect;
}
private static int GetKeyboardHeight(bool includeInput)
{
#if UNITY_EDITOR
return 0;
#elif UNITY_ANDROID
using (AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
AndroidJavaObject unityPlayer = unityClass.GetStatic<AndroidJavaObject>("currentActivity").Get<AndroidJavaObject>("mUnityPlayer");
AndroidJavaObject view = unityPlayer.Call<AndroidJavaObject>("getView");
AndroidJavaObject dialog = unityPlayer.Get<AndroidJavaObject>("mSoftInputDialog");
if (view == null || dialog == null)
return 0;
var decorHeight = 0;
if (includeInput)
{
AndroidJavaObject decorView = dialog.Call<AndroidJavaObject>("getWindow").Call<AndroidJavaObject>("getDecorView");
if (decorView != null)
decorHeight = decorView.Call<int>("getHeight");
}
using (AndroidJavaObject rect = new AndroidJavaObject("android.graphics.Rect"))
{
view.Call("getWindowVisibleDisplayFrame", rect);
return Screen.height - rect.Call<int>("height") + decorHeight;
}
}
#elif UNITY_IOS
return (int)TouchScreenKeyboard.area.height;
#endif
}