C# 如何更改GUI按钮高亮显示的颜色
我想在按下按钮时更改GUI按钮高亮显示的颜色。但是,我找不到如何更改脚本onGUI()方法 我没有显示全部代码。必要的部分如下 这里是onGUI()方法 否则,检查按钮是否被单击C# 如何更改GUI按钮高亮显示的颜色,c#,unity3d,C#,Unity3d,我想在按下按钮时更改GUI按钮高亮显示的颜色。但是,我找不到如何更改脚本onGUI()方法 我没有显示全部代码。必要的部分如下 这里是onGUI()方法 否则,检查按钮是否被单击 if(GUI.Button(new Rect(currentPart.DrawDepth * spacing + x + subPartsSpacing, y, 200, 20), currentPart.EnglishTitle,myStyle)) { if (!currentPart.IsClicked)
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]);
}
}
}
}
if(GUI.按钮(新的Rect(currentPart.DrawDepth*间距+x+子部分间距,y,200,20),currentPart.EnglishTitle,myStyle))
{
如果(!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
您正在使用自定义的图形用户界面样式
您可以通过
打开和按下元素时的渲染设置
和
按下控件时的渲染设置
GUIStyle的组件
但是您需要一个纹理2d
,以便在
myStyle.onActive.background = SomeTexture2D;
不幸的是,无法直接更改GUI样式的颜色色调
注意:为了更轻松地配置GUI样式,您只需在ProjectView中创建一个资产&rightarrow;右键单击&rightarrow<代码>创建
&右箭头<代码>GUISkin
通过Inspector在此处执行所有设置,只需在EditorScript中引用此GUISkin
资源,并使用GUI.skin=myReferencedGuiSkin代码>
或者,您可以尝试使用按钮打开标志
您可以使用并检查它是否为EventType.MouseUp
或是否在用于按钮的rect
之外,以便重置标志。(但不确定这是否可行)
private bool按下;
私人布尔徘徊;
...
公营机构
{
var color=GUI.color;
var rect=新的rect(…);
var ev=事件电流;
悬停=ev.mousePosition.x>rect.x&&ev.mousePosition.xrect.y&&ev.mousePosition.y
如果这是一个编辑器窗口,您必须确保设置为true
,以便接收鼠标事件
您的myStyle
在哪里?`private-GUIStyle-myStyle=new-GUIStyle();`<代码>myStyle.normal.textColor=Color.white代码>myStyle.alignment=TextAnchor.middleft代码>myStyle.fontSize=14代码>myStyle.fontStyle=fontStyle.Normal代码>
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();
}