以下是我更改图像颜色的代码。将cs脚本插入到我的图像属性后,我从颜色选择器中选择了图像颜色为黄色,然后将颜色选择器中的黄色分配给了图像的颜色字段。
public Color normalColor;
public Color selectedColor;
public Image image;
public void Select(){
isSelected = !isSelected;
image.color = isSelected ? selectedColor : normalColor;
if (isSelected) {
WordScramble.main.Select (this);
} else {
WordScramble.main.UnSelect(this);
}
}
单击图像时将调用Select函数,并且该函数可以正确地调用和工作其功能(更改颜色除外)。请告诉我我错了。
答案 0 :(得分:0)
image.GetComponent<Image>().color = isSelected ? selectedColor : normalColor;
一定要获得图像,而不仅仅是游戏对象。祝你好运!
答案 1 :(得分:0)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test07a : MonoBehaviour {
public Color normalColor;
public Color selectedColor;
public Image image;
private bool isSelected;
public void Select(){
isSelected = !isSelected;
image.color = isSelected ? selectedColor : normalColor;
if (isSelected) {
Debug.Log ("selected");
} else {
Debug.Log ("not selected");
}
}
}
尝试了该代码并正常工作(出于实验目的,我删除了WordScramble
并初始化了布尔isSelected
)。
如果您的问题是图像在单击后消失,请尝试从颜色选择器中检查颜色的Alpha值:
that's the problem when I try your code