c# - Unity3D C# Button Sprite 交换 - 在运行时附加图像

标签 c# button unity3d sprite

在创建“英雄选择菜单”时,我正在创建按钮。这些按钮将根据它们将代表的“英雄”获取它们的相关图像/ 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/

相关文章:

iphone - 如何通过longPressGestureRecognizer获取button.tag?

c# - 在不在 Unity 中创建新对象的情况下打碎对象

c# - Unity 中的 Quaternion.FromToRotation 出错?

c# - 从/向 native 插件传递 wchar_t 字符串 - 仅适用于 Windows?

c# - 带有 Win32 父窗口的 WPF OpenFileDialog;窗口在关闭时没有重新获得焦点

c# - 类中声明的变量的内存分配

c# - Selenium C# Webdriver FindElements(By.LinkText) 正则表达式?

c# - 如何检测 MDIClient 窗口何时滚动

带按钮的 JavaFX-8 TableView 渲染了太多按钮?

file - 付款后直接创建带有链接的自定义 Paypal 按钮