Unity3d provides very convenient functions to help usersMember variablesDisplay in Inspector and define the serialize relationship. To put it simply, all attributes displayed in Inspector without Custom inspector have the serialize function at the same time. In other words, what do you see in Inspector? When you save the game, these values will be saved as binary files.
How to define variables that can be serialize
1. Public variable
Without adding any attribute, the public variable is considered serialize by default.
public int MaxExp;
2. [serializefield] attribute
Force unity to serialize a private domain
This is an internal unity serialization function. Sometimes we need a private or protected attribute of serialize. In this case, we can use the attribute [serializefield:
[SerializeField]protected int foobar = 0;
Note:: The defined member variables are also displayed in inspector.
In the latest UI System of unity, add [serializefield] above the UI property, as shown below:
[SerializeField]private Button btn1;
Serializefield reference: http://game.ceeger.com/Script/Attributes/SerializeField.html
3. Separate class or struct
Serializable is the serialization of. net.
Sometimes we will customize some separate class/struct. Because these classes are not derived from monobehavior, they are not identified by unity3d as serialize-enabled structures by default. Naturally, it will not be displayed in inspector. We can add the attribute [system. serializable] to enable unity3d to detect and register these classes as serialize types. The procedure is as follows:
[System.Serializable]public class FooBar { public int foo = 5; public int bar = 10;}
Note:Serializable can only serialize class, struct, Enum, and delegate, But Cannot serialize attributes.
4. scriptableobject
The scriptableobject type is often used to store some unity3d objects that cannot be packaged, such as strings and some class objects. With this type of child type, you can use buildpipeline to package it into an assetbundle package for future use. For details, refer to [CB] scriptableobject serialization.
How to define nonserialize Variables
4.1. Protected, Private, internal variables
By default, the protected, private, and internal variables will not be serialize.
4.2. [system. nonserialized] attribute
Sometimes we need to define some public variables for easy operation, but we do not want them to be retained. In this case, you can use [system. nonserialized] to complete this operation:
[System.NonSerialized] public float foobar = 1.0f;
4.3. readonly, const, static Modifier
If the variable is added with readonly, const, static, and other modifiers, no matter how the serialize is set, the serialize will be performed.
4.4. dictionary <t, k>
Unity3d can serialize the list <t> display, but since their programmers are so lazy or not powerful enough, we can't even serialize dictionary <t, k> This is a common type. Generally, we use serialize to perform a list <t> and initialize the dictionary method in awake to complete the serialize operation of the dictionary. For example:
[System.Serializable]public class NameToID { public string name = ""; public int ID = -1;}
public List<NameToID> nameToIDList = new List<NameToID>();
Dictionary<string,int> nameToID = new Dictionary<string,int>();
//void Awake () { foreach ( NameToID info in nameToIDList ) { nameToID[info.name] = info.ID; } nameToIDList = null; // put it null make garbage collect it (I wish)}
Display in Inspector
In inspector, variables are displayed separately based on uppercase letters of variables, and the first letter is forcibly displayed in uppercase. For example:
public int myFooBar = 0;
In the GUI, it will be displayed as: My Foo bar
Note: The content of this article is reproduced from: http://www.cnblogs.com/oldman/articles/2409523.html
Serializefield and serializable