Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Unity将基于2D数组的瓷砖实例化到平台游戏中?_C#_Unity3d_2d - Fatal编程技术网

C# 如何使用Unity将基于2D数组的瓷砖实例化到平台游戏中?

C# 如何使用Unity将基于2D数组的瓷砖实例化到平台游戏中?,c#,unity3d,2d,C#,Unity3d,2d,我正在构建一个非常简单的平台游戏,使用2D数组来构建基于它的地图 我想要两个简单的目标,但我目前还没有找到答案: 确保相机是16:9,并且我的场景将100%显示在其中 在阵列中构建二维平台平铺集 我的环境: Unity 5.5.0f3(在2D模式下) 摄像机投影或摄影尺寸10.9 比赛以16:9显示 瓷砖集尺寸为128x128像素 这是我目前的代码: public Camera camera; public GameObject prefab; void Start () { Ve

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

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

  • 确保相机是16:9,并且我的场景将100%显示在其中
  • 在阵列中构建二维平台平铺集
  • 我的环境:

    • 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);
            }
        }
    }
    
    其结果如下:

    一点也不像我预期的那样,经过反复试验,我发现应该是16,16:

    Vector3 pos = camera.ScreenToWorldPoint(new Vector3 (16, 16, 0));
    
    现在它是一个完美的适合,但它超过了1行在顶部和1,5列在右侧。这不应该,因为他们都是16:9

    我显然做错了什么,但我看不出是什么,我过去经历过这个问题,但我不记得我发现了什么。

    Pos”一开始就需要换班。它可以通过使用

    Vector3 pos=camera.ScreenToWorldPoint(新的Vector3(0,0,0));
    pos=新矢量3(pos.x+prefact.GetComponent().bounds.extents.x,
    pos.y+prefact.GetComponent().bounds.extents.y,
    0);
    //..其余部分与您的代码相同
    
    这将比使用幻数(16,16,0)更好。无论使用何种比例,第一块瓷砖都将位于左下角

    128x128像素只告诉我您使用的是方形瓷砖。所以它是128x128像素,但我可以用一块瓷砖填充整个屏幕,或者我可以使它尽可能小(以世界坐标思考)。解决方案是缩放瓷砖或更改相机的正交大小

    简单的解决方案是更改正交尺寸以适合瓷砖

    camera.orthographicSize = 18 * prefab.GetComponent<Renderer>().bounds.size.y * 0.5f;
    
    camera.orthographicSize=18*prefact.GetComponent().bounds.size.y*0.5f;
    
    正交大小等于世界坐标高度的一半,需要18个瓷砖高度

    因此,所有代码组合在一起:

            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
    
    Bounds-bound=prefact.GetComponent().Bounds;
    camera.Orthographic size=18*bound.size.y*0.5f;
    Vector3位置=摄像机屏幕到世界点(Vector3.zero);
    pos=新矢量3(pos.x+绑定的.extensts.x,pos.y+绑定的.extensts.y,0);
    //..其余部分与您的代码相同
    
    您的inspector摄像机视图与游戏摄像机视图是否正交。inspector布局看起来像inspector中的摄影机视图仍然是3D(透视)。这是我的摄影机inspector:不,我是指。单击gizmo中心的正方形以在“检查器”中的正交视图和透视视图之间切换。这解决了你的问题吗?这里:它是2D,在Gizmosah下没有选择任何内容抱歉,我从来没有在2D中构建,所以我不知道这是隐藏的。运行游戏时,你的游戏视图是什么样子的?你是一个天使,我会自豪地给你赏金!非常感谢您的帮助,回答得很好!它就像一个符咒!
    camera.orthographicSize = 18 * prefab.GetComponent<Renderer>().bounds.size.y * 0.5f;
    
            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