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# 我想用统一的正方形网格绘制游戏板,我是初学者_C#_Unity3d_Game Development - Fatal编程技术网

C# 我想用统一的正方形网格绘制游戏板,我是初学者

C# 我想用统一的正方形网格绘制游戏板,我是初学者,c#,unity3d,game-development,C#,Unity3d,Game Development,我想画一个有网格的游戏板,我把游戏板的中心宽度设为总宽度的80%,高度设为总高度的60%,我试过了,但仍然不能兼容所有行和列的值,请查看代码并帮助我 private void SetupGrid() { float height=Camera.main.orthographicSize*2f; float width=Camera.main.aspect*height; Debug.Log("height:"+height); Debug.Log("width:

我想画一个有网格的游戏板,我把游戏板的中心宽度设为总宽度的80%,高度设为总高度的60%,我试过了,但仍然不能兼容所有行和列的值,请查看代码并帮助我

private void SetupGrid()
{
    float height=Camera.main.orthographicSize*2f;
    float width=Camera.main.aspect*height;

    Debug.Log("height:"+height);
    Debug.Log("width:"+width);

    float possibleHeight=0.6f*height;
    float possibleWidth=0.8f*width;

    Debug.Log("possibleHeight:"+possibleHeight);
    Debug.Log("possibleWidth:"+possibleWidth);

    float eachcellWidth=possibleWidth/mWidth;
    float eachcellHeight=possibleHeight/mHeight;

    Debug.Log("eachcellWidth:"+eachcellWidth);
    Debug.Log("eachcellHeight:"+eachcellHeight);

    float cellCenterX=eachcellWidth/2;
    float cellCenterY=eachcellHeight/2;

    Debug.Log("cellCenterX:"+cellCenterX);
    Debug.Log("cellCenterY:"+cellCenterY);

    float offSetBetweenEachCell=eachcellWidth*0.15f;

    float startXpos=-(mWidth/2*eachcellWidth)+cellCenterX;
    float startYpos=-(mHeight/2*eachcellHeight)+cellCenterY;

    Debug.Log("start X position value:"+startXpos);
    Debug.Log("start Y position value:"+startYpos);

    SpriteRenderer spriteRenderer=mTilePrefab.GetComponent<SpriteRenderer>();

    float actualSpriteWidth=spriteRenderer.sprite.bounds.size.x;
    float actualSpriteHeight=spriteRenderer.sprite.bounds.size.y;

    Debug.Log("Actual Sprite width:"+actualSpriteWidth);
    Debug.Log("Actual Sprite Height:"+actualSpriteHeight);

    float finalCellWidth=eachcellWidth-offSetBetweenEachCell;
    float finalCellHeight=eachcellHeight-offSetBetweenEachCell;

    Vector3 eachCellScaleUnit=new Vector3(finalCellWidth/actualSpriteWidth,finalCellHeight/actualSpriteHeight);

    Vector3 referenceVector=Vector3.one;
    GameObject cell=null;
    for(int i=0;i<mWidth;i++)
    {
        for(int j=0;j<mHeight;j++)
        {
            float posX=startXpos+(j*eachcellWidth);
            float posY=startYpos+(i*eachcellHeight);

            referenceVector.x=posX;
            referenceVector.y=posY;

            cell=Instantiate(mTilePrefab,referenceVector,Quaternion.identity);
            cell.transform.localScale=eachCellScaleUnit;
        }
    }
}
private void SetupGrid()
{
浮动高度=摄影机.main.正交尺寸*2f;
浮动宽度=摄影机.main.aspect*高度;
Debug.Log(“高度:+高度);
Log(“宽度:“+width”);
浮动可能高度=0.6f*高度;
浮动可能宽度=0.8f*宽度;
Log(“possiblehight:+possiblehight”);
Log(“possibleWidth:+possibleWidth”);
浮动每个单元格宽度=可能宽度/宽度;
浮动每个单元格高度=可能的高度/mh;
Log(“eachcellWidth:+eachcellWidth”);
Log(“eachcellHeight:+eachcellHeight”);
浮动单元格中心x=每个单元格宽度/2;
浮动单元中心Y=每个单元高度/2;
Log(“cellCenterX:+cellCenterX”);
Log(“cellCenterY:+cellCenterY”);
每个单元格之间的浮动偏移=每个单元格宽度*0.15f;
float startXpos=-(mWidth/2*每个单元格宽度)+cellCenterX;
浮动星型=-(mHweight/2*每个单元格高度)+单元格中心;
Log(“开始X位置值:“+startXpos”);
Log(“起始Y位置值:“+startYpos”);
spritender spritender=mtileprefable.GetComponent();
float actualSpriteWidth=spriteRenderer.sprite.bounds.size.x;
float actualSpriteHeight=spriteRenderer.sprite.bounds.size.y;
Log(“实际精灵宽度:”+实际精灵宽度);
Log(“实际精灵高度:”+实际精灵高度);
float finalCellWidth=每个单元格之间的每个单元格宽度偏移量;
浮动最终单元格高度=每个单元格之间的每个单元格高度偏移量;
Vector3 eachCellScaleUnit=新的Vector3(最终单元格宽度/实际优先级宽度,最终单元格高度/实际优先级高度);
矢量3参考矢量=矢量3.1;
游戏对象单元格=空;

对于(int i=0;i嗯……我不确定您想要的行为是什么,但为什么您希望每个网格单元都是方形的?甚至
Debug.Log
eachcellWidth
1.125
eachcellHeight
1.5
。您要缩放两次,一次是从
eachcell
值,一次是从
实际值Sprite
值,因此很难确定要缩放到哪个值


如果你想要的是制作网格,你不必费劲地制作。Unity已经支持构建网格。试着阅读并利用它。

你能给我发一些教程,告诉我如何在Unity中准确地使用内置网格吗?我是一个初学者,非常感谢你的帮助