当前位置:网站首页>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
边栏推荐
- Add support for @data add-on in idea
- redis秒杀案例,跟着b站尚硅谷老师学习
- easyui中textbox在光标位置插入内容
- spark学习笔记(五)——sparkcore核心编程-RDD转换算子
- Reading notes of Kazuo Inamori's advice to young people
- How to interact with the server when the client sends an SQL message
- Member array and pointer in banyan loan C language structure
- 微信小程序生成Excel
- app端接口用例设计方法和测试方法
- Explain工具实际操作
猜你喜欢

DTS is equipped with a new self-developed kernel, which breaks through the key technology of the three center architecture of the two places Tencent cloud database

Explain工具实际操作

spark:计算不同分区中相同key的平均值(入门级-简单实现)
![[untitled] JDBC connection database read timeout](/img/24/726ed8b3419866244a1b69e6485d7c.png)
[untitled] JDBC connection database read timeout

Quick sequencing and optimization

Basic concept and essence of Architecture

Explain

Code review pyramid

mysql如何优化

基于OpenCV的轮廓检测(2)
随机推荐
Learn the recycling mechanism of recyclerview again
docker 创建mysql 8.x容器,支持mac ,arm架构芯片
Banyan loan,
spark:地区广告点击量排行统计(小案例)
MySQL的数据库有关操作
[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree
阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
unity游戏,隐私协议最简单解决方案!仅3行代码就搞定!(转载)
Spark Learning Notes (V) -- spark core core programming RDD conversion operator
智能体重秤方案主控采用CSU18M91
Typescript TS basic knowledge interface, generics
If the detailed explanation is generated according to the frame code
架构基本概念和架构本质
How to optimize MySQL
Quick sequencing and optimization
spark:计算不同分区中相同key的平均值(入门级-简单实现)
[从零开始学习FPGA编程-54]:高阶篇 - 基于IP核的FPGA开发-PLL锁相环IP核的原理与配置(Altera)
$128million! IQM, a Finnish quantum computing company, was supported by the world fund
[understanding of opportunity -52]: the depth of communication varies from person to person
Explain工具实际操作