【Unity】绘制表格:排行榜

news/2024/10/8 9:01:35/文章来源:https://www.cnblogs.com/sitarblogs/p/18450964

绘制一款表格,表格特征为头行颜色不同、间隔行不同背景色、滚动视图;表格内容是分数排行榜,前三名有奖牌图标前缀。

成果展示

Scene部分

标题和背景
滚动视图:表头、表格行模板

效果图

脚本部分

脚本绑定在滚动组件的Viewport物体上。

public class HighscoreTable : MonoBehaviour
{private Transform entryContainer;private Transform entryTemplate;private List<HighscoreEntry> highscoreEntryList;private List<Transform> highscoreEntryTransformList;private void Awake(){entryContainer = transform.Find("highscoreEntryContainer");entryTemplate = entryContainer.Find("highscoreEntryTemplate");//模板行隐藏entryTemplate.gameObject.SetActive(false);highscoreEntryTransformList = new List<Transform>();//数据highscoreEntryList = new List<HighscoreEntry>() {new HighscoreEntry{ score = 43421,name="nckla"},new HighscoreEntry{ score = 5634,name="dsc"},new HighscoreEntry{ score = 765,name="cxaccs"},new HighscoreEntry{ score = 3412,name="asasxa"},new HighscoreEntry{ score = 5321,name="cdwqdas"},new HighscoreEntry{ score = 76544,name="gtrgf"},new HighscoreEntry{ score = 863423,name="bbfbtg"},new HighscoreEntry{ score = 323123,name="bgbnhn"},new HighscoreEntry{ score = 643,name="tetrwe"},};Highscores highscores = new Highscores { highscoreEntryList = highscoreEntryList };//数据排序for (int i = 0; i < highscores.highscoreEntryList.Count; i++){for (int j = i + 1; j < highscores.highscoreEntryList.Count; j++){if (highscores.highscoreEntryList[j].score > highscores.highscoreEntryList[i].score){HighscoreEntry tmp = highscores.highscoreEntryList[i];highscores.highscoreEntryList[i] = highscores.highscoreEntryList[j];highscores.highscoreEntryList[j] = tmp;}}}//数据显示到UIforeach (var item in highscores.highscoreEntryList){CreateHighscoreEntryTransform(item, entryContainer, highscoreEntryTransformList);}}[System.Serializable]private class HighscoreEntry{public int score;public string name;}private class Highscores{public List<HighscoreEntry> highscoreEntryList;}private void CreateHighscoreEntryTransform(HighscoreEntry highscoreEntry, Transform container, List<Transform> transformList){//实例化新的行Transform entryTransform = Instantiate(entryTemplate, container);entryTransform.gameObject.SetActive(true);//计算排名并显示:前三名特别int rank = transformList.Count + 1;string rankString;switch (rank){default: rankString = rank + "TH"; break;case 1: rankString = "1ST"; break;case 2: rankString = "2ND"; break;case 3: rankString = "3RD"; break;}entryTransform.Find("posText").GetComponent<TextMeshProUGUI>().SetText(rankString);//显示分数和姓名int score = highscoreEntry.score;entryTransform.Find("scoreText").GetComponent<TextMeshProUGUI>().SetText(score.ToString());string name = highscoreEntry.name;entryTransform.Find("nameText").GetComponent<TextMeshProUGUI>().SetText(name);//间隔行不同背景色entryTransform.Find("background").gameObject.SetActive(rank%2==1);//第一名文本内容颜色不同if (rank == 1) {entryTransform.Find("nameText").GetComponent<TextMeshProUGUI>().color = Color.green;entryTransform.Find("posText").GetComponent<TextMeshProUGUI>().color = Color.green;entryTransform.Find("scoreText").GetComponent<TextMeshProUGUI>().color = Color.green;}//前三名前缀不同颜色的奖牌switch (rank){default:entryTransform.Find("troph").gameObject.SetActive(false);break;case 1: entryTransform.Find("troph").GetComponent<Image>().color = UtilsClass.GetColorFromString("FFD200"); break;case 2: entryTransform.Find("troph").GetComponent<Image>().color = UtilsClass.GetColorFromString("C6C6C6"); break;case 3: entryTransform.Find("troph").GetComponent<Image>().color = UtilsClass.GetColorFromString("876F56"); break;}//所有行整理到表格transformList.Add(entryTransform);}
} 

排行榜内容可以使用PlayerPrefs实现数据持久化

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/809800.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

A股、港股大涨,看多中国的顶级金融机构为何都选择极狐GitLab?

疯狂上涨的 A股、港股 节前一周,上证指数累计上涨超 12%,创下2008年11月以来最大单周涨幅;深证成指累计上涨超17%,创下1996年4月最大单周涨幅;创业板指上涨超22%,创下史上最大单周涨幅。 过去两周,富时A50指数上涨25.5%、金龙指数上涨38.7%、iShare MSCI China ETF 上涨…

thinkphp 设置运行目录为/public后 404错误

当你在 ThinkPHP 中设置了运行目录为 /public 后,可能会遇到 404 错误。这通常是因为 Web 服务器的配置问题导致的。以下是解决这个问题的几个步骤: 1. 修改 .htaccess 文件 如果你使用的是 Apache 服务器,需要确保 .htaccess 文件正确配置。在项目根目录下创建或修改 .htac…

网站证书错误打不开网页怎么办

遇到网站证书错误导致网页无法打开的情况,可以尝试以下几个步骤来解决:清除浏览器缓存和Cookies清除浏览器中的缓存数据和Cookies可能会帮助解决问题。更换浏览器尝试使用不同的浏览器访问该网站,有时候问题可能是浏览器特定的。检查日期和时间设置确保您的设备日期和时间设…

网站怎么修改里面资料

修改网站内部资料的具体步骤取决于您的网站类型和技术栈。以下是针对不同类型的网站的详细步骤: 1. 使用CMS系统(如WordPress、Drupal等) 修改文字登录后台:登录到CMS后台管理系统(例如WordPress的/wp-admin)。编辑页面或文章:导航到“页面”或“文章”部分,找到需要修…

开源的键鼠共享工具「GitHub 热点速览」

十一长假回来,我的手放在落灰的键盘上都有些陌生了,红轴竟敲出了青轴般的响声,仿佛在诉说对假期结束的不甘。 假期回归的首更,让我们看看又有什么好玩的开源项目冲上了开源热榜。一套键盘和鼠标控制多台电脑的工具 deskflow,现在已全面开源、支持多平台,对于开发者更加友…

雅礼国庆集训 day1 T3 画作

题面 题目下载 算法 猜测最优解是 每一次染色都是之前染色的子集且颜色相反(证明不会) 所以可以逆向思维(注意直接逆向不成立) 最后一次染色一定在一个四连通块中, 之前的染色一定是后一次染色的超集 把每个颜色的连通块缩点, 例如每次将一个点(即原图中的连通块)染色成反色, 相…

Windows Server 2025 RTM 中文版、英文版下载 (released Sep 2024)

Windows Server 2025 RTM 中文版、英文版下载 (released Sep 2024)Windows Server 2025 RTM 中文版、英文版下载 (released Sep 2024) Windows Server 2025 LTSC RTM 已发布 请访问原文链接:https://sysin.org/blog/windows-server-2025/ 查看最新版。原创作品,转载请保留出处…

VMware ESXi 8.0U3 集成 AQC 网卡定制版更新 OEM BIOS 2.7 支持 Windows Server 2025

VMware ESXi 8.0U3 集成 AQC 网卡定制版更新 OEM BIOS 2.7 支持 Windows Server 2025VMware ESXi 8.0U3 集成 AQC 网卡定制版更新 OEM BIOS 2.7 支持 Windows Server 2025 VMware ESXi 8.0U3 macOS Unlocker & OEM BIOS 集成网卡驱动和 NVMe 驱动 (集成驱动版) 发布 ESXi 8…

VMware Aria Automation 8.18 发布,新增功能概览

VMware Aria Automation 8.18 发布,新增功能概览VMware Aria Automation 8.18 - 多云基础架构自动化平台 Multi-Cloud Infrastructure Automation Platform 请访问原文链接:https://sysin.org/blog/vmware-aria-automation/,查看最新版。原创作品,转载请保留出处。 作者主页…

VMware Aria Automation Orchestrator 8.18 发布,新增功能概览

VMware Aria Automation Orchestrator 8.18 发布,新增功能概览VMware Aria Automation Orchestrator 8.18 - 现代工作流程自动化平台 请访问原文链接:https://sysin.org/blog/vmware-aria-automation-orchestrator/,查看最新版。原创作品,转载请保留出处。 作者主页:sysin…

C#/.NET/.NET Core技术前沿周刊 | 第 8 期(2024年10.01-10.06)

前言 C#/.NET/.NET Core技术前沿周刊,你的每周技术指南针!记录、追踪C#/.NET/.NET Core领域、生态的每周最新、最实用、最有价值的技术文章、社区动态、优质项目和学习资源等。让你时刻站在技术前沿,助力技术成长与视野拓宽。欢迎投稿,推荐或自荐优质文章/项目/学习资源等。…

从SQL Server过渡到PostgreSQL:理解模式的差异

从SQL Server过渡到PostgreSQL:理解模式的差异 前言 随着越来越多的企业转向开源技术,商业数据库管理员和开发者也逐渐面临向PostgreSQL迁移的需求。 虽然SQL Server和PostgreSQL共享许多数据库管理系统(RDBMS)的基本概念,但它们在处理某些结构上的差异可能会让人感到困惑…