如何修改Unity中GUI.Button点击时的高亮颜色(OnGUI()实现)
Alright, let's tackle how to get your GUI.Button to show a custom highlight color when it's clicked. Looking at your existing code, you're already tracking the button's state with currentPart.IsClicked—perfect, we can build on that to adjust the button's appearance dynamically.
Option 1: Predefine Styles for Normal/Highlighted States
The cleanest approach is to create two distinct GUIStyle instances upfront: one for the button's default state, and another for when it's clicked/highlighted. This avoids recreating styles every frame (which can hurt performance).
First, set up your styles in Awake() or Start():
private GUIStyle normalButtonStyle; private GUIStyle highlightedButtonStyle; void Awake() { // Base styles on Unity's default button skin to keep consistent sizing/fonts normalButtonStyle = new GUIStyle(GUI.skin.button); highlightedButtonStyle = new GUIStyle(GUI.skin.button); // Configure normal state colors normalButtonStyle.normal.textColor = Color.black; normalButtonStyle.normal.background = CreateSolidTexture(Color.gray); // Configure highlighted (clicked) state colors highlightedButtonStyle.normal.textColor = Color.white; highlightedButtonStyle.normal.background = CreateSolidTexture(Color.darkBlue); } // Helper method to create a simple solid-color texture for button backgrounds private Texture2D CreateSolidTexture(Color color) { Texture2D texture = new Texture2D(2, 2); Color[] pixels = new Color[texture.width * texture.height]; for (int i = 0; i < pixels.Length; i++) { pixels[i] = color; } texture.SetPixels(pixels); texture.Apply(); return texture; }
Then, in your OnGUI() method, switch between the styles based on currentPart.IsClicked:
void OnGUI() { // Pick the right style based on the button's clicked state GUIStyle activeStyle = currentPart.IsClicked ? highlightedButtonStyle : normalButtonStyle; if (GUI.Button(new Rect(currentPart.DrawDepth * spacing + x + subPartsSpacing, y, 200, 20), currentPart.EnglishTitle, activeStyle)) { // Your existing click logic here... 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]); } } } } }
Option 2: Dynamically Adjust a Single Style
If you prefer to use a single style and modify its properties on the fly (good for quick tweaks), you can adjust the style's normal properties directly in OnGUI() based on the clicked state:
private GUIStyle myStyle; void Awake() { myStyle = new GUIStyle(GUI.skin.button); // Set default colors here myStyle.normal.textColor = Color.black; myStyle.normal.background = CreateSolidTexture(Color.gray); } void OnGUI() { // Update the style's appearance based on the button's state if (currentPart.IsClicked) { myStyle.normal.textColor = Color.white; myStyle.normal.background = CreateSolidTexture(Color.darkBlue); } else { myStyle.normal.textColor = Color.black; myStyle.normal.background = CreateSolidTexture(Color.gray); } if (GUI.Button(new Rect(currentPart.DrawDepth * spacing + x + subPartsSpacing, y, 200, 20), currentPart.EnglishTitle, myStyle)) { // Your existing click logic here... } }
Key Notes
- The
CreateSolidTexturemethod is necessary becauseGUIStyle.backgroundrequires a texture—we're just making a tiny 2x2 solid color texture to use as the button background. - If you want a highlight only while the button is being pressed (not held after release), you can use
myStyle.activeinstead of modifying thenormalstate. Theactivestate applies when the button is being clicked down. - For toggle-like behavior (keep highlighted until clicked again), using the
IsClickedflag to switch styles is exactly what you need, as shown above.
内容的提问来源于stack exchange,提问作者Cem Tirpanli

