简体   繁体   English

如何获得正确的统一主相机尺寸?

[英]How to get correct unity main camera dimensions?

As I have known, the correct size for the camera is half of your wanted width, so in my case: 众所周知,相机的正确尺寸是所需宽度的一半,因此在我的情况下:

Width: 1920
Height: 1080
Camera Size: 540
Each block represents 64x64 pixels

In unity I was having this problem by start placing blocks in the -960 (half of 1920, which is the camera minimun x): 团结起来,我开始将块放置在-960(1920年下半年,这是相机的最小x)中来解决这个问题: 在此处输入图片说明 Since as shown I thought, just add half of the blocks width to its x, and subtract half of its height in its y, so this happen: 正如我所想的那样,只需将块宽度的一半加到它的x上,然后将块高度的一半减到它的y上,这样就发生了: 在此处输入图片说明 After a lots of tries I figured out it was supposed to be placed at -938 and 518, which is 22 units away from the corner, by placing in that position this is my result: 经过大量尝试,我发现应该将其放置在-938和518处,这距离拐角处22个单位,通过将该位置放置,这是我的结果: 在此处输入图片说明 (Ignore the other blocks for now, they are with wrong algorithm now) (暂时忽略其他块,它们现在使用了错误的算法)

So what I ask is: Why 22? 所以我要问的是:为什么要22? How can I get to that value from my starting values? 如何从初始值中获得该值? (Please don't answer something like 2.032%, because probably have something more integer to its math calculation) (请不要回答2.032%之类的内容,因为它的数学计算可能会包含更多的整数)

Code if needed: 代码是否需要:

        float height = Camera.main.orthographicSize *2f;
        float width = height / (float)Screen.height * (float)Screen.width;
        Destroy(map);
        map = new GameObject();
        print("W: "+width+". H: "+height);
        lastH=height;
        lastW=width;
        for (int i=0; i<30; i++) {
            for (int j=0; j<16; j++) {
                if(mapa[i,j]>0){
                    GameObject aux = objetos[mapa[i,j]-1];
                    float wX=aux.transform.localScale.x,hY=aux.transform.localScale.y;
                    print ("wX: "+wX+", hY: "+hY);
                    float unidadeW = 2*(float)lastW/(float)(20*wX);
                    float unidadeH = 2*(float)lastH/(float)(20*hY);
                    //aux.transform.localScale = new Vector3(unidadeW,unidadeH,0);
                    GameObject t = (GameObject)Instantiate(aux, new Vector2(j*wX*wX/100-width/2+wX*wX/200,-i*hY*hY/100+height/2-hY*hY/200),Quaternion.identity);
                    t.transform.parent = map.transform;
                }
            }
        }

The correct way to make a distance between two units is their diagonal, see the image below: 在两个单位之间建立距离的正确方法是对角线,请参见下图: 在此处输入图片说明

Doesn't matter which is your orientation point, if its top left or center, the distance between the blocks will be √2*side, so the correct distance between mine was around 22, and I can find it by doing this equation: 不管是哪个定向点,如果它的左上角或中心点,那么块之间的距离将是√2* side,因此我的正确距离是22,我可以通过执行以下公式找到它:

sqrt(2)/2*32=22.62 (the correct distance between two tiles of 64x64), instead of using 64+ at the last x and 64+ at the last y the right way to do is by adding 22.62 at both multiplications. sqrt(2)/2*32=22.62(两个64x64的图块之间的正确距离),而不是在最后一个x使用64+和在最后一个y使用64+,正确的方法是在两个乘法中都加上22.62。

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

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