2017-03-28 12 views
1

나는 C#을 단일성으로 배우고 있으며 몇 가지 포인터를 사용할 수 있습니다.CreateCell C# 절차 격자 생성 통합을 제한하는 방법

catlikecoding 16 진수지도 튜토리얼을 따르고 있는데, 내 자신 만의 방법으로 그리드를 수정했습니다.

http://catlikecoding.com/unity/tutorials/hex-map-1/

내 목표는 절차 적으로 7 * 7 그리드에서 출발 사각형의 피라미드를 만드는 것입니다. CreateCell 기능을 루프에 그들이

x + y > n - 1 where n = grid size (for example (6,1) or (5,6) 

내가 가지고있는 다음 식을 만날 때 (x, y) 좌표와 세포가 생성되지 않도록 내가 어떻게 제한을 배치합니까 조립식 비행기

을 사용하고 있습니다 지상 비행기 아래에 원하지 않는 비행기가있는 비행기의 마름모를 만들 때까지 받았다.

스크립트는 다음과 같습니다.

공용 클래스 HexGrid : MonoBehaviour {

public int width = 7; 
public int height = 7; 
public int length = 1; 


public SquareCell cellPrefab; 
public Text cellLabelPrefab; 

SquareCell[] cells; 

Canvas gridCanvas; 

void Awake() { 
    gridCanvas = GetComponentInChildren<Canvas>(); 

    cells = new SquareCell[height * width * length]; 

    for (int z = 0 ; z < height; z++) { 
     for (int x = 0; x < width; x++) { 
      for (int y = 0; y < length; y++) 
       CreateCell(x, z, y); 
     } 
    } 
} 

void CreateCell(int x, int z, int y) { 
    Vector3 position; 
    position.x = x * 10f ; 
    position.y = ((y + 1) - (x + z)) * 10f + 60f; 
    position.z = z * 10f ; 

    Cell cell = Instantiate<Cell>(cellPrefab); 
    cell.transform.SetParent(transform, false); 
    cell.transform.localPosition = position; 

    Text label = Instantiate<Text>(cellLabelPrefab); 
    label.rectTransform.SetParent(gridCanvas.transform, false); 
    label.rectTransform.anchoredPosition = 
     new Vector2(position.x, position.z); 
    label.text = x.ToString() + "\n" + z.ToString(); 
} 
} 

그리드 지금까지 Image

답변

0

빠른 해결책은 세포를 생성하는 코드의 일부 전에 if 문을 추가하는 것입니다. 이 경우 CreateCell() 메소드. if 문은 코드에서 논리를 가져야합니다. 또한 검사 할 크기에 대해 두 개의 변수를 만들어야합니다. 예를 들어

public int tempX; 
public int tempY; 
void Awake() { 
gridCanvas = GetComponentInChildren<Canvas>(); 

cells = new SquareCell[height * width * length]; 

for (int z = 0 ; z < height; z++) { 
    for (int x = 0; x < width; x++) { 
     for (int y = 0; y < length; y++) 
      { 
       if (x + y < (tempX + tempY) - 1) 
       { 
        CreateCell(x, z, y); 
       } 
      } 
     } 
    } 
} 
+1

_public INT tempY; _ _int 'TempY = height'_ _'if 및 (X + Y Freddo1083