如何更改编辑器脚本文本和颜色以及返回的GUILayout.Button?

时间:2019-03-20 18:59:21

标签: c# unity3d

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然后禁用按钮“搜索”,将字体颜色更改为红色,将文本更改为“无结果”。

如果myScript.objectsinfo.Length很大,则将按钮更改回黑色字体和文本“搜索”为0。

仅保留if (GUILayout.Button("Search"))一个按钮,只需根据myScript.objectsinfo.Length切换颜色和文本即可。

1 个答案:

答案 0 :(得分:1)

// 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通常变得非常复杂,我只是添加了额外的{ }来清理代码。

参考文献: