通过脚本更改 UI 按钮的颜色 [英] Change colors of UI button via script
问题描述
我正在尝试使用这行代码更改 UI 按钮上的颜色.
prev.GetComponent
但我收到此错误
<块引用>Assets/_Scripts/OptionSwitch.cs(28,53):错误 CS1612:无法修改UnityEngine.UI.Selectable.colors"的值类型返回值.考虑将值存储在临时变量中
在调用按钮和颜色之前,我尝试将它们存储为变量,但它不会更改错误代码.
编辑:
使用UnityEditor;使用 UnityEngine;使用 UnityEngine.UI;使用 UnityEngine.EventSystems;使用 UnityEngine.Sprites;公共类 OptionSwitch : MonoBehaviour {ColorBlock colorBlock = new ColorBlock();colorBlock.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);[MenuItem ("GameObject/UI/Switch")]静态无效开关(){if (GameObject.FindObjectOfType (typeof(Canvas)) != null) {Canvas canvas = (Canvas)GameObject.FindObjectOfType (typeof(Canvas));//定义上一个按钮GameObject prev = new GameObject("Previous", typeof(Button));上一层 = 5;prev.AddComponent();prev.transform.parent = canvas.transform;prev.GetComponent().sprite = AssetDatabase.GetBuiltinExtraResource("UI/Skin/UISprite.psd");prev.GetComponent
您必须更改 colors
而不是 normalColor
.GetComponent
返回 ColorBlock
.
因此,创建一个 ColorBlock
的新实例.从该 ColorBlock
修改 normalColor
,然后将该 ColorBlock
分配给 GetComponent
.>
完整示例:
ColorBlock colorBlock = new ColorBlock();colorBlock.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);prev.GetComponent
这将覆盖您的其他颜色设置.为了保留它们,从 prev.GetComponent
ColorBlock
ColorBlock colorBlock = prev.GetComponent
您还可以修改以下颜色属性:
colorBlock.pressedColor = new Color(1f, 0.0f, 0.0f, 1.0f);colorBlock.highlightedColor = new Color(0f, 1f, 0.0f, 1.0f);colorBlock.disabledColor = 新颜色(0f, 0f, 1, 1.0f);
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);
but I'm receiving this error
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.
EDIT:
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>();
}
}
}
You have to change the colors
not the normalColor
. The GetComponent<Button>().colors
returns ColorBlock
.
So, create a new instance of ColorBlock
. Modify normalColor
from that ColorBlock
then assign that ColorBlock
to the GetComponent<Button>().colors
.
Full example:
ColorBlock colorBlock = new ColorBlock();
colorBlock.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);
prev.GetComponent<Button>().colors = colorBlock;
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屋!