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

查看:190
本文介绍了如何使用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像素

  • Unity 5.5.0f3 (in 2D Mode)
  • Camera projection ortographic size 10.9
  • Game displayed in 16:9
  • Tileset dimensions are 128x128 px

这是我当前的代码:

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件事需要注意关于它:

There are 3 things to notice about it:


  • 我正在使用ScreenToWorldPoint来获取0,0,0的位置

  • 我的构建顺序从左下角到右上角,每次迭代都作为过去的位置+块的宽度/高度(x和y)

  • 我正在使用16:9对于计划是32:18

使用它,这是我的结果:

Using it, this is my result:

您可以看到它不在摄像机边界之内,即使摄像机和代码均为16:9,也超过了1列。还要注意,实例化点恰好在我的GameObject的中间,因此我开始将实例化为游戏对象宽度和高度的一半,这意味着:

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));

然后重新使用以下内容:

And the reuslt is the following:

我完全没有想到,通过反复试验,我发现它应该是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像素,但是我可以用一个图块填充整个屏幕,也可以将其缩小到最小(考虑世界坐标)。解决方案是缩放瓷砖或更改相机的 orthognalSize

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天全站免登陆