Search Unity

Issues with sorting Layers at runtime

Discussion in '2D' started by tiddlesworthgames, May 6, 2019.

  1. tiddlesworthgames

    tiddlesworthgames

    Joined:
    Dec 8, 2017
    Posts:
    3
    So let me start by saying i am not sure if this is the right place for my issue. I'm currently working on an editor tool that generates a hex grid. I set the sorting order as i instantiate my object:

    Code (CSharp):
    1. SpriteRenderer rend = tempCell.GetComponent<SpriteRenderer>();
    2.         rend.sortingOrder = -(sortOrder);
    In the Editor everything looks fine, exactly how it is supposed to look. It all works great.



    But as soon as i play it, the layers re-order themselves and ruins the whole thing.



    I need the sorting order to be correct because many of my sprites over extend the bounds the cell and i would like them to still appear correctly. Im pretty sure it has something to do with the hierarchy sorting order but im not really sure how to disable that. i could be wrong. ill include my Editor Tool class and my HexGrid Class below.
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. [ExecuteInEditMode]
    5. public class HexGrid : MonoBehaviour
    6. {
    7.     public int rows;
    8.     public int columns;
    9.     int sortOrder;
    10.     int sortHelper;
    11.  
    12.     public Cell cellPrefab;
    13.     Cell[,] cells;
    14.     public List<Cell> wut;
    15.     Vector3 spawnLoc;
    16.     float xBuffer;
    17.     float yBuffer;
    18.     bool buffered;
    19.  
    20.     public void CreateGrid(int x, int y)
    21.     {
    22.         sortOrder = 0;
    23.         sortHelper = 0;
    24.         xBuffer = 0;
    25.         yBuffer = 0;
    26.         rows = x;
    27.         columns = y;
    28.         spawnLoc = new Vector3(0, 0, 0);
    29.         buffered = false;
    30.         //grid = Instantiate(this);
    31.         //change to transform of world parent object.
    32.         GenCells();
    33.     }
    34.  
    35.     public void GenCells()
    36.     {
    37.         cells = new Cell[rows, columns];
    38.  
    39.         for (int x = 0; x < rows; x++)
    40.         {
    41.             if ((x & 1) != 0)
    42.                 sortHelper++;
    43.             sortOrder = sortHelper;
    44.             for(int y = 0; y < columns; y++)
    45.             {
    46.                 GenCell(x, y);
    47.                 spawnLoc.y += yBuffer;
    48.                 sortOrder++;
    49.             }
    50.             spawnLoc.y = 0;
    51.             if ((x & 1) == 0)
    52.             {
    53.                 spawnLoc.y += yBuffer / 2;
    54.             }
    55.             spawnLoc.x += xBuffer;
    56.         }
    57.     }
    58.  
    59.     public void GenCell(int x, int y)
    60.     {
    61.         Cell tempCell = cells[x,y] = Instantiate(cellPrefab, this.transform);
    62.         tempCell.transform.localPosition = spawnLoc;
    63.         tempCell.x = x;
    64.         tempCell.y = y;
    65.         SpriteRenderer rend = tempCell.GetComponent<SpriteRenderer>();
    66.         rend.sortingOrder = -(sortOrder);
    67.        
    68.         if (!buffered)
    69.         {
    70.             BoxCollider2D bounds = tempCell.GetComponent<BoxCollider2D>();
    71.             xBuffer = bounds.bounds.size.x;
    72.             yBuffer = bounds.bounds.size.y;
    73.             buffered = true;
    74.         }
    75.         //remove boxcollider2d from each cell...
    76.         if (y > 0)
    77.         {
    78.             tempCell.SetNeighbor(HexDirection.S, cells[x, y - 1]);
    79.         }
    80.         if (x > 0)
    81.         {
    82.             if ((x & 1) == 0)
    83.             {
    84.                 tempCell.SetNeighbor(HexDirection.NW, cells[x - 1, y]);
    85.                 if (y > 0)
    86.                 {
    87.                     tempCell.SetNeighbor(HexDirection.SW, cells[x - 1, y - 1]);
    88.                 }
    89.             }
    90.             else
    91.             {
    92.                 tempCell.SetNeighbor(HexDirection.SW, cells[x - 1, y]);
    93.                 if (y < columns - 1)
    94.                 {
    95.                     tempCell.SetNeighbor(HexDirection.NW, cells[x - 1, y + 1]);
    96.                 }
    97.             }
    98.         }
    99.     }
    100.  
    101. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class GridGen : ScriptableWizard
    7. {
    8.     public int x = 3;
    9.     public int y = 4;
    10.     public Cell cellPrefab;
    11.  
    12.     [MenuItem("My Tools/Grid Generater")]
    13.     static void CreateWizard()
    14.     {
    15.         ScriptableWizard.DisplayWizard<GridGen>("Grid Generation", "Generate");
    16.     }
    17.  
    18.     private void OnWizardCreate()
    19.     {
    20.         GameObject gridObj = new GameObject();
    21.         gridObj.name = "Grid";
    22.         HexGrid grid = gridObj.AddComponent<HexGrid>();
    23.         grid.cellPrefab = cellPrefab;
    24.         grid.CreateGrid(x, y);
    25.     }
    26. }
    Thanks for any assitance you can provide in resolving my issues.
     

    Attached Files: