我想用方格统一绘制游戏板,我是初学者
i want to draw the gameboard with square grid in unity and i am a begineer
我想画一个有网格的游戏板 我已经尝试过居中游戏板宽度为总宽度的 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;
}
}
}
但是当我根据行和列缩放它时它不是正方形。
嗯...我不确定你想要的行为是什么,但你为什么期望每个网格单元格都是正方形的?连Debug.Log
都说eachcellWidth
是1.125
,eachcellHeight
是1.5
。您正在缩放两次,一次是从 eachcell
值,一次是从 actualSprite
值,因此很难确定要缩放到哪个值。
如果你想要的是制作网格,你不必费力制作。 Unity 已经支持构建网格。尝试阅读 this 并加以利用。
我想画一个有网格的游戏板 我已经尝试过居中游戏板宽度为总宽度的 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;
}
}
}
但是当我根据行和列缩放它时它不是正方形。
嗯...我不确定你想要的行为是什么,但你为什么期望每个网格单元格都是正方形的?连Debug.Log
都说eachcellWidth
是1.125
,eachcellHeight
是1.5
。您正在缩放两次,一次是从 eachcell
值,一次是从 actualSprite
值,因此很难确定要缩放到哪个值。
如果你想要的是制作网格,你不必费力制作。 Unity 已经支持构建网格。尝试阅读 this 并加以利用。