Unity实现棋盘方格

2024-08-26 23:36
文章标签 实现 unity 棋盘 方格

本文主要是介绍Unity实现棋盘方格,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文参考:p1_哔哩哔哩_bilibili 

一、精要提炼

1、Button自带的白色底图是圆角的,Image组件自带的白色底图是方角的。

2、2D中Instantiate指定的位置为屏幕坐标系的位置,左下角为(0,0)

3、求某个组件的位置:xx.transform.position,xx为GameObject对象

4、求某个组件的width:xx.getComponent<RectTransform>().rect.width

5、设置某个组件的大小:xx.getComponent<RectTransform>().sizeDelta = new Vector2(yy, zz);

二、通过预设体创建第一个方格

效果如图:

1、设置Game屏幕分辨率为2000*3200,大致相当于一般收集的分辨率

2、创建Canvas, 命名为BoardCanvas,其参数配置如下:

3、在Canvas下创建一个Panel,命名为BoardPanel,其大小自动为Scratch填充整个Canvas

4、在Panel下创建Image,命名为Background,其参数为:

大小:1700*1700

5、在Panel下再创建一个Button作为棋盘的方格,命名为BlockButton,其下有Text和Image组件。

Button的大小:200*200

Button自带白色底图是圆角的,我们希望Button的白色底图是方角的,需要需要再添加如上所示的Image组件,其自带的白色底图是方格的。

6、在Assets下创建Prefab目录,将BlockButton放置其中。我们希望通过代码生成一个个的方格。

7、创建一个Empty物体命名为GameManager

8、创建一个C#脚本命名为BoardGameManager,并挂载到GameManager下:

public class BoardGameManager : MonoBehaviour
{[SerializeField]private GameObject blockButton;[SerializeField]private GameObject panel;private void Awake(){Instantiate(blockButton, new Vector3(0,0,0), Quaternion.identity, panel.transform);}
}

然后配置两个GameObject的信息如下:

执行代码后效果如下:

方格出现在Canvas的左下角,而不是我们想要的Panel的中间位置。

这是因为Instantiate中执行的(0,0,0)是屏幕坐标系的位置,位于屏幕的左下角,如果需要转化为Canvas中的中间位置,则需要加上canvas的坐标位置。代码修改如下:

public class BoardGameManager : MonoBehaviour
{[SerializeField]private GameObject blockButton;[SerializeField]private GameObject panel;[SerializeField]private GameObject boardCanvas;private void Awake(){GameObject button = Instantiate(blockButton, new Vector3(0,0,0), Quaternion.identity, panel.transform);button.transform.position += boardCanvas.transform.position;}
}

三、创建任意个数的方格

已知Panel中心点的局部位置为(0,0),即最外层正中心的位置。

方格分别标记为(m,n),此为序号信息,非坐标值信息。第一个为(0,0),第二个为(0,1),依次进行标记。如下图所示:

已知最外层正方形的边长为len1,空隙的长度为len2,方格的个数为count。

根据正中心的坐标值,可以求得最左上角的坐标值,进而求得每个方格中心点的坐标值。首先count为单数/双数分别推导公式,然后就会发现count单数/双数共用同一个公式。此处省略计算过程,只在代码中给出最终的结果公式。

BoardGameManager.cs完整的代码如下:

public class BoardGameManager : MonoBehaviour
{[SerializeField] private GameObject blockButton;[SerializeField] private GameObject background;[SerializeField] private GameObject panel;[SerializeField] private GameObject boardCanvas;public int count;private void Awake(){float totalLen = background.GetComponent<RectTransform>().rect.width;float gapLen = 10;if(count == 0){count = 8;}renderBoard(totalLen, gapLen, count);}/*** totalLen: background总长度* gapLen: 方格间空隙长度* count: 方格个数*/private void renderBoard(float totalLen, float gapLen, int count){float buttonLen = 1f * (totalLen - count * gapLen - gapLen) / count;for (int i = 0; i < count; i++){for (int j = 0; j < count; j++) {float poxX = 1f / 2 * (2 * i + 1 - count) * (totalLen - gapLen) / count;float poxY = 1f / 2 * (2 * j + 1 - count) * (totalLen - gapLen) / count;    Debug.Log(poxX + " " + poxY);GameObject button = Instantiate(blockButton, new Vector3(poxX, poxY, 0), Quaternion.identity, panel.transform);button.transform.position += boardCanvas.transform.position;button.GetComponent<RectTransform>().sizeDelta = new Vector2 (buttonLen, buttonLen);}}}
}

