- Home /
I have issue with unitys serialization
Hi, I'm having a issue with unitys serialization. I'm creating a new scriptableObject and then saving it to assets and it serializes only some of its components. (I'm working on a tile map editor and it wont serialize the created sprites) The editor works just fine until i press playmode, then i have to create the array of tiles again. I have single Tile variable in a different script that inherits a monobehavior and it serializes properly
TileAtlas serializes texture but not the tiles
Sorry for the lenghty format of code
[System.Serializable]
public class TileAtlas : ScriptableObject
{
public static readonly int size = 8;
public static readonly float uvSize = 0.125f;
public Texture2D texture;
public Tile[] tiles;
public TileAtlas(Texture2D texture)
{
this.texture = texture;
}
public void CreateSprites()
{
tiles = new Tile[size*size];
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
Rect rect = new Rect(x*(uvSize*texture.width),y*(uvSize*texture.height),uvSize*texture.width,uvSize*texture.height);
Sprite sprite = Sprite.Create(texture,rect,new Vector2(0,0),uvSize*texture.width);
Tile tile = new Tile(sprite);
this.tiles[x+(y*size)] = tile;
}
}
}
}
[System.Serializable]
public class Tile
{
public Sprite sprite;
public Tile (Sprite sprite)
{
this.sprite = sprite;
}
}
Answer by whydoidoit · Feb 22, 2014 at 04:29 PM
Tile requires a default constructor that takes no parameters to be serialized correctly.
Im assu$$anonymous$$g you mean this? It didn't work.
[System.Serializable]
public class Tile
{
public Sprite sprite;
public Tile ()
{
this.sprite = new Sprite();
}
public Tile (Sprite sprite)
{
this.sprite = sprite;
}
}
I found this quite weird but if set the tile.sprite to a spriteRenderer then the tile.sprite gets serialized?! this makes absolutely no sense to me.
Actually you should just need
public Tile() {}
Just so it can create one. I'm not sure on what might be happening with the sprite and renderer thing though
@whydoidoit can you elaborate more on why would one needs an empty constructor for a regular class to be serialized correctly? I use regular classes quite often - but I never payed attention to that detail with no problems so far
If you have a constructor - which the OP does - then the compiler no longer makes you a default one which takes no parameters. As the serialization system has no idea what you are supposed to pass to the constructors it works by using the default one and then setting the variables of the resulting object. If you don't have a default constructor then it can't make that initial instance so if you declare any constructor that takes parameters you must also define one that does not to support serialization.
Your answer
Follow this Question
Related Questions
Pixels per unit with tile palette? 1 Answer
tilepalette problems 0 Answers
How can I disable collision for 1 tile? 4 Answers
Get sprite of Tile 2 Answers
Is it possible to turn a tilemap into one unique sprite? 1 Answer