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.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); } } }