Я хочу нарисовать игровое поле с квадратной сеткой в ​​единстве, и я начинающий - PullRequest
0 голосов
/ 16 апреля 2020

Я хочу нарисовать игровую доску, в которой есть сетки. У меня центральная ширина игровой доски равна 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;
        }
    }
}

, но это не квадрат, когда я масштабирую его в соответствии со строкой и столбцом. Please refer the image

1 Ответ

0 голосов
/ 16 апреля 2020

Хмм ... Я не уверен, каково ваше поведение, но почему вы ожидаете, что каждая ячейка сетки будет квадратной? Даже Debug.Log говорит, что eachcellWidth это 1.125, а eachcellHeight это 1.5. Вы масштабируете дважды, один раз из eachcell значений и один раз из actualSprite значений, поэтому трудно определить, какое значение вы хотите масштабировать до.

Если вы хотите создать сетку, вам не нужно не нужно делать это нелегко. Unity уже поддерживает построение сетки. Попробуйте прочитать this и использовать его.

...