跳到主要内容

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 能获取到输入框引用。