如何使用 Unity 将基于 2D 数组的图块实例化为平台游戏? [英] How to instantiate tiles based on a 2D Array into a Platform Game using Unity?

查看:19
本文介绍了如何使用 Unity 将基于 2D 数组的图块实例化为平台游戏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个非常简单的平台游戏,使用 2D 数组来构建基于它的地图.

I'm building a very simple platform game using 2D array to build the map based on it.

我想要两个简单的目标,但我目前没有找到答案:

There are two simple goals I want and I'm currently not finding the answer:

  1. 确保相机为 16:9 并且我的场景将 100% 显示在其中
  2. 像在数组中一样构建 2D 平台图块集

我的环境:

  • Unity 5.5.0f3(2D 模式)
  • 相机投影正交尺寸 10.9
  • 游戏以 16:9 显示
  • 图块集尺寸为 128x128 像素

这是我当前的代码:

public Camera camera;
public GameObject prefab;

void Start () {
    Vector3 pos = camera.ScreenToWorldPoint(new Vector3 (0, 0, 0));
    Vector3 nextPosition = pos;
    for (int i = 0; i < 32; i++)
    {
        for(int j=0; j < 18; j++)
        {
            GameObject a = Instantiate(prefab, new Vector3(nextPosition.x, nextPosition.y, 0), Quaternion.identity);
            nextPosition = new Vector3(pos.x+(prefab.GetComponent<Renderer>().bounds.size.x)*i, pos.y+(prefab.GetComponent<Renderer>().bounds.size.y)*j,0);
        }
    }
}

有3点需要注意:

  • 我正在使用 ScreenToWorldPoint 来获取 0,0,0 的位置
  • 我的构建顺序是从左下到右上,每次迭代都被放置为过去的位置 + 块的宽度/高度(x 和 y)
  • 我使用 16:9 的方案,即 32:18

使用它,这是我的结果:

Using it, this is my result:

如您所见,它位于相机边界之外,即使相机和代码均为 16:9,它也超过了 1 列.还要注意,实例化点正好在我的游戏对象的中间,所以我开始将它实例化为游戏对象宽度和高度的一半,意思是:

As you can see it stays out of the camera boundary and even tho both camera and code are 16:9, it exceeds 1 column. Also note that the instantiate point is exactly in the middle of my GameObject, so I start instantiating it as half of the gameObject's width and height, meaning:

Vector3 pos = camera.ScreenToWorldPoint(new Vector3 (64, 64, 0));

结果如下:

完全不是我所期望的,通过反复试验,我发现它应该在 16,16:

Not what I expected at all, by trial and error I figured out it is suposed to be at 16,16:

Vector3 pos = camera.ScreenToWorldPoint(new Vector3 (16, 16, 0));

现在它非常适合,但它超过了顶部的 1 行和右侧的 1.5 列.这不应该,因为它们都是 16:9

Now its a perfect fit, but it exceeds 1 line at the top and 1,5 columns at the right. Which shouldn't because they are both 16:9

我显然做错了什么,但我看不出是什么,我过去曾遇到过这个问题,但我不记得我想出了什么.

I'm clearly doing something wrong but I can't see what, I've been through this problem in the past but I don't remember what I figured out.

推荐答案

"Pos" 需要在开始时换班.可以使用 Bounds.extents

"Pos" needs a shift at the start. It can be achieved using Bounds.extents

    Vector3 pos = camera.ScreenToWorldPoint(new Vector3 (0, 0, 0));

    pos = new Vector3( pos.x + prefab.GetComponent<Renderer>().bounds.extents.x,
                      pos.y + prefab.GetComponent<Renderer>().bounds.extents.y,
                      0);


    //....the rest is the same as your code

这比使用幻数 (16,16,0) 更好.无论您使用何种比例,第一个图块都将位于左下角.

This will be better than using the magic numbers (16,16,0). The first tile will be positioned at the left-bottom corner no matter what scale you use.

128x128 px 只会告诉我您使用的是方形磁贴.所以它是 128x128 像素 但我可以用一个瓷砖填满整个屏幕,或者我可以让它尽可能小(考虑世界坐标).解决方案是缩放图块或更改相机的正交尺寸.

128x128 px only tells me that you're using a square tile. So it's 128x128 px but I can fill the whole screen with one tile or I can make it as tiny as I can (thinking in world coordinate). The solution is either to scale the tiles or change the orthognalSize of the camera.

简单的解决方案是更改 orthographicSize 以适应图块.

The easy solution is to change the orthographicSize to fit the tiles.

camera.orthographicSize = 18 * prefab.GetComponent<Renderer>().bounds.size.y * 0.5f;

orthographicSize 等于世界坐标高度的一半,您需要 18 个瓷砖的高度.

orthographicSize equals half the height in world coordinate and you need 18 tiles in height.

所以所有的代码结合起来:

So all code combined :

        Bounds bound = prefab.GetComponent<Renderer>().bounds;

        camera.orthographicSize = 18 * bound.size.y * 0.5f;

        Vector3 pos = camera.ScreenToWorldPoint(Vector3.zero);
        pos = new Vector3( pos.x + bound.extents.x, pos.y + bound.extents.y, 0);

        //....The rest is the same as your code

这篇关于如何使用 Unity 将基于 2D 数组的图块实例化为平台游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