Search Unity

Simple Inspector based Map Editor

Discussion in 'Scripting' started by Fhiz, Apr 20, 2018.

  1. Fhiz

    Fhiz

    Joined:
    Feb 28, 2018
    Posts:
    81
    Hey guys,
    Can someone provide me with some information about how to implement a very simple Grid/Map Editor via the Inspector and a Scriptable Object? I tried various implementations but Im not happy at all.

    Basically, what Im looking for is the simplest map editor ever: a 2d array grid of Sprites edited via the Inspector and saved as Scriptable Object.

    Attached is the code Im using right now, as well as a screen. This shows the implementation of using a Color (was not able to remove the Eyedropper).

    But: I would prefer using Sprites to determine the Tile that is placed on a grid space. But Sprites do not show up in the Inspector at all (I see just that selection ring and the beginning of the filename).

    I guess I have to replace Color with a CustomPropertyDrawer - any advice here?

    Code (CSharp):
    1. [CreateAssetMenu(fileName = "MapData", menuName = "New Map")]
    2. public class MapData : ScriptableObject
    3. {
    4.     private const int defaultGridSize = 3;
    5.  
    6.     [Range(1, 30)]
    7.     public int gridSize = defaultGridSize;
    8.  
    9.     public CellRow[] cells = new CellRow[defaultGridSize];
    10.  
    11.  
    12.     public Color[,] GetCells()
    13.     {
    14.         Color[,] ret = new Color[gridSize, gridSize];
    15.  
    16.         for (int i = 0; i < gridSize; i++)
    17.         {
    18.             for (int j = 0; j < gridSize; j++)
    19.             {
    20.                 ret[i, j] = cells[i].row[j];
    21.             }
    22.         }
    23.  
    24.         return ret;
    25.     }
    26.  
    27.     [System.Serializable]
    28.     public class CellRow
    29.     {
    30.         [ColorUsageAttribute(false)]public Color[] row = new Color[defaultGridSize];
    31.     }
    32.    
    33. }
     

    Attached Files:

    • test.jpg
      test.jpg
      File size:
      26.3 KB
      Views:
      653
  2. Fhiz

    Fhiz

    Joined:
    Feb 28, 2018
    Posts:
    81
    Sorry for the double-post (and answering my own question) - if anybody is interested:

    Im now using a open-source C# script (2dTilemapEditor) in combination with my own code and project. The script is outdated but easy to adjust and implement if you want to give it a whirl yourself:

    https://github.com/toinfiniityandbeyond/unity-tilemap/

    And here we have two screenshots, showing the tilemap editor and the prepared scene at runtime:

    editor.jpg runtime.jpg
     
    inkcomic and Kurt-Dekker like this.