Complex information
The asset allows to handle some types of information quite complex, here is how to use them.
Arrays
NOTE
It does not support Generic types.
The asset allows you to save arrays, but all its types are required to be serializable by default.
You can save any type of list, as long as its type is serializable, string is serializable by default, so we are going to save a arrays of strings:
public string[] list = new string[2];
If you want to save an array of some class that you have created you can do this, but the class has to contain the attribute [System.Serializable]
and all its sub types must be Serializable. Example of how to save a custom class:
public MyType[] list = new MyType[2];
[System.Serializable]
public class MyType {
public float randomFloat;
public string randomString;
public MyType(float randomFloat, string randomString) {
this.randomFloat = randomFloat;
this.randomString = randomString;
}
}
Saving Unity objects
Unity contains certain objects that are not serializable but are very useful and are necessary to have them in arrays, such as Vector3
. This is why the asset allows you to save some of the most used types of Unity as an array, the ones that are supported are: Vector2, Vector3, Vector4, Color, Quaternion, Keyframe, GradientAlphaKey.
Code example:
public Vector3[] list = new Vector3[2];
List
The lists are handled just like the Arrays, the only difference is that they are a list, see the documentation of arrays. The same examples but only as lists:
public List<string> list = new List<string>();
public List<MyType> list = new List<MyType>();
[System.Serializable]
public class MyType {
public float randomFloat;
public string randomString;
public MyType(float randomFloat, string randomString) {
this.randomFloat = randomFloat;
this.randomString = randomString;
}
}
public List<Vector3> list = new List<Vector3>();
Dictionary
The asset allows you to save and load dictionary, while all its types are serializable by default, generic types can be used.
Here are some examples that the asset allows to save:
public Dictionary<string, int> Dict = new Dictionary<string, int>();
If you want to save an Dictionary of some class that you have created you can do this, but the class has to contain the attribute [System.Serializable]
and all its sub types must be Serializable. Example of how to save a custom class:
public Dictionary<MyType, int> Dict = new Dictionary<MyType, int>();
[System.Serializable]
public class MyType {
public float randomFloat;
public string randomString;
public MyType(float randomFloat, string randomString) {
this.randomFloat = randomFloat;
this.randomString = randomString;
}
}
Here are some examples using generics that the asset allows to save:
//Generic on the key
public Dictionary<MyGeneric<int, int, string>, int> Dict = new Dictionary<MyGeneric<int, int, string>, int>();
[System.Serializable]
public class MyGeneric<T1, T2, T3> {
public T1 t1;
public T2 t2;
public T3 t3;
public MyGeneric(T1 t1, T2 t2, T3 t3) {
this.t1 = t1;
this.t2 = t2;
this.t3 = t3;
}
}
//Generic on the value
public Dictionary<int, MyGeneric<int, int, string>> Dict = new Dictionary<int, MyGeneric<int, int, string>>();
[System.Serializable]
public class MyGeneric<T1, T2, T3> {
public T1 t1;
public T2 t2;
public T3 t3;
public MyGeneric(T1 t1, T2 t2, T3 t3) {
this.t1 = t1;
this.t2 = t2;
this.t3 = t3;
}
}
Generic type
NOTE
It is not necessary that their types be serializable.
The asset allows to save almost any generic types.
Here you can find more information about generics: Link.
Here are some examples:
public MyGeneric<int> Generic;
public class MyGeneric<T1> {
public T1 t1;
public MyGeneric(T1 t1) {
this.t1 = t1;
}
}
A generic type with multiple parameters:
public MyGeneric<int, string, int> Generic;
It also supports generics that contain other generic:
public MyGeneric<int, string, MyGeneric<Vector3, Color, Quaternion>> Generic;
Components or GameObjects
The asset allows you to save references to other GameObject or Components that are in the scene.
The GameObject is not saved in itself, the reference to a GameObject is saved. For example if you have a script that has a GameObject field, The asset will save like a name (does not save the name itself, save an ID) of the GameObject for when you load the game that variable points to the same GameObject.
Here are some examples of how they are declared in a component:
public GameObject reference;
public BoxCollider reference;
public Transform reference;