- Home /
Write/Read binary file
Hello everybody. I geting problem with read binary file, this my code:
class herb_pos { var x:float; var y:float; var z:float; }
var herbs : GameObject[]; var load_herbs=new Array(); var herb_proto : GameObject;
function Write() { var w: herb_pos = new herb_pos(); herbs = GameObject.FindGameObjectsWithTag("herb"); var fs: FileStream = new FileStream("herb.sbin", FileMode.Create); var bf: BinaryFormatter = new BinaryFormatter(); var herb_count:int=herbs.length; bf.Serialize(fs, herb_count); for (var herb : GameObject in herbs) { w.x=herb.transform.position.x; w.y=herb.transform.position.y; w.z=herb.transform.position.z; bf.Serialize(fs, w); } fs.Close(); }
function Read() {
var vf: IFormatter = new BinaryFormatter(); var fs = new FileStream("herb.sbin", FileMode.Open, FileAccess.Read); var herb_count:int=vf.Deserialize(fs); for (var i=0;i<herb_count;i++) { var w1: herb_pos= vf.Deserialize(fs); load_herbs.Add(Instantiate (herb_proto, Vector3(w1.x,w1.y,w1.z), herb_proto.transform.rotation)); load_herbs[herb_count].name="herb"+herb_count; } fs.Close(); }
If I am run Read function - creat nothing. But need creat herbs whith load coordinates. P.S. File created, if I use Write function. Where my error? All thanks.
Answer by cupsster · Oct 30, 2011 at 10:33 PM
try to make binary file with .txt at the end instead of .sbin, should help