简体   繁体   English

Unity3D C# 按钮精灵交换 - 在运行时附加图像

[英]Unity3D C# Button sprite swap - attach images at runtime

I am creating buttons when my "Hero Selection Menu" is being created.我正在创建“英雄选择菜单”时创建按钮 These buttons will get their related images/Sprites depending on the "Hero" they will represent.这些按钮将根据它们所代表的“英雄”获得相关的图像/精灵

I have the following method, but I don't understand which variable I have to apply the sprites to .我有以下方法,但我不明白我必须将精灵应用到哪个变量

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
//...
    }

The states are definitely working, I have confirmed it through Debug.Log(...);这些状态肯定有效,我已经通过 Debug.Log(...) 确认了它;

Again the problem is: which value has to be changed if not.image?同样的问题是:如果不是.image,则必须更改哪个值?

Thanks in advance, Csharpest提前致谢, Csharpest

You're trying to attach a sprite to a button component. 您正在尝试将精灵附加到按钮组件。 The sprite is in the Image component. 精灵位于Image组件中。 Check this out! 看一下这个!

GameObject buttonGameObject;
Sprite newSprite;

void Start() {
    buttonGameObject.GetComponent<Image>().sprite = newSprite;   
}

But to fix your code, you'd probably do something like: 但是为了修复你的代码,你可能会做类似的事情:

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;    
    }

If you want to change a button spriteswap sprites in a script you must use spriteState , you can do something like this; 如果你想在脚本中更改按钮spriteswap sprite,你必须使用spriteState ,你可以这样做;

Button _thisButton;
Sprite  _normalSprite;
Sprite _highlightSprite;

void ChangeSprites(){
  // _thisButton.transition = Selectable.Transition.SpriteSwap;
  var ss = _thisButton.spriteState;
  _thisButton.image.sprite = _normalSprite;
  //ss.disabledSprite = _disabledSprite;
  ss.highlightedSprite = _highlightSprite;
  //ss.pressedSprite = _pressedSprie;
  _thisButton.spriteState = ss;
}

Unity makes the swap in the button automatically if you are using a normal button and selecting SpriteSwap, if you need to change the transition option then uncomment the first line of the function. 如果您使用普通按钮并选择SpriteSwap,Unity会自动交换按钮,如果您需要更改转换选项,则取消注释该函数的第一行。

You can do it by changing the sprite attribute of the image component at runtime.您可以通过在运行时更改图像组件的 sprite 属性来实现。

button1.GetComponent<Image>().sprite = sprite;

Full demo code:完整的演示代码:

using UnityEngine;
using UnityEngine.UI;

public class SpriteChangeDemo : MonoBehaviour
{
    public Button button1;
    public Sprite sprite1, sprite2;

    void Start()
    {
        ChangeSprite(sprite1);
        ChangeSprite(sprite2);
    }

    public void ChangeSprite(Sprite sprite) {
        button1.GetComponent<Image>().sprite = sprite;  
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM