首页 > 解决方案 > 使用正则表达式的 TextMesh Pro InputField 字符验证

问题描述

当 TextMesh Pro InputField 设置为使用 Regex 时,如何设置 Regex 字符串?

我已将其设置为使用正则表达式

inputfield.characterValidation = TMPro.TMP_InputField.CharacterValidation.Regex;

但我无法找出在哪里设置所述正则表达式字符串。

谢谢

标签: unity3d

解决方案


对于需要通过脚本设置输入字段值的,可以使用反射来设置私有变量值。

Type inputFieldType = Type.GetType("TMPro.TMP_InputField, Unity.TextMeshPro", true, true);
System.Reflection.FieldInfo regexValueFieldInfo = inputFieldType.GetField("m_RegexValue",System.Reflection.BindingFlags.NonPublic| System.Reflection.BindingFlags.Instance);
regexValueFieldInfo.SetValue(m_InputField, "your regex here");     

推荐阅读