Hello Guest

Author Topic: Created SpriteCollectionData at runtime, now how to save it to disk?  (Read 2008 times)

Euthyphro

  • Newbie
  • *
  • Posts: 20
    • View Profile
How would won save tk2dSpriteCollectionData to disk so that it doesn't have to be recreated at a later date? I'm guessing it wouldn't be able to be serialized and saved. I couldn't find a decent explanation and I've been fishing around the tk2d files for the past several hours for clues.

This is the code I have, it creates a sprite collection from a png file but the file save does not work because it wont let me serialize tk2dSpriteCollectionData.

Code: [Select]
using UnityEngine;
using System.Collections;
using System.IO;
using PixelShootLibrary;
public class PixelShootCreateTileSpriteCollectiona : MonoBehaviour {


string imageLocation = @"C:\Users\Euthyphro\AppData\Roaming\PixelShoot\MapTextures\stone16x16px.png";
string spriteCollectionDataLocation = @"C:\Users\Euthyphro\AppData\Roaming\PixelShoot\MapTextures\SpriteCollection.dat";
public tk2dSpriteCollectionData spriteCollectionData;
public Texture2D texture;
tk2dBaseSprite spriteInstance = null;

void Start ()
       {
CreateSpriteCollection ();
}
void CreateSpriteCollection()
{
FileManager fm = new FileManager();
if(File.Exists(spriteCollectionDataLocation))
{
spriteCollectionData = (tk2dSpriteCollectionData)fm.OpenStream(spriteCollectionDataLocation);
}
else
{
if(File.Exists(imageLocation))
{
byte[] fileData = File.ReadAllBytes(imageLocation);
spriteCollectionData = Resources.Load ("Sprites/TileMapCollection", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
texture = new Texture2D(288,270);
texture.filterMode = FilterMode.Point;
texture.LoadImage(fileData);
spriteCollectionData.textureFilterMode = FilterMode.Point;
string[] names = new string[256];
Rect[] regions = new Rect[256];
Vector2[] anchors = new Vector2[256];
for(int  y = 0; y < 16; y++)
{
for(int x = 0; x < 16; x++)
{
int pos = x+(16*y);
names[x+(16*y)] = pos.ToString()+"tile";
int offsetX = x;
int offsetY = y;
if(y > 0)
{
offsetY = y*16+(2*y);
}
if(x > 0)
{
offsetX = x*16+(2*x);
}
regions[x+(16*y)] = new Rect(offsetX, offsetY, 16, 16) ;
anchors[x+(16*y)] = new Vector2(offsetX, offsetY);
spriteCollectionData = tk2dSpriteCollectionData.CreateFromTexture(texture,tk2dSpriteCollectionSize.ForTk2dCamera(), names, regions, anchors);
}
}

fm.WriteStream(spriteCollectionDataLocation, spriteCollectionData);
}
}
}


}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9708
    • View Profile
Re: Created SpriteCollectionData at runtime, now how to save it to disk?
« Reply #1 on: October 11, 2014, 02:23:34 pm »
You cant save that to disk at runtime, unity serialization only works in the editor...

Euthyphro

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Created SpriteCollectionData at runtime, now how to save it to disk?
« Reply #2 on: October 12, 2014, 08:25:46 am »
You cant save that to disk at runtime, unity serialization only works in the editor...

Yea that's my point, you can't serialize. So how might I begin to actually save the sprite collection data while at runtime? A lot of my players request to be able to make custom spritemaps by dropping their images in a game folder so they can load their own custom textures in-game but right now with tk2d the way it is they would have to create the collection from scratch each time using CreateFromTexture and as a result that's huge load time every time you go in-game especially when there are 40+ tilemaps each with 256 tiles per tilemap. Ideally instead I would like to have them created once and saved. Short of rewriting tk2d, is there a quicker way to do this?
« Last Edit: October 12, 2014, 08:29:12 am by Euthyphro »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9708
    • View Profile
Re: Created SpriteCollectionData at runtime, now how to save it to disk?
« Reply #3 on: October 12, 2014, 03:30:46 pm »
I think you missed my point - this has nothing to do with tk2d, you can't serialize anything in Unity at runtime using the built in serialization system, i.e. you can't save a structure that you've populated at runtime, like the tk2dSpriteCollectionData CreateFromTexture creates. The time it takes to CreatefromTexture is surely drawfed by LoadFromPNG, or whatever it is you're using to load the image?

You could try CreateFromTexturePacker and use that path, but thats just a wrapper around CreateFromTexture.