【问题标题】:Unity Inspector Text Label at PositionUnity Inspector 位置处的文本标签
【发布时间】:2018-07-08 09:38:25
【问题描述】:

我正在尝试修饰我的 Inspector,但在尝试在 Inspector 的指定位置绘制文本时遇到了很多麻烦。我在下面的示例检查器上绘制了一个彩色矩形,并希望在其上绘制文本(如标签)。

如何在上面显示的“X”位置绘制文本/标签?我不确定从哪里开始。任何帮助将不胜感激。


实施答案:

public override void OnGUI(Rect position)
{
    GUIStyle myStyle = new GUIStyle();

    myStyle.fontSize = 16;
    myStyle.alignment = TextAnchor.UpperLeft;
    myStyle.padding.top = 5;
    myStyle.padding.left = -3;
    myStyle.fontStyle = FontStyle.Bold;

    Color32 color = colorSpacer.drawColor; // Custom Property Attribute
    EditorGUI.DrawRect(new Rect(position.x-11, position.y, position.width+11, position.height-2), color);
    Rect r = GUILayoutUtility.GetLastRect();
    EditorGUI.LabelField(r, "Section Header", myStyle);
}

【问题讨论】:

  • 请显示您的自定义检查器的实际代码,以便我可以指出在哪里添加插入标签的代码。

标签: user-interface unity3d


【解决方案1】:

如果您使用EditorGUI 绘制自定义编辑器,则可以使用LabelFieldPrefixLabelSelectableLabelHandlePrefixLabel 方法,具体取决于您所需的具体行为。

如果您使用EditorGUILayout 绘图,则有这些方法的等效方法。

此外,HeaderAtribute 装饰器无需自定义编辑器即可完成此操作。

【讨论】:

  • EditorGUI.LabelField 正是我所需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多