killapp/Assets/Editor/EnableHideMobileInput.cs
“虞渠成” e93cc36d33 补充提交
2026-09-11 09:48:44 +08:00

65 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 批量勾选项目中所有 Prefab 里 InputField 的 Hide Mobile Input 选项
/// 菜单Tools/UI/勾选所有输入框的 Hide Mobile Input
/// 说明:
/// 1. shouldHideMobileInput 属性在编辑器下非移动平台getter 恒返回 true
/// 因此直接操作序列化字段 m_HideMobileInputinspector 勾选框实际写入的字段)
/// 2. 使用 LoadPrefabContents/SaveAsPrefabAsset 打开并保存,
/// 确保 YAML 真正写盘LoadAssetAtPath + SaveAssets 不够可靠)
/// </summary>
public static class EnableHideMobileInput
{
[MenuItem("Tools/UI/勾选所有输入框的 Hide Mobile Input")]
public static void EnableAll()
{
var log = new StringBuilder();
int files = 0, fields = 0;
foreach (var guid in AssetDatabase.FindAssets("t:Prefab", new[] { "Assets" }))
{
string path = AssetDatabase.GUIDToAssetPath(guid);
var contents = PrefabUtility.LoadPrefabContents(path);
try
{
int changed = 0;
foreach (var field in contents.GetComponentsInChildren<InputField>(true))
{
var so = new SerializedObject(field);
var prop = so.FindProperty("m_HideMobileInput");
if (prop != null && !prop.boolValue)
{
prop.boolValue = true;
so.ApplyModifiedProperties();
changed++;
}
}
if (changed > 0)
{
PrefabUtility.SaveAsPrefabAsset(contents, path);
files++;
fields += changed;
log.AppendLine($"{path}{changed} 个");
}
}
finally
{
PrefabUtility.UnloadPrefabContents(contents);
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
if (files == 0)
Debug.Log("所有输入框的 Hide Mobile Input 均已勾选,无需修改。");
else
Debug.Log($"处理完成:{files} 个 Prefab 共 {fields} 个输入框已勾选。\n{log}");
}
}