2016-03-03 90 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像素,并确定。谢谢 –