Shegotwet/Assets/Scripts/Util/MobileKeyboardHandlerBottom...

47 lines
1.3 KiB
C#
Raw Normal View History

2025-09-27 08:41:58 +00:00
using UnityEngine;
using TMPro;
public class MobileKeyboardHandlerBottomPivot : MonoBehaviour
{
public TMP_InputField inputField;
public RectTransform canvasRect;
private Vector2 originalAnchoredPos;
private RectTransform inputRect;
[Range(0.2f, 0.5f)]
public float keyboardHeightRatio = 0.35f;
void Awake()
{
inputRect = inputField.GetComponent<RectTransform>();
originalAnchoredPos = inputRect.anchoredPosition;
inputField.onSelect.AddListener(OnInputSelected);
inputField.onDeselect.AddListener(OnInputDeselected);
}
void OnInputSelected(string text)
{
StartCoroutine(MoveInputAboveKeyboard());
}
void OnInputDeselected(string text)
{
// 원래 자리로 복원
inputRect.anchoredPosition = originalAnchoredPos;
}
System.Collections.IEnumerator MoveInputAboveKeyboard()
{
// 키보드가 뜨는 동안 잠시 대기
yield return new WaitForSeconds(0.1f);
float canvasHeight = canvasRect.rect.height;
float estimatedKeyboardHeight = canvasHeight * keyboardHeightRatio;
Vector2 newPos = inputRect.anchoredPosition;
newPos.y = estimatedKeyboardHeight + inputRect.rect.height + 10; // 10px 여유
inputRect.anchoredPosition = newPos;
}
}