2016-03-03 88 views
0

我創建了一個int數組的層次。這是代碼:錯誤的瓷磚佈局

using UnityEngine; 
using System.Collections; 

public class Level1 : MonoBehaviour 
{ 
    int[][] level = new int[][] 
    { 
     new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     new int[] { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16} 
    }; 
    public Transform tile00; 
public Transform tile16; 
public Transform tile38; 

int rows = 12; 
int cols = 32; 

void Start() 
{ 

    BuildLevel(); 
} 

void BuildLevel(){ 
    int i, j; 

    GameObject dynamicParent = GameObject.Find ("DynamicObjects"); 
    for(i=0; i<rows; i++) 
    { 
     for(j=0; j<cols; j++) 
     { 
      Transform toCreate = null; 
      Debug.Log (i + " , " + j + " " + level[i][j]); 
      if (level[i][j] == 0) 
       toCreate = tile00; 
      if (level[i][j] == 83) 
       toCreate = tile38;; 
      if (level[i][j] == 16) 
       toCreate = tile16; 

      Vector3 v3 = new Vector3(16-j, 6-i, 0); 
      Transform newObject = Instantiate(toCreate, v3, Quaternion.identity) as Transform; 
      newObject.parent = dynamicParent.transform; 
     } 
    } 
} 
    } 

輸出畫面是這樣的:

enter image description here

將瓷磚50 X 50我改變磚的尺寸,我改變上的X和Y位置我嘗試了一切,但沒有找到解決辦法。請給我一個理念,好嗎?

對於水平瓷磚我想獲得的佈局(圖像與油漆處理):

enter image description here

+0

呃...它目前還不完全清楚你希望你的代碼做什麼 - 你能弄清楚如何預計你的場景會在運行你的代碼之後看到?否則,我們只是猜測這裏可能是錯的。 – Serlite

+0

試過什麼解決方案?你甚至不指定你想要的佈局。 –

+0

@Serlite - 我發佈了我想要的佈局圖片。 –

回答

0

最可能的答案是,因爲這條線的

Vector3 v3 = new Vector3(16-j, 6-i, 0); 

你說你的圖像是每個50 x 50像素。假設您沒有將像素更改爲精靈的單位屬性,則這兩個X軸上的每個圖像佔用0.5個Unity單位的空間。

現在,在您的計算中,這裏是發生了什麼。


迭代1-(i = 0,j = 0)。位置= Vector3(16,6,0)
迭代2 - (i = 0,j = 1)。 Position = Vector3(15,6,0)
...
迭代33 - (i = 1,j = 0)。位置的Vector3 =(16,5,0)


現在,迭代1 &迭代2之間在X值的差爲1個統一單元。我們之前已經確定,由於它們的大小,這些精靈將僅佔用0.5個Unity單元。
沿着Y軸的相同東西迭代1 &迭代33。相差1個單位,每張圖片僅佔0.5個單位。

所以,要麼改變圖像爲100×100像素,或者改變像素的單位

+0

是的。 Tou're正確。我將像素更改爲單位50像素,並確定。謝謝 –