通过脚本更改UI按钮的颜色 [英] Change colors of UI button via script
问题描述
我正在尝试使用此行代码来更改UI按钮上的颜色.
I'm trying to change the colours on a UI Button using this line of code.
prev.GetComponent<Button>().colors.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);
但我收到此错误
资产/_Scripts/OptionSwitch.cs(28,53):错误CS1612:无法修改"UnityEngine.UI.Selectable.colors"的值类型返回值.考虑将值存储在临时变量中
Assets/_Scripts/OptionSwitch.cs(28,53): error CS1612: Cannot modify a value type return value of `UnityEngine.UI.Selectable.colors'. Consider storing the value in a temporary variable
在调用它们之前,我曾尝试将按钮和颜色都存储为变量,但它不会更改错误代码.
I've tried storing both the button and the color as variables before calling them but it doesn't change the error code.
编辑:
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Sprites;
public class OptionSwitch : MonoBehaviour {
ColorBlock colorBlock = new ColorBlock();
colorBlock.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);
[MenuItem ("GameObject/UI/Switch")]
static void Switch(){
if (GameObject.FindObjectOfType (typeof(Canvas)) != null) {
Canvas canvas = (Canvas)GameObject.FindObjectOfType (typeof(Canvas));
// Define Previous Button
GameObject prev = new GameObject ("Previous", typeof(Button));
prev.layer = 5;
prev.AddComponent<Image> ();
prev.transform.parent = canvas.transform;
prev.GetComponent<Image> ().sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd");
prev.GetComponent<Button>().colors = buttonColors;
// Define Previous Button Image
GameObject previm = new GameObject("Previous Image", typeof(RawImage));
previm.layer = 5;
previm.transform.parent = prev.transform;
} else {
// Create Canvas
GameObject canvas = new GameObject("Canvas", typeof(Canvas));
canvas.AddComponent<CanvasScaler> ();
canvas.AddComponent<GraphicRaycaster> ();
canvas.layer = 5;
canvas.GetComponent<Canvas> ().renderMode = RenderMode.ScreenSpaceOverlay;
canvas.transform.localPosition = Vector3.zero;
// Create Event System
GameObject eventsystem = new GameObject("EventSystem", typeof(EventSystem));
eventsystem.AddComponent<StandaloneInputModule>();
eventsystem.AddComponent<TouchInputModule>();
}
}
}
推荐答案
您必须更改colors
而不是normalColor
. GetComponent<Button>().colors
返回ColorBlock
.
You have to change the colors
not the normalColor
. The GetComponent<Button>().colors
returns ColorBlock
.
因此,创建ColorBlock
的新实例.从ColorBlock
修改normalColor
,然后将ColorBlock
分配给GetComponent<Button>().colors
.
So, create a new instance of ColorBlock
. Modify normalColor
from that ColorBlock
then assign that ColorBlock
to the GetComponent<Button>().colors
.
完整示例:
ColorBlock colorBlock = new ColorBlock();
colorBlock.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);
prev.GetComponent<Button>().colors = colorBlock;
这将覆盖您的其他颜色设置.要保存它们,请从prev.GetComponent<Button>().colors;
This will overwrite your other color settings. To preserver them, create your ColorBlock
from prev.GetComponent<Button>().colors;
ColorBlock colorBlock = prev.GetComponent<Button>().colors;
colorBlock.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);
prev.GetComponent<Button>().colors = colorBlock;
您还可以在下面修改颜色属性:
You can also modify the color properties below:
colorBlock.pressedColor = new Color(1f, 0.0f, 0.0f, 1.0f);
colorBlock.highlightedColor = new Color(0f, 1f, 0.0f, 1.0f);
colorBlock.disabledColor = new Color(0f, 0f, 1, 1.0f);
这篇关于通过脚本更改UI按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!