You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用EditorStyles修改xNode节点中列表的背景纹理

修改xNode节点内列表背景纹理的解决方案

要修改xNode节点中列表的背景纹理,核心是找到Unity Editor中负责列表背景的GUIStyle,并在节点编辑器的生命周期中临时替换它,避免影响其他UI元素。以下是具体实现步骤和修改后的代码:

关键说明

xNode绘制List/Array类型属性时,默认依赖Unity的EditorStyles.textArea作为列表区域的背景样式(部分场景也会用到EditorStyles.helpBox,但前者更常见)。我们需要像你处理numberField、objectField一样,保存原始样式,修改后再恢复。

修改后的BaseNodeEditor代码

using UnityEditor;
using UnityEngine;
using XNodeEditor;

[CustomNodeEditor(typeof(BaseNode))]
public class BaseNodeEditor : NodeEditor
{
    private static GUIStyle gl_label_normal;
    private static GUIStyle gl_label_bold;
    private static GUIStyle gl_header;
    private static GUIStyle gl_numberField;
    private static GUIStyle gl_objectField;
    // 新增:保存列表背景的原始样式
    private static GUIStyle gl_textArea;

    public override void OnBodyGUI()
    {
        if (gl_label_normal == null) gl_label_normal = new GUIStyle(EditorStyles.label);
        if (gl_label_bold == null) gl_label_bold = new GUIStyle(EditorStyles.boldLabel);
        if (gl_numberField == null) gl_numberField = new GUIStyle(EditorStyles.numberField);
        if (gl_objectField == null) gl_objectField = new GUIStyle(EditorStyles.objectField);
        // 初始化保存textArea的原始样式
        if (gl_textArea == null) gl_textArea = new GUIStyle(EditorStyles.textArea);

        // 替换列表背景样式
        EditorStyles.textArea.normal.background = GetBackgroundTexture("#00000080");
        // 可选:设置列表的文本颜色等其他样式
        EditorStyles.textArea.normal.textColor = Color.white;

        // 你原有的样式修改
        EditorStyles.objectField.normal.background = GetBackgroundTexture("#00000080");
        EditorStyles.numberField.normal.background = GetBackgroundTexture("#00000080");
        EditorStyles.numberField.normal.textColor = Color.white;

        EditorStyles.label.focused.textColor = Color.blue;
        EditorStyles.label.hover.textColor = Color.black;
        EditorStyles.label.normal.textColor = Color.black;
        EditorStyles.boldLabel.normal.textColor = Color.black;
        EditorStyles.boldLabel.hover.textColor = Color.black;

        base.OnBodyGUI();

        // 恢复所有原始样式
        EditorStyles.label.normal = gl_label_normal.normal;
        EditorStyles.boldLabel.normal = gl_label_bold.normal;
        EditorStyles.numberField.normal = gl_numberField.normal;
        EditorStyles.objectField.normal = gl_objectField.normal;
        // 恢复textArea的原始样式
        EditorStyles.textArea.normal = gl_textArea.normal;
    }
    
    public override void OnHeaderGUI()
    {
        if (gl_header == null) gl_header = new GUIStyle(NodeEditorResources.styles.nodeHeader);
        NodeEditorResources.styles.nodeHeader.normal.textColor = Color.black;
        base.OnHeaderGUI();
        NodeEditorResources.styles.nodeHeader.normal = gl_header.normal;
    }
    
    private Texture2D GetBackgroundTexture(string hexColor) {
        Color color = Color.white;
        ColorUtility.TryParseHtmlString(hexColor, out color);
        Texture2D texture = new Texture2D(1, 1);
        texture.SetPixel(0, 0, color);
        texture.Apply();
        return texture;
    }
}

备选方案:手动绘制列表属性

如果上述方法不生效(比如xNode使用了自定义的列表绘制逻辑),可以在OnBodyGUI中手动遍历并绘制列表属性,指定自定义GUIStyle:

// 在base.OnBodyGUI()之前或之后添加(如果要完全替换默认绘制,注释掉base.OnBodyGUI())
SerializedProperty listProp = serializedObject.FindProperty("你的列表字段名");
if (listProp != null)
{
    // 创建自定义列表样式
    GUIStyle customListStyle = new GUIStyle(EditorStyles.textArea);
    customListStyle.normal.background = GetBackgroundTexture("#00000080");
    customListStyle.normal.textColor = Color.white;

    // 绘制列表
    EditorGUILayout.PropertyField(listProp, true);
    // 如果需要更精细控制,展开列表绘制每个元素
    if (listProp.isExpanded)
    {
        for (int i = 0; i < listProp.arraySize; i++)
        {
            EditorGUILayout.PropertyField(listProp.GetArrayElementAtIndex(i), GUIContent.none, true);
        }
    }
    serializedObject.ApplyModifiedProperties();
}

内容的提问来源于stack exchange,提问作者Ary Guilherme Pires Caramez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 14:57:48