简体   繁体   中英

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(...);

Again the problem is: which value has to be changed if not.image?

Thanks in advance, Csharpest

You're trying to attach a sprite to a button component. The sprite is in the Image component. 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;

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.

You can do it by changing the sprite attribute of the image component at runtime.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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