Serialized field unity что это

SerializeField

class in UnityEngine

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Description

Force Unity to serialize a private field.

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.

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)
— Enums
— Structs

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.

Источник

SerializeField

class in UnityEngine

Успех!

Благодарим вас за то, что вы помогаете нам улучшить качество документации по Unity. Однако, мы не можем принять любой перевод. Мы проверяем каждый предложенный вами вариант перевода и принимаем его только если он соответствует оригиналу.

Ошибка внесения изменений

По определённым причинам предложенный вами перевод не может быть принят. Пожалуйста попробуйте снова через пару минут. И выражаем вам свою благодарность за то, что вы уделяете время, чтобы улучшить документацию по Unity.

Описание

Force Unity to serialize a private field.

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.

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)
— Enums
— Structs

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.

Источник

Script Serialization

Serialization of “things” is at the very core of Unity. Many of our features build ontop of the serialization system:

Inspector window. The inspector window doesn’t talk to the C# api to figure out what the values of the properties of whatever it is inspecting is. It asks the object to serialize itself, and then displays the serialized data.

Prefabs. Internally, a prefab is the serialized data stream of one (or more) game objects and components. A prefab instance is a list of modifications that should be made on the serialized data for this instance. The concept prefab actually only exists at editor time. The prefab modifications get baked into a normal serialization stream when Unity makes a build, and when that gets instantiated, the instantiated gameobjects have no idea they were a prefab when they lived in the editor.

Loading. Might not seem surprising, but backwards compatible loading is a system that is built on top of serialization as well. In-editor yaml loading uses the serialization system, but also the runtime loading of scenes, assets and assetbundles uses the serialization system.

Now you’d say that none of this very much concerns you, you’re just happy that it works and want to get on with actually creating some content.

Where it will concern you is that we use this same serializer to serialize MonoBehaviour components, which are backed by your scripts. Because of the very high performance requirements that the serializer has, it does not in all cases behave exactly like what a c# developer would expect from a serializer. In this part of the docs we’ll describe how the serializer works, and some best practices on how to make best use of it.

What does a field of my script need to be in order to be serialized?

Which fieldtypes can we serialize?

What are these situations where the serializer behaves differently from what I expect?

Custom classes behave like structs

If you populate the animals array with three references to a single Animal object, in the serialization stream, you will find 3 objects. when it’s deserialized, there are now three different objects. If you need to serialize a complex object graph with references, you cannot rely on Unity’s serializer doing that all automagically for you, and have to do some work to get that object graph serialized yourself. See the example below on how to serialize things Unity doesn’t serialize by itself.

No support for null for custom classes

Pop quiz. How many allocations are made when deserializing a MonoBehaviour that uses this script:

It wouldn’t be strange to expect 1 allocation. That of the Test object. It also wouldn’t be strange to expect 2 allocations. One for the Test Object, one for a Trouble object. The correct answer is 729. The serializer does not support null. If it serializes an object, and a field is null, we just instantiate a new object of that type, and serialize that. Obviously this could lead to infinite cycles, so we have a relatively magical depth limit of 7 levels. At that point we just stop serializing fields that have types of custom classes/structs and lists and arrays.

Since so many of our subsystems build on top of the serialization system, this unexpectedly large serialization stream for the Test monobehaviour will cause all these subsystems to perform more slowly than necessary. When we investigate performance problems in customer projects, almost always do we find this problem. We added a warning for this situation in Unity 4.5.

No support for polymorphism

if you have a public Animal[] animals and you put in an instance of a dog, a cat and a giraffe, after serialization, you will have three instances of Animal.

One way to deal with this limitation is to realize that it only applies to “custom classes”, which get serialized inline. References to other UnityEngine.Objects get serialized as actual references, and for those polymorphism does actually work. You would make a ScriptableObject derived class or another MonoBehaviour derived class, and reference that. The downside of that is that you need to store that monobehaviour or scriptable object somewhere, and cannot serialize it inline nicely.

The reason for these limitations is that one of the core foundations of the serialization system is that the layout of the datastream for an object is known ahead of time, and depends on the types of the fields of the class, instead of what happens to be stored inside the fields.

I want to serialize something that Unity’s serializer doesn’t support. What do I do?

In many cases the best approach is to use serialization callbacks. They allow you to be notified before the serializer reads data from your fields and after it is done writing to them. You can use this to have a different representation of your hard-to-serialize data at runtime than when you actually serialize. You would use these to transform your data into something Unity understands right before unity wants to serialize it, and you use it to transform the serialized form back into the form you like to have your data in at runtime right after unity has written the data to your fields.

