我如何更改guilayout.button in Editor脚本文本,颜色和背部


using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(GameObjectInfo))]
public class GameObjectInfoButton : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
        GameObjectInfo myScript = (GameObjectInfo)target;
        var style = new GUIStyle(GUI.skin.button);
        style.normal.textColor = Color.red;
        if (myScript.useButton == false)
        {
            GUI.enabled = false;
        }
        else
        {
            GUI.enabled = true;
        }
        if(myScript.objectsinfo.Length == 0)
        {
        }
        else
        {
        }
        if (GUILayout.Button("Search"))
        {
            myScript.Search();
        }
        GUILayout.Space(70);
        if (GUILayout.Button("Compare"))
        {
        }
    }
}

我想要如果myScript.objectsinfo.Length == 0,然后禁用按钮"搜索"更改字体颜色以将文本更改为" no result"。

,如果myScript.Objectsinfo.length很大,则0将按钮更改为黑色字体和文本"搜索"。

并仅保留一个按钮if (GUILayout.Button("Search"))只需根据myScript.Objectsinfo.length切换颜色和文本。

// Disable the Button
EditorGUI.BeginDisabledGroup(myScript.objectsinfo.Length == 0);
{
    // change fontColor
    var originalFontColor = GUI.contentColor;
    if(myScript.objectsinfo.Length == 0) GUI.contentColor = Color.Red;
    {
        // Change the text
        if (GUILayout.Button(myScript.objectsinfo.Length == 0 ? "No Results" :"Search"))
        {
                myScript.Search();
        }
    }
    // reset back to normal color
    GUI.contentColor = originalFontColor;
}
EditorGUI.EndDisabledGroup();

我只是添加其他{ }即可清洁代码,因为EditorCode通常会变得非常复杂。

参考:

  • gui.ContentColor
  • editorgui.begindisabledgroup&editorgui.enddisabledgroup
  • ?(也是三元)操作员

最新更新