Skip to content

Instantly share code, notes, and snippets.

@openroomxyz
Last active April 10, 2020 11:37
Show Gist options
  • Save openroomxyz/6fe5c8a41316052341c493956f2aaec4 to your computer and use it in GitHub Desktop.
Save openroomxyz/6fe5c8a41316052341c493956f2aaec4 to your computer and use it in GitHub Desktop.
Unity : How to read Binary File Until The End of it (size not written inside file )?
private void ReadBinFile(string path, System.Action<Vector3, float> f)
{
if (!File.Exists(path)) //if file does not exist we will not try reading it
{
return;
}
BinaryReader br;
//reading from the file
try
{
br = new BinaryReader(new FileStream(path, FileMode.Open));
}
catch (IOException e)
{
Debug.Log(e.Message + "\n Cannot open file.");
return;
}
try
{
while (br.BaseStream.Position != br.BaseStream.Length)
{
float x = br.ReadSingle();
float y = br.ReadSingle();
float z = br.ReadSingle();
float radij = br.ReadSingle();
int second = br.ReadInt32();
f(new Vector3(x, y, z), radij);
}
}
catch (IOException e)
{
Debug.Log(e.Message + "\n Cannot read from file.");
return;
}
br.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment