コード例 #1
0
 public void updateGrid()
 {
     if (gridSize.x * gridSize.y >= transform.childCount)// to understand that should we add or remove elements in the new situation
     {
         int[,] hashMap = new int[(int)(gridSize.x), (int)(gridSize.y)];
         foreach (Transform element in transform)
         {
             IGridPiece gridInterface = element.GetComponent <IGridPiece>();
             hashMap[(int)gridInterface.point.x, (int)gridInterface.point.y] = 1;
         }
         Debug.Log("Hashmap length is: " + hashMap.Length);
         for (int i = 0; i < gridSize.x; i++)
         {
             for (int j = 0; j < gridSize.y; j++)
             {
                 if (hashMap[i, j] != 1)
                 {
                     Transform createdPiece = ObjectPooler.Instance.SpawnFromPool("circle", Vector3.zero, Quaternion.identity).transform;
                     createdPiece.GetComponent <IGridPiece>().point = new Vector2(i, j);
                     createdPiece.parent = transform;
                     //createdPiece.GetComponent<IGridPiece>().UpdatePositionandScale(calculatePos(i, j), calculateScale());
                 }
             }
         }
     }
     else
     {
         int childrenNumber = transform.childCount;
         for (int i = childrenNumber - 1; i >= 0; i--)
         {
             IGridPiece gridPiece      = transform.GetChild(i).GetComponent <IGridPiece>();
             Vector2    pointOfelement = gridPiece.point;
             if (pointOfelement.x >= gridSize.x || pointOfelement.y >= gridSize.y)
             {
                 transform.GetChild(i).parent = null;
                 gridPiece.Disappear();
             }
         }
     }
     foreach (Transform element in transform)
     {
         IGridPiece gridInterface = element.GetComponent <IGridPiece>();
         gridInterface.UpdatePositionandScale(calculatePos((int)gridInterface.point.x, (int)gridInterface.point.y), calculateScale());
         //element.localPosition = calculatePos((int)gridInterface.point.x, (int)gridInterface.point.y);
     }
 }