for (int i = 0; i < 5000000; i++)
{
int x = Random.Range(0, 11);
int y = Random.Range(0, 11);
int b = Random.Range(0, 4);
mylist.Add(x);
mylist.Add(y);
mylist.Add(b);
}
List<int> Load3()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = new FileStream(Application.persistentDataPath + "/bf.txt", FileMode.Open);
return bf.Deserialize(file) as List<int>;
}
this is my code for saving and loading data to unity SOOOO.
how do i compress it because in this test currently it takes 65MB for simple data.
lzf is for strings so no use.
A single integer is 32-bits of data, which is 4-bytes of data. You’re creating 15 MILLION of them to save into a file. That’s 60MB by itself, yes. You can “compress it” by A: not using such a massive number of values and B: using int16s instead of the normal int32s will cut the space needed in half, and using int8s instead will cut it down to 1/4th. ^_~
As to actual, real data-compression check this out. I doubt you’ll find anything specifically written for Unity though, if that’s what you wanted. Have fun!
Only if it’s unsigned (positive numbers only), which is called “byte” btw (the signed version is “sbyte”). If you wanted to go for int16, you should be sure to use “ushort”, the unsigned version with only 0 and positive numbers, which has a range from 0 to like 65,500-something.
I have no idea if sharpziplib works well with an Android build, but I can’t really imagine why it wouldn’t.
I thought because of librariys and stuff which i dont have much knoweledge of.
Yes, i googled and find everything about int types, BUT if i make x byte, it doesn’t matter because biggest is y ushort and thus i have to save list as ushort and still takes the same amount of place.
Can i do something like: create 3 lists and then save bf.Serialize(file, mylist1, mylist2, mylist3); because i dont want to have 3 save files for 3 different ints and how would i deserialize them later anyway?
Saving the 3 lists in 3 different files will take up the exact same amount of space, so I don’t see the point. “ushort” does not take the same amount of space as an int32 (which is what everything is now)- it’s half the space.
This does not appear to be the kind of problem you should really be nit-picking little space-saving measures like this anyways (google “premature optimization”)- even moreso because the simplest and best solution is to not save 15,000,000 integer values. coughs If you really need that many integer values to be saved to a file in a mobile game, you’ve got far larger problems than the space being consumed by the save-file IMO. You’re looking for a solution that has no problem.
Well this stores x,y and type of block at that location. since it can’t save multidimensional array i dont see other way of doing so, i will probably have about 1,000,000x3 not 15 mil anyway.
this way i load and assign values to like world[1,20] = 2 at start of game and save when needed to file. 1 thing left for me is sharplib which may save quite alot of space i think, because x is byte and y ushort so x can have lots of free space that can be compressed
Why store the x and y coordinates? You can save a lot of space by not storing the x and y coordinates. Just store every single block in order. This is essentially a grey scale image. Then you can run it through whatever image compression algorithm takes your fancy.
@Kiwasi You mean like make gray scailng to store type of block, compress it with some algorithm and then save with bit serializer? can i do that to text file?
this seems like nice way but want to save to textfile with binary formatter
You’re getting REALLY caught up on something that makes no difference at all- user interactability with your data storage methods. If you don’t want them to edit it, change the type of file that it LOOKS like as obfuscation, but load it as a standard image format. Unless you think they’re going to have such intimate knowledge of your program that’ll see right through that? If you’re only trying to protect it from the most casual of script-kiddies, then you don’t need to go that far out of your way.
Btw, the concept that there’s a community out there just waiting to try and hack your data is extremely arrogant, and you need to keep in mind that no matter what format you save it in, it’s still going to be able to be edited and people can make changes with “trial and error” in mind very easily regardless. I could also go in and change the values of data during run-time with a memory editor, and there’s absolutely nothing you can do.
If you’re that worried, encrypt the data I suppose (REALLY encrypt it, with an actual encryption algorithm). You can easily have the data handled like a completely normal image and encrypt the hell out of it only as it’s being saved. Then later you can decrypt it on load and treat the data like a completely normal image again in processing. Any design decisions that you make with “what’s going to keep this from getting hacked?” is focusing on entirely the wrong thing, because you can’t.
using(var w = new BinaryWriter(File.OpenWrite("test.bin")))
{
//PHO header for Phoda?
w.Write('P');
w.Write('H');
w.Write('O');
w.Write('0');
//WRITE NUM POSITIONS
w.Write((Int32)myList.Length)
//WRITE POSITIONS
for(int i=0; i < myList.Length; i += 3){
w.Write((Int32)myList[i]);
w.Write((Int32)myList[i+1]);
w.Write((Int32)myList[i+2]);
//Write additional things?
//w.Write((byte)0); //block type or something if needed eventually
}
}
using(var r = new BinaryReader(File.OpenRead("test.bin")))
{
//PHO header for Phoda?
int header = r.ReadInt32(); //PHO0 in binary is 810502224
if(header == 810502224){
//valid PHO file
int positionCount = r.ReadInt32();
for(int i=0; i < positionCount; i++){
int x = r.ReadInt32();
int y = r.ReadInt32();
int z = r.ReadInt32();
//Read additional things?
//byte b = r.ReadByte();
}
}else{
//invalid
throw new System.Exception("Invalid or corrupt PHO file!");
}
}