2014-06-24 4 views
3

나는 장래에 생성 된 절차를 만들고 싶은 타일 배경으로 Shmup 게임을 만들려고합니다. 하지만 지금은 정적 인 배경을 만들 수 없습니다. 무슨 일이 있어도 타일을 화면에 채울 수는 없습니다.절차 타일 된 배경 생성

64x64 픽셀의 타일을 사용하고 있습니다.

그리고 여기에 코드를 내가 사용 :

여기
void Start() 
{ 
    m_screenHeight = 1408; 
    m_screenWidht = Camera.main.aspect * m_screenHeight; 

    float UnitsPerPixel = 1f/100f; 

    Camera.main.orthographicSize = m_screenHeight/2f * UnitsPerPixel; 

    m_horizontalTilesNumber = (int)Math.Floor(m_screenWidht/TileSize); 
    m_verticalTilesNumber = (int)Math.Floor(m_screenHeight/TileSize); 

    for (int i = 0; i < m_horizontalTilesNumber; i++) 
    { 
     for (int j = 0; j < m_verticalTilesNumber; j++) 
     { 
      Instantiate(Tile, Camera.main.ScreenToWorldPoint(new Vector3(TileSize * i, TileSize * j, 0)), Quaternion.identity); 
     } 
    } 
} 

그것은 좋아 보이는 것입니다 :

enter image description here

내가 내 픽셀 좌표를 변환하는 몇 가지 문제가있어 나에게 보인다 단위, 또는 이와 비슷한.

여기의 도움말이나 안내는 도움이 될 것입니다.

+0

를 뭐죠 TileSize의 가치인가? 텍스처와 같은 크기 여야합니다. 또한 카메라가 직교로 설정되어 있는지 확인하십시오. – Imapler

+0

@Imapler TileSize = 64 (픽셀)이며 카메라가 직교로 설정되어 있습니다. – Giusepe

답변

4

아래 코드를 사용해보십시오 :

/// <summary> 
/// Tile prefab to fill background. 
/// </summary> 
[SerializeField] 
public GameObject tilePrefab; 

/// <summary> 
/// Use this for initialization 
/// </summary> 
void Start() 
{ 
    if (tilePrefab.renderer == null) 
    { 
     Debug.LogError("There is no renderer available to fill background."); 
    } 

    // tile size. 
    Vector2 tileSize = tilePrefab.renderer.bounds.size; 

    // set camera to orthographic. 
    Camera mainCamera = Camera.main; 
    mainCamera.orthographic = true; 

    // columns and rows. 
    int columns = Mathf.CeilToInt(mainCamera.aspect * mainCamera.orthographicSize/tileSize.x); 
    int rows = Mathf.CeilToInt(mainCamera.orthographicSize/tileSize.y); 

    // from screen left side to screen right side, because camera is orthographic. 
    for (int c = -columns; c < columns; c++) 
    { 
     for (int r = -rows; r < rows; r++) 
     { 
      Vector2 position = new Vector2(c * tileSize.x + tileSize.x/2, r * tileSize.y + tileSize.y/2); 

      GameObject tile = Instantiate(tilePrefab, position, Quaternion.identity) as GameObject; 
      tile.transform.parent = transform; 
     } 
    } 
} 

현재 전체 데모 프로젝트 찾을 수 있습니다 https://github.com/joaokucera/unity-procedural-tiled-background

+0

화면을 채우기 위해 두 열을 더 추가해야만하고 매력처럼 작동했습니다. \t \t int 행 = Mathf.CeilToInt (mainCamera.orthographicSize/tileSize.y) + 2; – Giusepe