Faster Saving and Loading using ES2Writer and ES2Reader
If you are looking for extra performance when saving and loading, or extra flexibility in general, then we advise that you use ES2Writer and ES2Reader. This is faster than making multiple ES2.Save and ES2.Load calls because it keeps the file open between calls.
Using ES2Writer and ES2Reader with Tags
Much like ES2.Save and ES2.Load, ES2Writer and ES2Reader can be used with Tags.
Before you start, it should also be noted that you should not open multiple ES2Readers or ES2Writers for the same file as this will cause unexpected behaviour.
Saving Tags using ES2Writer
To save data to a file, you create an ES2Writer using ES2Writer.Create and then call it’s Write methods to save data to the file. When you are finished writing to the file, you must then call the writer’s Save method which stores the data to the file.
Unless you are using the ES2Writer as part of a Using statement, you must call the writer’s Dispose method when you are finished with it.
C#
1 2 3 4 5 6 7 8 9 |
using(ES2Writer writer = ES2Writer.Create("myFile.txt")) { // Write our data to the file. writer.Write(this.name, "nameTag"); writer.Write(this.transform, "transformTag"); writer.Write(new int[]{1,2,3},"intArrayTag"); // Remember to save when we're done. writer.Save(); } |
JS
1 2 3 4 5 6 7 8 |
var writer : ES2Writer = ES2Writer.Create("myFile.txt"); // Write our data to the file. writer.Write(this.name, "nameTag"); writer.Write(this.transform, "transformTag"); writer.Write(new int[]{1,2,3}, "intArrayTag"); // Remember to save and dispose when you're finished. writer.Save(); writer.Dispose(); |
Loading Tags using ES2Reader
To load data from a file, you create an ES2Reader using ES2Reader.Create and then call it’s Read methods to load data from the file.
You may also use the Self-assigning Read methods to load data straight into a Component.
Unless you are using the ES2Reader as part of a Using statement, you must call the reader’s Dispose method when you are finished with it.
C#
1 2 3 4 5 6 7 |
using(ES2Reader reader = ES2Reader.Create("myFile.txt")) { // Read data from the file in any order. reader.Read<Transform>("transformTag", this.transform); this.name = reader.Read<string>("nameTag"); int[] myIntArray = reader.ReadArray<int>("intArrayTag"); } |
JS
1 2 3 4 5 6 7 |
var reader : ES2Reader = ES2Reader.Create("myFile.txt")) // Read data from the file in any order. reader.Read.<Transform>("transformTag", this.transform); this.name = reader.Read.<String>("nameTag"); var myIntArray = reader.ReadArray.<int>("intTag"); // Remember to dispose when we're finished. reader.Dispose(); |
Using ES2Reader and ES2Writer Sequentially
The fastest way to save and load data is to load it in the same order as you saved it, without using tags. This is done in exactly the same way as the previous example, except we don’t specify a tag when saving and loading.
It is very important that you load the data in the same order as you saved it, otherwise you will receive errors at runtime.
Also note that when saving sequentially, we use Save(false) instead of Save() to tell Easy Save that we’re not using tags.
Saving to a File Sequentially
C#
1 2 3 4 5 6 7 8 9 |
using(ES2Writer writer = ES2Writer.Create("myFile.txt")) { // Write our data to the file in the order we are going to read it. writer.Write(this.name); writer.Write(this.transform); writer.Write(new int[]{1,2,3}); // Remember to save when we're done. writer.Save(false); } |
JS
1 2 3 4 5 6 7 8 |
var writer : ES2Writer = ES2Writer.Create("myFile.txt"); // Write our data to the file in the order we are going to read it. writer.Write(this.name); writer.Write(this.transform); writer.Write(new int[]{1,2,3}); // Remember to save and dispose when you're finished. writer.Save(false); writer.Dispose(); |
Loading from a File Sequentially
To load data from a file, you create an ES2Reader using ES2Reader.Create(filepath) and then call it’s Read() methods to load data from the file.
It is important that you load data in the same order that you saved it.
You may also use the Self-assigning Read methods to load data straight into a Component.
Unless you are using the ES2Reader as part of a Using statement, you must call the reader’s Dispose() method when you are finished with it.
C#
1 2 3 4 5 6 7 |
using(ES2Reader reader = ES2Reader.Create("myFile.txt")) { // Read the data from the file in the same order as we saved it. this.name = reader.Read<string>(); reader.Read<Transform>(this.transform); int[] myIntArray = reader.ReadArray<int>(); } |
JS
1 2 3 4 5 6 7 |
var reader : ES2Reader = ES2Reader.Create("myFile.txt")) // Read the data from the file in the same order as we saved it. this.name = reader.Read.<String>(); reader.Read.<Transform>(this.transform); var myIntArray = reader.ReadArray.<int>(); // Remember to dispose when we're finished. reader.Dispose(); |