Setting Sprites on Buttons from an Array

I wanted to show one way of instantiating the buttons with sprites instead of just text.

In the UnitActionSystemUI.cs, create a public array of sprites using public Sprite actionSprites or whatever name you want. Then in ActionButton.cs add the following

private UnitActionSystemUI unitActionSystemUI;

    private void Awake()
    {
        actionImage = transform.GetComponent<Image>();
        unitActionSystemUI = GameObject.Find("UnitActionSystemUI").GetComponent<UnitActionSystemUI>();
    }

Then in the SetBaseAction(BaseAction baseAction) function add a switch for each case of what the action name can be. For example:

switch (baseAction.GetActionName())
            {
                case "Sword":
                    actionImage.sprite = unitActionSystemUI.actionSprites[0];
                    return;
                case "Interact":
                    actionImage.sprite = unitActionSystemUI.actionSprites[1];
                    return;
                case "Move":
                    actionImage.sprite = unitActionSystemUI.actionSprites[2];
                    return;
                case "Shoot":
                    actionImage.sprite = unitActionSystemUI.actionSprites[3];
                    return;
                case "Spin":
                    actionImage.sprite = unitActionSystemUI.actionSprites[4];
                    return;
            }

I also altered the textMeshPro.text so it has no text. Also, the prefab for the button may be too dark because the color is gray so I would recommend changing it to white.

1 Like

Privacy & Terms