当前位置: 首页 > news >正文

Unity中代码替换Button当前、选中的图片

Unity中代码替换Button当前、选中的图片

public GameObject btn; { SpriteState spriteState = new SpriteState(); spriteState.highlightedSprite = sprite["选中图片"]; spriteState.pressedSprite = sprite["选中图片"]; spriteState.selectedSprite = sprite["选中图片"]; spriteState.disabledSprite = sprite["未选中图片"]; btn.GetComponent<Button>().spriteState = spriteState; // 强制刷新至正常状态 EventSystem.current.SetSelectedGameObject(null); // 或:强制刷新为 Normal btn.OnDeselect(null); } 嵌套方式 public class MyButton : Button { public void ForceNormal() { DoStateTransition(SelectionState.Normal, true); } }

如果你在 Button.onClick 回调内部直接执行更换图片逻辑: 触屏输入流程还没走完,状态切换时序冲突!必须延迟一帧执行

// OnClick事件函数 public void OnBtnClick() { StartCoroutine(UpdateSpriteDelay()); } IEnumerator UpdateSpriteDelay() { yield return null; // 等待当前帧输入事件循环结束 // 在这里执行更换sprite、spriteState、清空选中 Image img = btn.GetComponent<Image>(); img.sprite = source; SpriteState state = new SpriteState { highlightedSprite = high, pressedSprite = high }; Button button = btn.GetComponent<Button>(); button.spriteState = state; EventSystem.current.SetSelectedGameObject(null); }

Unity中代码替换下拉栏内容

List<Dropdown.OptionData> odList = new List<Dropdown.OptionData>(); Dropdown.OptionData od = new Dropdown.OptionData(); od.text = "123"; odList.Add(od); transform.GetComponent<Dropdown>().options = odList; transform.GetComponent<Dropdown>().value = 0;
http://www.jsqmd.com/news/1348111/

相关文章:

  • 如何使用No-Consolation:5分钟快速掌握内存中执行EXE/DLL的终极指南
  • 《响应式布局跨端 UI 一致性方案 线上高并发排障实战》
  • 群晖NAS硬盘兼容性终极解决方案:3步解锁任意硬盘支持
  • 商业网站建设案例教程:从0到1打造高转化官网的实战心法与避坑指南
  • 2026年录音转文字的软件哪个好 实测对比:谁才是办公的效率王者
  • qqwry项目揭秘:从数据抓取到自动更新的完整实现
  • 10分钟上手leaflet-omnivore:从安装到加载GPX/KML数据的简单教程
  • 一站式自助建站平台哪个好?2026建站心得分享!
  • kettle-manager部署避坑指南:环境变量配置、启动脚本优化与常见问题解决
  • 终极指南:如何在Unity中实现电影级海洋渲染效果
  • 生产环境最佳实践:onetimepass的密钥管理与安全配置
  • Delon主题系统深度定制:打造独一无二的管理界面风格
  • 为什么选择Delon?探索ng-alain核心模块的10大优势
  • BIThesis表格与矩阵行间距优化:从混乱到专业的排版进阶指南
  • 如何用OneNote高效备考408考研:我的3个月复习经验分享
  • AcWing算法基础课
  • react-typeahead测试策略:确保组件稳定运行的完整方案
  • 采购经理告别手工拼Excel,跨系统数据汇总一句话搞定
  • 10个kubernetes-el实用技巧,让你的K8s集群管理效率提升300%
  • ComfyUI-LTXVideo终极指南:从零到一掌握AI视频生成核心技术
  • 【软件设计师】-进制转换
  • 想要流程资产统一管控,2026 有哪些智能 BPM 系统支持 BPA 流程体系搭建 - 优企甄选
  • 5分钟快速安装:免费macOS风格鼠标指针完整指南
  • leaflet-omnivore完全指南:如何让Leaflet地图支持6种地理数据格式?
  • Intel Texture Works终极指南:在Photoshop中实现专业级游戏纹理压缩
  • Charles抓包
  • 从源码到部署:NemotronLabs-VoiceChat-11B-mlx-8bit完整技术指南
  • Tailscale 没拦住 Hugging Face 入侵:我们该反思什么?
  • 试卷手写文字消除1:数据集说明(含下载链接)
  • NBTExplorer终极指南:3步掌握免费开源的Minecraft数据编辑器