IEnumerator:在c#中如要要使用yield,那么那个函数形式可以是 IEnumerator myfunction(){ yield;}; 具体是什么意思,不太清楚。
ScriptableObject: derived from Object;
This is most useful for assets which are only meant to store data;
A class you can derive from if you want to create objects that don't need to be attached to game objects.
Serializable:
The Serializable attribute lets you embed a class with sub properties in the inspector.
You can use this to display variables in the inspector similar to how a Vector3 shows up in the inspector.
The name and a triangle to expand its properties. To do this you need create a class that derives from System.
Object and give it the Serializable attribute. In JavaScript the Serializable attribute is implicit and not necessary.
以下来自http://answers.unity3d.com/questions/971/how-to-scrip-a-save-load-game-option;但是ISerializable类我没有找到,难道是版本问题?
The Serializer works like this:这一段的作用是,使用ISerializable类保存用户工程中的状态信息,类似游戏进度的功能;
You create a holder class for everything you want to save and then you mark it with the Serializable attribute:
[Serializable ()]
public class SaveData : ISerializable {
public bool foundGem1 = false;
public float score = 42;
public int levelReached = 3;
public SaveData () {
}
}
Then you have to define functions for how the class should be loaded and how it should be saved, these should be put inside the class.
public SaveData (SerializationInfo info, StreamingContext ctxt)
{
//Get the values from info and assign them to the appropriate properties
foundGem1 = (bool)info.GetValue("foundGem1", typeof(bool));
score = (float)info.GetValue("score", typeof(float));
levelReached = (int)info.GetValue("levelReached", typeof(int));
}
//Serialization function.
public void GetObjectData (SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("foundGem1", (foundGem1));
info.AddValue("score", score);
info.AddValue("levelReached", levelReached);
}
The only thing left now is the save and load functions in your script.
public void Save () {
SaveData data = new SaveData ();
Stream stream = File.Open("MySavedGame.game", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Binder = new VersionDeserializationBinder();
Debug.Log ("Writing Information");
bformatter.Serialize(stream, data);
stream.Close();
}
public void Load () {
SaveData data = new SaveData ();
Stream stream = File.Open("MySavedGame.gamed", FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Binder = new VersionDeserializationBinder();
Debug.Log ("Reading Data");
data = (SaveData)bformatter.Deserialize(stream);
stream.Close();
}
Oh, just one thing more, you need to define a VersionDeserializationBinder class, otherwise you can't load the game when you open it later, something about Unity generates a new key of some sort for the binary formatter, search for "Serialization" on the forum and you can find a post about that.
public sealed class VersionDeserializationBinder : SerializationBinder
{
public override Type BindToType( string assemblyName, string typeName )
{
if ( !string.IsNullOrEmpty( assemblyName ) && !string.IsNullOrEmpty( typeName ) )
{
Type typeToDeserialize = null;
assemblyName = Assembly.GetExecutingAssembly().FullName;
// The following line of code returns the type.
typeToDeserialize = Type.GetType( String.Format( "{0}, {1}", typeName, assemblyName ) );
return typeToDeserialize;
}
return null;
}
}
You will need to use these namespaces to get it working:
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.Runtime.Serialization;
using System.Reflection;