unity学习笔记,关于一些类
2010-03-21 15:49
791 查看
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:
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.
The only thing left now is the save and load functions in your script.
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.
You will need to use these namespaces to get it working:
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;
相关文章推荐
- 关于linux 深度学习环境的一些配置笔记
- unity学习笔记--unity的一些学习资料推荐
- Unity学习笔记 之 关于 Unity UI 的 Slider 的代码记录
- Unity学习笔记 之 关于 Unity UI 的 Slider 的代码记录
- Groovy学习笔记之关于闭包的一些补充
- 学习笔记一:关于directx sdk的安装于一些概念
- 关于学习XHTML的一些笔记
- Unity学习笔记 一些小疑问解答记录
- 关于HTML+css 的一些学习笔记
- 工作学习笔记——一些关于链接的有趣小问题
- 关于三层框架学习中的一些笔记
- 【学习笔记】关于变量类型的一些知识
- 关于this的一些学习笔记
- 一些关于Python的基础知识 - 千月的python linux 系统管理指南学习笔记(3)
- IntelliJ IDEA 学习笔记 - 关于tab的一些设置
- 关于jvm 的一些学习笔记
- Unity 3D 学习笔记(一):关于坐标
- 数据结构学习笔记 --- 线性表 (一些常见的关于链表的算法和面试题)
- 关于Vue.js一些问题和思考学习笔记(2)
- 关于框架的一些学习笔记