Cute_demon_attacks/meng_yao/Assets/script/Manager/InputUI.cs
舒荣森 9082a536ce add
2024-10-31 16:50:35 +08:00

45 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class InputUI : MonoBehaviour
{
public InputField inputField;
private void Start()
{
EventTrigger trigger = inputField.gameObject.AddComponent<EventTrigger>();
// 添加点击事件
EventTrigger.Entry clickEntry = new EventTrigger.Entry();
clickEntry.eventID = EventTriggerType.PointerClick;
clickEntry.callback.AddListener((data) => { HidePlaceholder(); });
trigger.triggers.Add(clickEntry);
// 添加鼠标离开事件
EventTrigger.Entry exitEntry = new EventTrigger.Entry();
exitEntry.eventID = EventTriggerType.PointerExit;
exitEntry.callback.AddListener((data) => { ShowPlaceholder(); });
trigger.triggers.Add(exitEntry);
}
private void HidePlaceholder()
{
if (inputField.placeholder != null)
{
inputField.placeholder.gameObject.SetActive(false);
}
}
private void ShowPlaceholder()
{
if (inputField.placeholder != null && string.IsNullOrEmpty(inputField.text))
{
inputField.placeholder.gameObject.SetActive(true);
}
}
}