Suppose you want to have a tree datastructure. If you let Unity directly serialize the data structure, the “no support for null” limitation would cause your datastream to become very big, leading to performance degratations in many systems:

Instead, you tell Unity not to serialize the tree directly, and you make a seperate field to store the tree in a serialized format, suited for unity’s serializer:

Beware that the serializer, including these callbacks coming from the serializer usually happen not on the main thread, so you are very limited in what you can do in terms of invoking Unity API. You can however to the necessary data transformations do get your data from a non-unity-serializer-friendly format to a unity-serializer-friendly-format.

Script serialization errors

When scripts call the Unity API from constructors or field initializers, or during deserialization (loading), they can trigger errors. This section demonstrates the poor practise that causes these errors.

In Unity 5.4 these errors do not, in most cases, throw a managed exception, and do not interrupt the execution flow of your scripts. This eases the process of upgrading your project to Unity 5.4. However, these errors will throw a managed exception in subsequent releases of Unity. You should therefore fix any errors as soon as possible when upgrading to 5.4.

Calling Unity API from constructor or field initializers

When Unity creates an instance of a MonoBehaviour or ScriptableObject derived class, it calls the default constructor to create the managed object. This happens before entering the main loop, and before the scene has been fully loaded. Field initializers are also called from the default constructor of a managed object. In general, do not call the Unity API from a constructor, as this is unsafe for the majority of the Unity API.

Examples of poor practice:

Both these cases generate the error message: “Find is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call in in Awake or Start instead.”

Calling Unity API during deserialization

When Unity loads a scene, it recreates the managed objects from the saved scene and populates them with the saved values (deserializing). In order to create the managed objects, call the default constructor for the objects. If a field referencing an object is saved (serialized) and the object default constructor calls the Unity API, you will get an error when loading the scene. As with the previous error, it is not yet in the main loop and the scene is not fully loaded. This is considered unsafe for the majority of the Unity API.

Example of poor practice:

This generates the error: “Find is not allowed to be called during serialization, call it from Awake or Start instead.”

Источник

SerializeField

class in UnityEngine

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Submission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Description

Force Unity to serialize a private field.

When Unity serializes your scripts, it only serializes public fields. If you also want Unity to serialize your private fields you can add the SerializeField attribute to those fields.

The serialization system can do the following:

Serializable types

Unity can serialize fields of the following types:

For more information on serialization, see Script Serialization.

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.

Note: If you want to serialize a custom Struct field, you must give the Struct the [System.Serializable] attribute.

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.

Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at issuetracker.unity3d.com.

Copyright ©2021 Unity Technologies. Publication Date: 2021-11-26.

Источник

Русские Блоги

Как понять функцию [SerializeField] сериализации в редакторе Unity

Ранее я не понимал характеристики сериализации и [SerializeField] (если вы не так поняли, пожалуйста, исправьте меня)

Что такое сериализация в первую очередь, Baidu объясняет следующим образом

Сериализация (сериализация) Процесс преобразования информации о состоянии объекта в форму, которая может быть сохранена или передана. Во время сериализации объект записывает свое текущее состояние во временное или постоянное хранилище. Вы можете воссоздать объект позже, прочитав или десериализовав состояние объекта из хранилища.

Понятно, что при воспроизведении игры сохранения сохраненная информация об игроке будет сохранена в файл. Когда вы в следующий раз откроете игру, чтобы прочитать сохранение, вы сможете продолжить играть из сохраненного состояния.

То же самое верно для редактора Unity. Значение открытого публичного свойства отображается на панели свойств. Мы можем произвольно изменить значение (соответствующее значение в скрипте не изменилось). Когда мы закрываем редактор Unity и открываем его снова, значение свойства является тем, которое мы изменили. Значение, а не начальное значение, назначенное в сценарии, указывающее, что значение атрибута «заархивировано»

Serialized field unity что это. f622458373f949f8ba826c0a13e0d227. Serialized field unity что это фото. Serialized field unity что это-f622458373f949f8ba826c0a13e0d227. картинка Serialized field unity что это. картинка f622458373f949f8ba826c0a13e0d227

Когда public int a = 0; создается в скрипте

Мы можем назначить свойство в Инспекторе (например, назначить 2)

Закрываем редактор Unity

При повторном открытии редактора значение a равно 2

Обратите внимание, что редактор Unity сериализировал общедоступные свойства (то есть измененные значения записываются аналогично архивированию)

Принудить единство, чтобы десериализовать частный домен. Это функция сериализации внутреннего единства. Иногда нам нужно сериализовать частное или защищенное свойство.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *