1. Home
  2. Docs
  3. UniREST Solution 3.5
  4. UniREST Client
  5. UniREST Binary

UniREST Binary

The UniRESTBinary class is a set of methods for working with binary data and performs various operations with bytes and Binary Strings. Basically, the main scopes of this class are:

  • extracting bytes from different sources like files, Unity Resources files (Textures, Audio Clips, etc.) and data objects;
  • converting a byte array into a simple Binary String (Base64 String) and vice-versa;
  • easily save, load and delete local binary files.

Why bytes?

The bytes are the basic elements constituting any file. Figuratively, they are like atoms that are the basis of matter. From the bytes point of view, all files and objects are the same things: there’s no difference between an image and a 3d model, between a video and an array of strings. Just as bytes can be extracted from files and data, bytes can be reassembled into the original files and data. This means, for example, you can take the bytes from a video, a texture, a 3d model, and a data object, save all in one file and then read that file to get back the 4 original sources. It’s a very flexible format!

Binary Formatter and Odin Serializer

The UniRESTBinary class can use both the standard .NET binary formatter and the Sirenix Odin Serializer. Starting from the 3.0 version, the default serializer is the .NET binary formatter, while the Odin Serializer is still integrated into the UniREST Solution but disabled.

PROSCONS
.NET binary formatterIt’s fast and lightweight. It’s supported by all the Unity platforms.It’s very limited and can’t serialize too complex data.
Odin SerializerIt can serialize practically any kind of data.It’s not light as .NET binary formatter. The WebGL platform requires the configuration of what Odin Serializer can serialize.

Instantiate a UniRESTBinary object

The first thing to do for working with this class features is to declare an instance of the UniRESTBinary class. This will initialize the Binary engine.

var myBinary = new UniRESTBinary(serializerType = SerializerType.BinaryFormatter);
ParameterData-typeDescription
serializerTypeEnum
(optional)
Default BinaryFormatter
Set the serialization technology to use.

OdinSerializer: the Sirenix Odin Serializer offers a sophisticated system ables of serializing practically any kind of data. It’s strictly recommended.

BinaryFormatter: the standard .NET serialization technology. It’s very fast and efficient, but it lacks in the support of many data-type formats.
Returned valueDescription
UniRESTBinary objectA UniRESTBinary instance object.

Load binary data

Binary data can be extracted from different kinds of resources. The UniRESTBinary class has a set of Load* methods which get bytes from specific kind of resources. Once binary data is loaded, you can choose what to do with this data.


LoadFromFile()

The LoadFromFile() method reads bytes from a file. You can read:

  • local files, providing the file name and selecting one of the available system folders;
  • files from the Unity project Resources folders, providing the resource path and name, and selecting the kind of resource (TextAsset, Texture2D, Sprite, AudioClip).
myBinary.LoadFromFile(filePath, sourcePosition = PersistentDataPath, resourceType = None);
filePathstringThe file name or the name of a Resource.
sourcePosition Enum
(optional)
The local file position. PersistentDataPath by default. It can be:

PersistentDataPath
AssetsPath
StreamingAssetsPath
TemporaryCachePath

UserDefined
When selected, a full, complete filePath must be provided.

Resources
When selected, you can read files from the Resources folders. A resourceType parameter must be selected so as to use the right method of reading bytes.
resourceType Enum
(optional)
The type of resource. None by default. It can be:

TextAsset
Texture2D_JPG
Texture2D_PNG
Texture2D_EXR
Texture2D_TGA
Sprite_EXR*
Sprite_JPG*
Sprite_PNG*
Sprite_TGA*
AudioClip

* Bytes are extracted from the Sprite’s Tetxure.
boolThis method just returns true or false.
var myBinary = new UniRESTBinary();

// Read bytes from the "model.fbx" file, located in the PersistentDataPath Game's folder.
var result = myBinary.LoadFromFile("model.fbx");

// Read bytes from the "shoot" audio file, under the "myassets" subfolder,
// located in a Resources Unity Project's folder.
var result = myBinary.LoadFromFile(
      "myassets/shoot", 
      UniRESTBinary.sourcePosition.Resources,                     
      UniRESTBinary.resourceType.AudioClip
);

LoadFromData()

