一个小知识点,怕忘记,所以记录下。废话不多说,直接上代码:

未赋值之前:

Unity使用代码动态给按钮赋值各个状态下的图片

 

使用下面代码赋值:

 1 using UnityEngine;
 2 using UnityEngine.UI;
 3 
 4 public class Test : MonoBehaviour
 5 {
 6     public Button button;
 7     public Sprite normalSprite;         //正常图片
 8     public Sprite highlightedSprite;    //高亮图片
 9     public Sprite pressedSprite;        //点击图片
10     public Sprite disabledSprite;       //禁用图片
11     private void Start()
12     {
13         button.GetComponent<Image>().sprite = normalSprite;
14         //将按钮变化模式改为 SpriteSwap
15         button.transition = Selectable.Transition.SpriteSwap;
16         //设置变化状态
17         SpriteState state = new SpriteState();
18         state.highlightedSprite = highlightedSprite;
19         state.pressedSprite = pressedSprite;
20         state.disabledSprite = disabledSprite;
21         button.spriteState = state;
22     }
23 }

 

赋值后:

Unity使用代码动态给按钮赋值各个状态下的图片

 

就是这么简单,相信大家应该学会了吧!

 

相关文章:

  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-20
  • 2022-12-23
  • 2022-12-23
  • 2021-08-12
  • 2022-12-23
相关资源
相似解决方案