如何在正交相机中适应二维网格

问题描述 投票:0回答:1

我创建随机大小的二维网格并将相机设置到网格的中间,我还尝试理解正交尺寸并调整它以适合整个网格,但我想我失败了,以下是代码片段和拟合问题:

如果您对我的编程和设计风格有任何其他建议,我也将不胜感激。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.WSA;

public class Grid2D : MonoBehaviour
{
    [SerializeField] private int _width, _height;
    [SerializeField] private Tile  _tilePrefap;
    [SerializeField] private  Camera _cam;

    void Start()
    {

        GenerarteGrid();
        SetCamera(_width,_height);

    }
    // Update is called once per frame
    void Update()
    {
        
    }

    private void GenerarteGrid()
    {
        for (int i = 0; i < _width; i++)
        {
            for (int j = 0; j < _height; j++)
            {
                var tile = Instantiate(_tilePrefap, new Vector3(i, j, 0), Quaternion.identity);
                tile.transform.parent = transform;
                tile.name = j + " " + i;

            }
        }
    }



    private void SetCamera(int x, int y)
    {
        _cam.orthographicSize = (float) _height / 2 + 0.5f ;

        _cam.transform.position = new Vector3((float)x / 2 - 0.5f, (float)y / 2 - 0.5f, 0);
    }


}

c# unity-game-engine 2d game-development
1个回答
0
投票

您必须检查是否匹配高度或宽度,例如

_cam.orthographicSize = width > height ? width / camera.aspect / 2f : height / 2f;
// optional space
_cam.orthographicSize += 0.5f;
© www.soinside.com 2019 - 2024. All rights reserved.