The LoadFromData() method reads bytes from a data object. The data can be anything that can be serialized.

myBinary.LoadFromData(data);
objectobjectThe data to read.
boolThis method just returns true or false.
var myBinary = new UniRESTBinary();

var playerEquip = new List<Equipment>();

// Read bytes from "playerEquip" object.
var result = myBinary.LoadFromData(playerEquip);

LoadFromBytes()

The LoadFromBytes() method loads bytes directly from a bytes array.

myBinary.LoadFromBytes(bytes);
bytesbytes arrayThe bytes to load.
var myBinary = new UniRESTBinary();

// Load bytes from "myFileContent" bytes array.
var result = myBinary.LoadFromBytes(myFileContent);

LoadFromBinaryString()

The LoadFromBinaryString() method reads bytes from a binary string.

myBinary.LoadFromBinaryString(binaryString, decompress = false);
binaryStringobjectThe binary string to read.
decompressbool
(optional)
Default false.
If true, decompress bytes data.
var myBinary = new UniRESTBinary();

// Read bytes from "playerEquip" object.
var result = myBinary.LoadFromBinaryString(myBinaryString);

Convert and save binary data

Once binary data has been loaded, you can perform two kinds of operation:

  • convert it, for example, in a binary string or back into the original data-type;
  • save it in a local file.

ToString()

The ToString() method converts the loaded bytes into a binary string. The advantage is that the returned data is just a string, that is suitable to be saved as an ordinary string (for example, in a Database table).

myBinary.ToString();
stringA Base64 string representation of the loaded bytes.

ToObject()

The ToObject() method converts the loaded bytes or the given binary string back into the original object the bytes were taken from.

myBinary.ToObject();
myBinary.ToObject(binaryString);
TYPEdata-typeThe data-type of the returned object.
binaryStringstring
(override)
The binary string to convert.
objectThe object of the specified data-type.

ToFile()

The ToFile() method saves the loaded bytes or the given binary string into a local file, in the device storage memory.

myBinary.ToFile(filePath, localPosition = PersistentDataPath);
myBinary.ToFile(binaryString, filePath, localPosition = PersistentDataPath);
filePathstringThe file name.
binaryStringstring
(override)
The binary string to convert.
localPosition Enum
(optional)
The local file position. PersistentDataPath by default. It can be:

PersistentDataPath
AssetsPath
StreamingAssetsPath
TemporaryCachePath

UserDefined
When selected, a full, complete filePath must be provided.

Resources
When selected, you can read files from the Resources folders. A resourceType parameter must be selected so as to use the right method of reading bytes.

Compression

Once you have loaded bytes from a resource, you can compress this data in order to reduce its size or decompress this data if it was previously compressed.

myBinary.CompressLoadedBytes();
myBinary.DecompressLoadedBytes();
var myBinary = new UniRESTBinary();

// Read bytes from the "model.fbx" file, located in the PersistentDataPath Game's folder.
var result = myBinary.LoadFromFile("model.fbx");

// If the bytes are correctly loaded from the .fbx file...
if (result)
{
    // ...I compress the loaded bytes so as to reduce the memory size.
    myBinary.CompressLoadedBytes();

    . . .
}

Password protection

Once you have loaded bytes from a resource, you can encrypt or decrypt that data with a password.

myBinary.Encrypt(password);
myBinary.Dencrypt(password);

This methods return an encrypted or decrypted byte array.


Enabling the Odin Serializer

Starting from the 3.0 version, the Odin Serializer is integrated into the UniREST Solution as DLLs and disabled. If you want to use it, you have to enable it.

Go under the Core > OdinSerializer folder and edit the UniRESTOdinConfig.cs file. Here, you will find the UseOdinSerializer property set to false. Set it to true.

In the Types function below there are the definitions of some common data-types. These definitions are used by the AOT system when you are going to use the Odin Serializer with the WebGL platform. Feel free to add new types or comment on unwanted types.

Now, go under the Core folder and edit the UniRESTBinary.cs file. Here, you will find a commented define USE_ODIN_SERIALIZER line. Simply uncomment it.

Now, you can use Odin Serializer as serialization technique.

If you don't want to use the Odin Serializer, you can also delete the Odin Serializer folder!