四、点击方格显示信息

首先,创建BoardButton的C#脚本,将button的属性信息存放在该类中。

public class BoardButton : MonoBehaviour
{private int posX;private int posY;public void setPos(int posX, int posY) { this.posX = posX;       this.posY = posY;}public void onClickButton(){Debug.Log(this.posX + ", " + this.posY);    }
}

然后,修改BoardGameManager.cs代码,增加记录pos的信息:

public class BoardGameManager : MonoBehaviour
{[SerializeField] private GameObject blockButton;[SerializeField] private GameObject background;[SerializeField] private GameObject panel;[SerializeField] private GameObject boardCanvas;public int count;private void Awake(){float totalLen = background.GetComponent<RectTransform>().rect.width;float gapLen = 10;if(count == 0){count = 8;}renderBoard(totalLen, gapLen, count);}/*** totalLen: background总长度* gapLen: 方格间空隙长度* count: 方格个数*/private void renderBoard(float totalLen, float gapLen, int count){float buttonLen = 1f * (totalLen - count * gapLen - gapLen) / count;for (int i = 0; i < count; i++){for (int j = 0; j < count; j++) {float poxX = 1f / 2 * (2 * i + 1 - count) * (totalLen - gapLen) / count;float poxY = 1f / 2 * (2 * j + 1 - count) * (totalLen - gapLen) / count;    Debug.Log(poxX + " " + poxY);GameObject button = Instantiate(blockButton, new Vector3(poxX, poxY, 0), Quaternion.identity, panel.transform);button.transform.position += boardCanvas.transform.position;button.GetComponent<RectTransform>().sizeDelta = new Vector2 (buttonLen, buttonLen);button.GetComponent<BoardButton>().setPos(i, j);}}}
}

最后,在BoardButton组件中配置OnClick事件函数:

此时运行程序,点击任何一个方格都会打印位置信息。

这篇关于Unity实现棋盘方格的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110005

相关文章

AJAX请求上传下载进度监控实现方式

《AJAX请求上传下载进度监控实现方式》在日常Web开发中,AJAX(AsynchronousJavaScriptandXML)被广泛用于异步请求数据,而无需刷新整个页面,:本文主要介绍AJAX请... 目录1. 前言2. 基于XMLHttpRequest的进度监控2.1 基础版文件上传监控2.2 增强版多

Redis分片集群的实现

《Redis分片集群的实现》Redis分片集群是一种将Redis数据库分散到多个节点上的方式,以提供更高的性能和可伸缩性,本文主要介绍了Redis分片集群的实现,具有一定的参考价值,感兴趣的可以了解一... 目录1. Redis Cluster的核心概念哈希槽(Hash Slots)主从复制与故障转移2.

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.

Mybatis 传参与排序模糊查询功能实现

《Mybatis传参与排序模糊查询功能实现》:本文主要介绍Mybatis传参与排序模糊查询功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、#{ }和${ }传参的区别二、排序三、like查询四、数据库连接池五、mysql 开发企业规范一、#{ }和${ }传参的

Docker镜像修改hosts及dockerfile修改hosts文件的实现方式

《Docker镜像修改hosts及dockerfile修改hosts文件的实现方式》:本文主要介绍Docker镜像修改hosts及dockerfile修改hosts文件的实现方式,具有很好的参考价... 目录docker镜像修改hosts及dockerfile修改hosts文件准备 dockerfile 文

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整