Unity3D圣典3.3版本-中文版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
SerializeField 序列化域
# SerializeField 序列化域 Inherits from Attribute Force Unity to serialize a private field. 强制 Unity 去序列化一个私有域。 You will almost never need this. When Unity serializes your scripts, it will only serialize public fields. If in addition to that you also want Unity to serialize one of your private fields you can add the SerializeField attribute to the field. 你几乎从来不需要它。当 Unity 序列化你的脚本的时候,它将紧紧序列化公有域。如果作为附加你也想要 Unity 去序列化你的一个私有域,你可以添加 SerializeField(序列化域)属性给这个域。 Unity will serialize all your script components, reload the new assemblies, and recreate your script components from the serialized verions. This serialization does not happen with .NET's serialization functionality, but with an internal Unity one. Unity 将序列化所有你的脚本组件,重载新的集合,并且重新创建你的脚本组件从这个序列化版本。这个序列化不会发生通过.Net 的序列化功能,而是用一个内部的 Unity 序列化功能。 The serialization system used can do the following: 这个序列化系统使用可以做以下事情: - CAN serialize public nonstatic fields (of serializable types) - 可以序列化公有的非静态域(序列化类型的) - CAN serialize nonpublic nonstatic fields marked with the [SerializeField] attribute. - 可以序列化非公有非静态域用[SerializeField]属性标记。 - CANNOT serialize static fields. - 不可以序列化静态域。 - CANNOT serialize properties. - 不可以序列化属性。 Your field will only serialize if it is of a type that Unity can serialize: 你的域将紧紧序列化如果他是 Unity 可以序列化的一种类型。 Serializable types are: 序列化的类型有: - All classed inheriting from UnityEngine.Object, for example Gameobject, Commponent, MonoBehaviour, Texture2D, AnimationClip.. - All basic data types like int, string, float, bool. - Some built in types like Vector2, Vector3, Vector4, Quaternion, Matrix4x4, Color, Rect, Layermask.. - Arrays of a serializable type - 所有继承自 UnityEngine.Object 的类,例如 GameObject,Component,MonoBehaviour,Texture2D,AnimationClip..- 所有基本类型像 int,string,float,bool.- 一些内建类型像 Vector2,Vector3,Vector4,Quaternion,Matrix4x4,Color,Rect,Layermask..- 一个序列化类型的 Array(数组) - List of a serializable type (new in Unity2.6) - 一个序列化类型的列表(新的在 Unity2.6) - Enums - 枚举。 Headsup note: if you put one element in a list (or array) twice, when the list gets serialized, you'll get two copies of that element, instead of one copy being in the new list twice. 特别注意:如果你放一个元素在一个列表(或者数组)两次,当列表被序列化的时候,你将得到两个这个元素的拷贝,而不是一个拷贝两次在新的列表。 Hint: Unity won't serialize Dictionary, however you could store a Listf<> or keys and a List<> for values, and sew them up in a non serialized dictionary on Awake(). This doesn't solve the problem of when you want to modify the dictionary and have it "saved" back, but it is a handy trick in a lot of other cases. 暗示:Unity 不会序列化 Dictionary(字典),然而你可以存储一个列表给键,一个列表给值,并且缝合他们在一个非序列化 dictionary(字典)在 Awake()。这不会解决当你想要修改 dictionary(字典)和让它的"被保存"返回的问题,但是它在很多其他的情况里是一个便利的窍门。 For UnityScript users: Fields in c# is a script variable in UnityScript, and [SerializeField] becomes @SerializeField. [Serializable] on a class becomes @script Serializable in a UnityScript 给 UnityScript 用户:C#中的域在 UnityScript 中是一个脚本变量,并且[SerializeField]变成 @SerializeField。[Serializable] 在一个类上变成 @script Serializable 在一个 UnityScript。 ``` // Javascript example //This field gets serialized because it is public. //这个域被序列化因为他是公开的。 var name = "John"; //This field does not get serialized because it is private. //这个域没有被序列化因为它是私有的。 private var age = 40; //This field gets serialized even though it is private //这个域被序列化尽管他是私有的。 //because it has the SerializeField attribute applied. //因为它应用了SerializeField属性 @SerializeField private var hasHealthPotion:boolean = true; function Update () { } ``` ``` //C# example using UnityEngine; public class SomePerson : MonoBehaviour { //This field gets serialized because it is public. public string name = "John"; //This field does not get serialized because it is private. private int age = 40; //This field gets serialized even though it is private //because it has the SerializeField attribute applied. [SerializeField] private bool hasHealthPotion = true; void Update () { } } ```
da
2022年5月14日 15:02
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码