按下的sprite"/>
【Unity】动态修改 Button 高亮,按下的sprite
1.首先要切换按钮的 Transition 为 SpriteSwap
2.拖拽实现
3.代码控制
c#:
public Button button;public Sprite normalSprite; //正常显示图片public Sprite highlightedSprite; //高亮显示图片public Sprite pressedSprite; //点击显示图片public Sprite disabledSprite; //禁用显示图片private void OnClickButton(){button.GetComponent<Image>().sprite = normalSprite;//设置按钮模式改为 SpriteSwapbutton.transition = Selectable.Transition.SpriteSwap;//设置变化状态SpriteState state = new SpriteState();state.highlightedSprite = highlightedSprite;state.pressedSprite = pressedSprite;state.disabledSprite = disabledSprite;//赋值button.spriteState = state;}
Lua:
原理是一样的
local curr_btn = self._control.class_btn
local sprite_state = UnityEngine.UI.SpriteState.New()
sprite_state.pressedSprite = ResourceLoader:loadSprite("res url")
--赋值
curr_btn .button.spriteState = sprite_state
更多推荐
【Unity】动态修改 Button 高亮,按下的sprite
发布评论