【问题标题】:How to change GUI button highlighted color如何更改 GUI 按钮突出显示的颜色
【发布时间】:2019-07-09 23:54:31
【问题描述】:

我想在按下按钮时更改 GUI 按钮突出显示的颜色。但是,我找不到如何更改我的脚本 onGUI() 方法。

我没有显示整个代码。必要的部分如下。

这里是onGUI()方法;

if-else 检查按钮是否被点击。

if(GUI.Button(new Rect(currentPart.DrawDepth * spacing + x + subPartsSpacing, y, 200, 20), currentPart.EnglishTitle,myStyle))
{
    if (!currentPart.IsClicked)
    {
        currentPart.IsClicked = true;
        HumanBodyVisualizer.ShowMode showModeFullBody = HumanBodyVisualizer.ShowMode.Invisible;
        bodyVisualizer.ShowBody(showModeFullBody);

        AllSubPartsAndRoot.Insert(AllSubPartsAndRoot.Count, currentPart);
        addAllSubPartsOfClickButton(currentPart, AllSubPartsAndRoot, AllSubPartsAndRoot.Count - 1);
        HumanBodyVisualizer.ShowMode showModeCurrentPart = HumanBodyVisualizer.ShowMode.LowTransparent;

        for (int i = 0; i < AllSubPartsAndRoot.Count; i++)
        {
            bodyVisualizer.ShowBodyPart(showModeCurrentPart, AllSubPartsAndRoot[i]);
        }
    }
    else
    {
        currentPart.IsClicked = false;
        List<HumanBodyPart> RemoveBodyParts = new List<HumanBodyPart>();
        RemoveBodyParts.Insert(0,currentPart);
        addAllSubPartsOfClickButton(currentPart, RemoveBodyParts, 1);

        for(int i = 0; i < RemoveBodyParts.Count; i++)
        {
            if (AllSubPartsAndRoot.Contains(RemoveBodyParts[i]))
            {      
                bodyVisualizer.ShowBodyPart(HumanBodyVisualizer.ShowMode.Invisible, RemoveBodyParts[i]);
                AllSubPartsAndRoot.Remove(RemoveBodyParts[i]);
            }
        }

        if(AllSubPartsAndRoot.Count == 0)
        {
            bodyVisualizer.ShowBody(HumanBodyVisualizer.ShowMode.LowTransparent);
        }
        else
        {
            for (int ii = 0; ii < AllSubPartsAndRoot.Count; ii++)
            {
                bodyVisualizer.ShowBodyPart(HumanBodyVisualizer.ShowMode.LowTransparent, AllSubPartsAndRoot[ii]);
            }    
        }   
    }
}

【问题讨论】:

  • 你的myStyle在哪里?
  • ` private GUIStyle myStyle = new GUIStyle();` myStyle.normal.textColor = Color.white; myStyle.alignment = TextAnchor.MiddleLeft; myStyle.fontSize = 14; myStyle.fontStyle = FontStyle.Normal;

标签: c# unity3d


【解决方案1】:

您正在使用自定义GUIStyle

您可以通过onActive 更改pressed 样式

元素打开和按下时的渲染设置。

还有active

按下控件时的渲染设置。

GUIStyle的组件

但是你需要一个Texture2D来为它输入

myStyle.onActive.background = SomeTexture2D;

很遗憾,无法直接更改 GUIStyle 的颜色。


注意:为了更轻松地配置您的 GUIStyle,您可以简单地在 ProjectView &rightarrow; 中创建一个资产。 右击 &rightarrow; Create &rightarrow; GUISkin

通过 Inspector 在此处进行所有设置,只需在 EditorScript 中引用此 GUISkin 资产并使用 GUI.skin = myReferencedGuiSkin;


或者,您可以尝试使用按钮打开标志。

您可以使用Event.current 并检查type 是否为EventType.MouseUpmousePosition 是否在您用于按钮的rect 之外以重置标志。 (不确定这是否可行)

private bool pressed;
private bool hovered;

...

public void OnGUI()
{
    var color = GUI.color;
    var rect = new Rect(...);
    var ev = Event.current;

    hovered = ev.mousePosition.x > rect.x && ev.mousePosition.x < rect.x + rect.width && ev.mousePosition.y > rect.y && ev.mousePosition.y < rect.y + rect.height;

    if (Event.current.type == EventType.MouseUp) pressed = false;
    else if (ev.type == EventType.MouseDown) pressed = true;

    if (hovered && pressed) GUI.color = Color.green;
    if (GUI.Button(rect, ...))
    {
        ...
    }
    GUI.color = color;

    // in order to receive the mouse events
    Repaint();
}

如果这是一个 EditorWindow,您必须确保将 wantsMouseMove 设置为 true 才能接收鼠标事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 2010-12-12
    相关资源
    最近更新 更多