unity学习笔记,关于一些类

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;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值