跳至主要内容

InputFieldPlaceholderRemover

重要 注意 提醒

Coding Style wiki


InputFieldPlaceholderRemoverButtonSystem 附帶的輸入框小工具 (MonoBehaviour 組件,詳見模塊介紹),掛載於 InputField (Legacy)TMP_InputField 同一物件上,於輸入框聚焦 (編輯) 時自動隱藏 Placeholder,離開聚焦後恢復顯示;純組件行為,掛上即生效,無需任何 API 呼叫

命名空間OxGKit.ButtonSystem
類型public class InputFieldPlaceholderRemover : MonoBehaviour
原始碼InputFieldPlaceholderRemover.cs
using OxGKit.ButtonSystem;

快速上手

  1. InputField (Legacy)TMP_InputField 所在物件上 Add Component -> InputFieldPlaceholderRemover
  2. 完成,進入 Play Mode 點擊輸入框,Placeholder 會於聚焦時自動隱藏、失焦時恢復顯示。

也可於運行時動態掛載:

using OxGKit.ButtonSystem;
using TMPro;
using UnityEngine;

public class SetupInputField : MonoBehaviour
{
public TMP_InputField inputField;

private void Awake()
{
// 掛載於輸入框同一物件上即生效
this.inputField.gameObject.AddComponent<InputFieldPlaceholderRemover>();
}
}

通用規則

支持的輸入框類型

輸入框實現方式
TMP_InputFieldonSelect / onDeselect 事件驅動:選取時隱藏 Placeholder,取消選取時恢復顯示。
InputField (Legacy)isFocusedUpdate 每幀輪詢:聚焦時隱藏 Placeholder,失焦時恢復顯示。

掛載規則

  • 組件於 Awake 自動抓取同一 GameObject 上的 InputFieldTMP_InputField 參考,必須與輸入框掛在同一物件上 (掛在父物件或子物件上無效)。
  • 本組件無公開 API 成員,行為全自動,無需 (也無法) 進行參數配置。

注意 輸入框需正確指定 Placeholder (UGUI 預設結構即有),組件是以 placeholder.gameObject.SetActive 進行隱藏/顯示控制。

提醒 於運行時動態掛載時,請確保輸入框組件已存在於物件上再 AddComponent,讓 Awake 能抓取到輸入框參考。