UnityEngine.SerializeField

UnityEngine.SerializeField

  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 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.

  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.
  - CANNOT serialize static fields.
  - CANNOT serialize properties.

  Your field will only serialize if it is of a type that Unity can serialize:

  

Serializable types are:

- All classes inheriting from UnityEngine.Object, for example GameObject, Component, 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
- List of a serializable type (new in 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 List<> for 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.

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.

参考:file://localhost/Applications/Unity/Unity.app/Contents/Documentation/html/en/ScriptReference/SerializeField.html  

System.Serializable

  当一个成员是复合类型时,只要在这个复合类型上标注了[System.Serializable]属性,那这个成员也是一个可编辑的成员。Inspector窗口会按树的形式,依次罗列这个类型中的可编辑成员。

时间: 2024-10-18 03:22:26

UnityEngine.SerializeField的相关文章

Unity 5.x动态加载光照信息(所有坑已踩)

能搜到这的应该是被新的烘焙系统坑了少时间,4.x到5.x美术必须重新烘焙,关于美术的没什么说的,只有---重新烘焙! 新的烘焙系统,为了兼容5.x的多场景编辑功能,将烘焙信息从mesh全部挪到了一个中间件xxx.assets,这个资源文件在5.x烘焙完成后和光照贴图存放在一起,然而关于这个资源文件,我是查来查去没有找到任何接口可以访问. 只能百度谷歌,发现方案基本都是序列化,因为上面提到的烘焙信息没有在mesh中保存,而是一个鸡肋资源文件,发布时xxx.assets可以删了. 序列化哪些数据,在

[UGUI]你说UnityEngine.UI.Button是怎么通过拖动来增加OnClick的监听器的呢?

一.写在前面 按国际惯例,先来讲一讲怎么涉及到这里了.我希望一个UI模块在Awake场景加载之后就可以通过回调来响应服务器消息,但是因为这个模块在场景加载的时候并不会被激活,所以需要将事件的订阅提前到该模块被加载之前,这就让我想起来了UGUI的Button的onClick事件,既然如此那就让我们一起来发现真相把. 二.真相就在这里! 首先呢,UGUI是开源的,要看源码啊,就不得不推荐Resharper了,这个插件有一个Assembly Explorer的东西,可以看到dll中的内容.下面就是Bu

unity HideInInspector与SerializeField

[HideInInspector]表示将原本显示在面板上的序列化值隐藏起来. [SerializeField]表示将原本不会被序列化的私有变量和保护变量可以序列化,这么他们在下次读取时,就是你上次赋值的值. 1如果a是公有的序列化变量. 1.1如果你想要在面板中看到a,那么用: public int a; 1.2如果你不想在面板中看到a,那么用: [HideInInspector] public int a; // 这样a可以在程序中被代码赋值,但不会在面板中看到并手动设置赋值. 2 如果a是私

unity3d 序列化SerializeField

首先,Unity会自动为Public变量做序列化,序列化的意思是说再次读取Unity时序列化的变量是有值的,不需要你再次去赋值,因为它已经被保存下来. 然后是,什么样的值会被显示在面板上? 已经被序列化,但是没有用HideInInspector标记的值. [HideInInspector]表示将原本显示在面板上的序列化值隐藏起来. [SerializeField]表示将原本不会被序列化的私有变量和保护变量可以序列化,这么他们在下次读取时,就是你上次赋值的值. 1如果a是公有的序列化变量. 1.1

Unity3d中UnityEngine.Object

UnityEngine.Object继承自system.Object,是Unity所涉及所有物体的基类. Static Functions 静态函数   下面的都是静态函数 Destroy Removes a gameobject, component or asset.删除一个游戏对象.组件或资源 DestroyImmediate Destroys the object obj immediately. You are strongly recommended to use Destroy i

[Unity 5.2] The imported type `UnityEngine.Advertisements.ShowResult&#39; is defined multiple times

unityAds报这个错: The imported type `UnityEngine.Advertisements.ShowResult' is defined multiple times google了一下,说是由于unit5.2内置了unityAds,不必再从asset store导入unityAds了. 但是如果导入了也可以用,只要将选中Assets/Standard Assets/UnityAds文件夹reimport一下,unity就能知道你是要用导入的unityAds了,就不会

SerializeField等Unity内的小用法

[SerializeField] 在Inspector版面中显示非public属性,并且序列化:若写在public前面,等于没写……(至于序列化是啥,自行脑补……),上图: [NonSerialized]在Inspector版面中隐藏public属性,并且序列化:如果写在非public属性前面,等于没写……,上图: [HideInInspector] 在Inspector版面中隐藏public属性,与上面相比,只是隐藏,没有序不序列化的功能……,上图: [AddComponentMenu("XX

NullReferenceException UnityEngine.Transform.get_localPosition

NullReferenceException  UnityEngine.Transform.get_localPosition unity程序中,需要取得GO自身的Transform,出现如上空异常,这看起来很荒谬. 其实是代码其他函数中,获取对象发生异常,比如在编辑器中没有赋值就使用.出现引用异常. 这就会间接导致程序出现如上异常. unity3d经常因为程序某个函数出现问题,而导致全局脚本出现引用失效. unity3d官方论坛解释. NullReferenceException are th

SerializeField和Serializable

Unity3D 中提供了非常方便的功能可以帮助用户将 成员变量 在Inspector中显示,并且定义Serialize关系.简单的说,在没有自定义Inspector的情况下所有显示在Inspector 中的属性都同时具有Serialize功能.换句话说,就是你在Inspector看到什么,保存游戏的时候,这些值就会被保存成二进制文件. 可被Serialize的变量的定义方法   1. public 变量 在没有加入任何Attribute的前提下,public变量是默认被视为可以被Serializ