当前位置:网站首页>unity之二维数组实现正六边形地图
unity之二维数组实现正六边形地图
2022-07-27 02:08:00 【爱上游戏开发】
最近项目有个需求,需要实现的效果如下:
左右上下相邻正六边形的位置坐标如下:
分析:
一行中,两个相邻六边形相差80,假设第一个x坐标为0,则下一个相邻六边形的x值为:
(x-1)*80
上下相邻两个六边形的X值相差-40,两两一循环,因此,可以用y值来确定X的偏移量:
local offX = y%2 == 0 and -40 or 0
最终x的坐标为:
local offX = y%2 == 0 and -40 or 0
(x-1)*80+offX
X的坐标确定啦,Y坐标就更简单了,上下两个相邻的y差值为70,假设第一个y坐标为0,则,对应的其他y坐标为:
(y-1)*70
最终,每个正六边形的坐标也就确定了,为:
local offX = y%2 == 0 and -40 or 0
go.transform.localPosition = Vector3.New((x-1)*80+offX,(y-1)*70,0)
完整代码如下:
ui.list = base:findobj("list")
ui.item = base:findobj("item")
local function RefreshUI()
local length = 8--长
local width = 4--宽
local itemList = {}
for y=1,width do
itemList[y] = {}
for x=1,length do
local obj = utils.addchild(ui.list,ui.item)
utils.findtext(obj,"Text").text = string.format("%s_%s",x,y)
itemList[y][x] = obj
local offX = y%2 == 0 and -40 or 0
itemList[y][x].transform.localPosition = Vector3.New((x-1)*80+offX,(y-1)*70,0)
SetActive(obj,true)
end
end
end
边栏推荐
- MySQL underlying data structure
- Code review pyramid
- mysql出现不存在错误
- Customer cases | pay attention to the elderly user experience, and the transformation of bank app to adapt to aging should avoid falsehood and be practical
- 复盘:图像有哪些基本属性?关于图像的知识你知道哪些?图像的参数有哪些
- spark学习笔记(六)——sparkcore核心编程-RDD行动算子
- How to conduct 360 assessment
- 768. 最多能完成排序的块 II 贪心
- How to uniquely identify a user SQL in Youxuan database cluster
- OC message mechanism
猜你喜欢

Spark Learning Notes (IV) -- spark core programming RDD

Kettle读取按行分割的文件

数据库概论 - 数据库的介绍

How many implementation postures of delay queue? Daily essential skills!

快速排序及优化

The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode

Idea 中添加支持@Data 插件

spark学习笔记(四)——sparkcore核心编程-RDD

【树链剖分】模板题

spark:地区广告点击量排行统计(小案例)
随机推荐
[1206. Design skip table]
The diagram of user login verification process is well written!
768. 最多能完成排序的块 II 贪心
Redis源码学习(33),命令执行过程
Message rejected MQ
LPCI-252通用型PCI接口CAN卡的功能和应用介绍
Duplicate disc: what are the basic attributes of an image? What do you know about images? What are the parameters of the image
typescript ts 基础知识之接口、泛型
spark:计算不同分区中相同key的平均值(入门级-简单实现)
How to uniquely identify a user SQL in Youxuan database cluster
Redis source code learning (33), command execution process
Csu18m91 is used as the master controller of the intelligent scale scheme
MySQL has a nonexistent error
MySQL underlying data structure
Contour detection based on OpenCV (2)
mysql出现不存在错误
Does Oracle have a distributed database?
$128million! IQM, a Finnish quantum computing company, was supported by the world fund
unity游戏,隐私协议最简单解决方案!仅3行代码就搞定!(转载)
mysql底层数据结构