Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trying to Script TileMaps, Saving issues...

Discussion in '2D' started by Denchyaknow, Nov 21, 2017.

  1. Denchyaknow

    Denchyaknow

    Joined:
    Aug 4, 2015
    Posts:
    31
    Hey guys I hope you can help, I looked everywhere but I can figure out why my tile maps get all fuzzed up when loading a prefab of a tilemap.

    Sketch.png

    As I say.. drag it into the scene from the project window unity starts having a fit and freezes, its about 10 mins until I can finally delete it from the hierarchy.

    So here my code thats run on a blank tilemap to spawn a CustomTile class I made that inherits from Tile

    Code (CSharp):
    1. public void SpawnTiles()
    2.     {
    3.         if (FloorTile == null)
    4.         {
    5.             FloorTile = ScriptableObject.CreateInstance<CustomTile>();
    6.         }
    7.         FloorTile.sprite = FloorSprite;
    8.         FloorTile.color = FloorColor;
    9.         if (WallTile == null)
    10.         {
    11.             WallTile = ScriptableObject.CreateInstance<CustomTile>();
    12.         }
    13.         WallTile.sprite = WallSprite;
    14.         WallTile.color = WallColor;
    15.         if (tiles.Length != mapSizeX * mapSizeY)
    16.             tiles = new CustomTile[mapSizeX * mapSizeY];
    17.         if (tilePositions.Length != mapSizeX * mapSizeY)
    18.             tilePositions = new Vector3Int[mapSizeX * mapSizeY];
    19.         tileMap.ClearAllTiles();
    20.         int index = 0;
    21.         for (int y = 0; y < cellmap.GetLength(1); y++)
    22.         {
    23.             for (int x = 0; x < cellmap.GetLength(0); x++)
    24.             {
    25.                 tilePositions[index] = new Vector3Int(x, y, 0);
    26.                 cellCache[x, y] = cellmap[x, y];
    27.                 switch (cellmap[x, y])
    28.                 {
    29.                     case 0://Floor Tile
    30.                         tileMap.SetTile(tilePositions[index], FloorTile);
    31.                         //tiles[index] = ScriptableObject.CreateInstance<CustomTile>();
    32.                         //tiles[index].sprite = FloorSprite;
    33.                         //tiles[index].color = FloorColor;
    34.                         //tiles[index] = new CustomTile(FloorSprite, FloorColor);
    35.                         break;
    36.                     case 1://Wall Tile
    37.                         tileMap.SetTile(tilePositions[index], WallTile);
    38.  
    39.                         //tiles[index] = ScriptableObject.CreateInstance<CustomTile>();
    40.                         //tiles[index].sprite = WallSprite;
    41.                         //tiles[index].color = WallColor;
    42.                         //tiles[index] = new CustomTile(WallSprite, WallColor);
    43.                         break;
    44.                 }
    45.                 index++;
    46.             }
    47.         }
    48.         //tileMap.SetTiles(tilePositions, tiles);
    49.         Debug.Log("Spawned " + tiles.Length.ToString() + " Tiles");
    50.  
    51.         tileMap.RefreshAllTiles();
    52.     }
    oh and heres my custom tile class just incase
    Code (CSharp):
    1. [System.Serializable]
    2. public class CustomTile : Tile
    3. {
    4.     public CustomTile()
    5.     {
    6.     }
    7.     public CustomTile(Sprite _sprite, Color _color)
    8.     {
    9.         sprite = _sprite;
    10.         color = _color;
    11.  
    12.     }
    13. }
    Then i just save it via prefabutility or manually.
    everything is fine up until the point I want to load the prefab. Even if i saved the prefab manually via dragging it into the project window it still comes back fuzzy.

    Anyone know whats up? I figured it might be because my Tiles are not actual assets just instances. if this is the case does anyone have a work around?
     
  2. Denchyaknow

    Denchyaknow

    Joined:
    Aug 4, 2015
    Posts:
    31
    Ok I dont know what I changed to get this but i think its progress
    Now when i bring back the prefab into the scene unity doesnt freeze but the tilemap still comes out odd.
    Sketch (1).png
    top grid is what it looks like when i save it
    bottom grid is what it looks like when i load the prefab.
     
  3. Denchyaknow

    Denchyaknow

    Joined:
    Aug 4, 2015
    Posts:
    31
    Ok actually...
    everytime i drag the prefab back in it comes out a different shade of pink? whaaaaaaaat the heck lol
    Sketch (2).png
     
  4. AleksikBRB

    AleksikBRB

    Joined:
    Jan 24, 2018
    Posts:
    1
    When there's no material or sprite given, Unity renders the stuff in pink.