@@ -19,17 +19,17 @@ public partial class CodeExecutorWindow
1919 private VisualElement m_CodeEditor = null ;
2020
2121 /// <summary>
22- /// 代码编辑器滚动视图
22+ /// 代码滚动视图
2323 /// </summary>
2424 private ScrollView m_CodeScrollView = null ;
2525
2626 /// <summary>
27- /// 代码编辑器输入框
27+ /// 代码文本框
2828 /// </summary>
2929 private TextField m_CodeTextField = null ;
3030
3131 /// <summary>
32- /// 代码编辑器输入框
32+ /// 代码文本框
3333 /// </summary>
3434 private VisualElement m_CodeTextFieldTextInput = null ;
3535
@@ -81,8 +81,10 @@ private void InitCodeEditor()
8181 }
8282 } ;
8383 m_CodeEditor . Add ( m_CodeScrollView ) ;
84+ // 代码滚动视图形变回调
85+ m_CodeScrollView . RegisterCallback < GeometryChangedEvent > ( OnCodeScrollViewGeometryChangedEvent ) ;
8486
85- // 代码输入框
87+ // 代码文本框
8688 m_CodeTextField = new TextField ( )
8789 {
8890 name = "TextField" ,
@@ -101,7 +103,7 @@ private void InitCodeEditor()
101103 }
102104 } ;
103105 m_CodeScrollView . Add ( m_CodeTextField ) ;
104-
106+ // 样式Hack
105107 {
106108 // 输入框文本设为左上对齐
107109 VisualElement textInput = m_CodeTextFieldTextInput = m_CodeTextField . Q < VisualElement > ( "unity-text-input" ) ;
@@ -117,15 +119,11 @@ private void InitCodeEditor()
117119 textInput . style . borderBottomLeftRadius = 0 ;
118120 textInput . style . borderBottomRightRadius = 0 ;
119121 }
120-
121- // 滚动视图变化回调
122- m_CodeScrollView . RegisterCallback < GeometryChangedEvent > ( OnCodeScrollViewGeometryChangedEventChanged ) ;
123-
124- // 输入框内容变化回调
122+ // 内容变化回调
125123 m_CodeTextField . RegisterValueChangedCallback ( OnCodeTextFieldValueChanged ) ;
126- // 输入框键盘按下回调
124+ // 键盘按下回调
127125 m_CodeTextField . RegisterCallback < KeyDownEvent > ( OnCodeTextFieldKeyDown ) ;
128- // 输入框鼠标滚轮回调
126+ // 鼠标滚轮回调
129127 m_CodeTextField . RegisterCallback < WheelEvent > ( OnCodeTextFieldMouseWheel ) ;
130128
131129 // 复制到剪切板按钮
@@ -162,19 +160,22 @@ private void InitCodeEditor()
162160 }
163161
164162 /// <summary>
165- /// 滚动视图变化回调
163+ /// 代码滚动视图形变回调
166164 /// </summary>
167165 /// <param name="evt"></param>
168- private void OnCodeScrollViewGeometryChangedEventChanged ( GeometryChangedEvent evt )
166+ private void OnCodeScrollViewGeometryChangedEvent ( GeometryChangedEvent evt )
169167 {
170168 EditorApplication . delayCall += UpdateCodeTextFieldHeight ;
169+ }
171170
172- void UpdateCodeTextFieldHeight ( )
173- {
174- if ( m_CodeScrollView == null ) return ;
175- float height = m_CodeScrollView . contentViewport . localBound . height ;
176- m_CodeTextField . style . minHeight = height ;
177- }
171+ /// <summary>
172+ /// 更新代码文本元素高度
173+ /// </summary>
174+ private void UpdateCodeTextFieldHeight ( )
175+ {
176+ if ( m_CodeScrollView == null ) return ;
177+ float height = m_CodeScrollView . contentViewport . localBound . height ;
178+ m_CodeTextField . style . minHeight = height ;
178179 }
179180
180181 /// <summary>
@@ -183,7 +184,6 @@ void UpdateCodeTextFieldHeight()
183184 /// <param name="evt"></param>
184185 private void OnCodeTextFieldValueChanged ( ChangeEvent < string > evt )
185186 {
186- if ( m_CodeTextField . isReadOnly ) return ;
187187 if ( m_CurrSnippetInfo == null ) return ;
188188 // 更新数据
189189 string code = m_CodeTextField . value ;
@@ -322,10 +322,13 @@ private int SetCodeEditorFontSize(int size)
322322
323323 private void SetCodeEditorEditable ( bool isEditable , bool focus = false )
324324 {
325- // 输入框只读
325+ // 文本框只读
326326 m_CodeTextField . isReadOnly = ! isEditable ;
327- // 输入框背景颜色
328- m_CodeTextFieldTextInput . style . backgroundColor = ( isEditable ? textFieldNormalBgColor : textFieldReadOnlyBgColor ) ;
327+
328+ // 背景颜色
329+ Color bgColor = ( isEditable ? textFieldNormalBgColor : textFieldReadOnlyBgColor ) ;
330+ m_CodeScrollView . style . backgroundColor = bgColor ;
331+ m_CodeTextFieldTextInput . style . backgroundColor = bgColor ;
329332 // 边框颜色
330333 Color borderColor = ( isEditable ? textFieldNormalBorderColor : textFieldReadOnlyBorderColor ) ;
331334 m_CodeScrollView . style . borderTopColor = borderColor ;
0 commit comments