Unity 2022.1.0b10中TextArea/Multiline检视面板显示异常问题
Unity 2022.1.0b10 版本TextArea/Multiline特性失效修复
问题原因
这是Unity 2022.1.0b10.2818 beta版本的原生编辑器BUG,和代码编写方式无关。
该版本Inspector的IMGUI绘制模块存在逻辑错误,计算带TextArea、Multiline特性的字符串字段高度时会错误返回0,直接导致多行输入区域被判定为0高度隐藏,最终面板仅显示字段名称,输入功能异常。
复现用最简代码:
[TextArea] public string sds;
正常逻辑下上述代码会在Inspector生成可随内容向下扩展的多行文本输入框,BUG触发时输入框完全消失,仅保留字段名标签。
解决方法
- 优先选择:更换Unity版本
beta版本本身为测试用途,存在大量未修复的编辑器功能BUG,禁止用于正式项目开发。直接升级到2022.1后续正式补丁版本,或切换到2021LTS这类长期支持稳定版,不需要修改任何业务代码,TextArea和Multiline功能会自动恢复正常。 - 临时适配(必须使用该特定beta版本时采用)
编写自定义PropertyDrawer覆盖原生TextArea绘制逻辑,手动修正高度计算错误。将以下脚本放入项目内任意Editor文件夹下即可生效:
using UnityEditor; using UnityEngine; [CustomPropertyDrawer(typeof(TextAreaAttribute))] public class FixedTextAreaDrawer : PropertyDrawer { public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { TextAreaAttribute attr = attribute as TextAreaAttribute; float lineHeight = EditorGUIUtility.singleLineHeight; float padding = EditorGUIUtility.standardVerticalSpacing * 2; int currentLines = property.stringValue?.Split('\n').Length ?? attr.minLines; int validLines = Mathf.Clamp(currentLines, attr.minLines, attr.maxLines); return lineHeight * validLines + padding; } public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); property.stringValue = EditorGUI.TextArea(position, label, property.stringValue); EditorGUI.EndProperty(); } }
如果是[Multiline]特性出现同类问题,处理逻辑完全一致:把上述代码中的特性类型替换为MultilineAttribute,绘制方法替换为带多行参数的EditorGUI.TextField重载即可。
- 应急方案(仅临时调试可用):给对应字段添加
[Space(固定高度值)]特性强制占位,该方式高度固定,无法随文本内容长度自适应,不建议长期使用。
内容的提问来源于stack exchange,提问作者piscopancer
相关产品推荐
相关产品推荐

