在创建“英雄选择菜单”时,我正在创建按钮。这些按钮将根据它们将代表的“英雄”获取它们的相关图像/ Sprite 。
我有以下方法,但我不明白我必须将 Sprite 应用于哪个变量。
Button _thisButton;
Sprite _normalSprite;
Sprite _highlightSprite;
protected override void DoStateTransition (SelectionState state, bool instant){
switch (state) {
case Selectable.SelectionState.Normal:
_thisButton.image = _normalSprite; //.image is not correct
Debug.Log("statenormalasd");
break;
case Selectable.SelectionState.Highlighted:
_thisButton.image = _normalSprite; //.image is not correct
//...
}
这些状态确实有效,我已经通过 Debug.Log(...) 确认了它;
同样的问题是:如果不是 .image,必须更改哪个值?
提前致谢, 最锐利的
最佳答案
您正在尝试将 Sprite 附加到按钮组件。 Sprite 位于图像组件中。看看这个!
GameObject buttonGameObject;
Sprite newSprite;
void Start() {
buttonGameObject.GetComponent<Image>().sprite = newSprite;
}
但要修复您的代码,您可能会做类似的事情:
Button _thisButton;
Sprite _normalSprite;
Sprite _highlightSprite;
protected override void DoStateTransition (SelectionState state, bool instant){
switch (state) {
case Selectable.SelectionState.Normal:
_thisButton.GetComponent<Image>().sprite = _normalSprite;
Debug.Log("statenormalasd");
break;
case Selectable.SelectionState.Highlighted:
_thisButton.GetComponent<Image>().sprite = _normalSprite;
}
关于c# - Unity3D C# Button Sprite 交换 - 在运行时附加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42249720/