简体   繁体   English

在 Unity 中制作瓷砖网格

[英]Making a tile grid in Unity

I am creating a simple puzzle game in unity and I would like to make a tile map.我正在统一创建一个简单的益智游戏,我想制作一个瓷砖地图。

I have some decent knowledge in programming so I'm completly confused with this not working.我在编程方面有一些不错的知识,所以我完全对这不起作用感到困惑。

The code is as simple as this:代码很简单:

 using System.Collections; using System.Collections.Generic; using UnityEngine; public class GridMaker : MonoBehaviour { public GameObject tilePrefab; public Vector2 gridSize; public void GenerateMap() { for (int x = 0; x < gridSize.x; x++) { Instantiate(tilePrefab, new Vector3(x, 0, 0), Quaternion.identity); for (int y = 1; y < gridSize.y; y++) Instantiate(tilePrefab, new Vector3(0, 0, y), Quaternion.identity); } } }

It could be that I'm tired, but this could should work!可能是我累了,但这应该可行!

Here is a 2D visualisation on how it should look when generated:这是一个关于它在生成时的外观的 2D 可视化:

Ex.前任。 gridSize is set to (3,3) 1's represent the tiles gridSize 设置为 (3,3) 1's 代表瓦片

111 111 111 111 111 111

But here is how it looks as is:但它的外观如下:

1 1 111 1 1 111

Why is that?这是为什么? Considering it starts from the down leftmost one.考虑到它从最左边的一个开始。

PS.附注。 I'm going to sleep now.我要去睡觉了。 Will be online in roughly 12 hours.大约 12 小时后上线。

The answer is quite simple and I feel dumb for making such a mistake.答案很简单,我为犯这样的错误感到愚蠢。 Here is the fix:这是修复:

From

for (int y = 1; y < gridSize.y; y++) Instantiate(tilePrefab, new Vector3(0, 0, y), Quaternion.identity); for (int y = 1; y < gridSize.y; y++) Instantiate(tilePrefab, new Vector3(0, 0, y), Quaternion.identity);

To

for (int y = 1; y < gridSize.y; y++) Instantiate(tilePrefab, new Vector3(x, 0, y), Quaternion.identity); for (int y = 1; y < gridSize.y; y++) Instantiate(tilePrefab, new Vector3(x, 0, y), Quaternion.identity);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM