arabera Mike Ton 7 years ago
542
Honelako gehiago
All UnityEngine.Objects can have references to other UnityEngine.Objects
While most Object types are built-in, there are two special types
MonoBehaviour
a wrapper that links to a MonoScript
The MonoScript does not contain any actual executable code.
MonoScript is an internal data type that Unity uses to hold a reference to a specific scripting class within a specific assembly and namespace
ScriptableObject
can be natively serialized and deserialized and manipulated in the Inspector window
provides a convenient system for developers to define their own data types
set of serialized data collectively describing a specific instance of a resource
Objects are subclasses of the UnityEngine.Object base class
any type of resource which the Unity Engine uses
such as a mesh, a sprite, an AudioClip or an AnimationClip
file on disk, stored in the Assets folder of a Unity project
Other Assets need to be processed into native formats, such as FBX files.
Some Assets contain data in formats native to Unity, such as materials.
any given Asset file contains one or more Objects
class Test : MonoBehaviour{ public Trouble t; } [Serializable] class Trouble{ public Trouble t1; public Trouble t2; public Trouble t3; }
class Animal{ public string name; } class MyScript : MonoBehaviour{ public Animal[] animals; }
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.
When it’s deserialized, there are now three different objects.
If you populate the animals array with three references to a single Animal object, in the serializationstream you will find 3 objects.
readonly
const
static
or have [SerializeField] attribute
Loading
Assetbundles also make use of the serialization system.
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, as well as the runtime loading of scenes and assets.
Saving
If you open a .unity scene file with a text editor, and have set unity to “force text serialization”, we run the serializer with a yaml backend.
Resource.GarbageCollectSharedAssets()
This is what makes textures that were used by scene1, get unloaded when you load scene2.
This is our native garbage collector and is different to the C# garbage collector
The native garbage collector runs the serializer in a mode where we use it to have objects report all references to external UnityEngine.Objects.
It is the thing that we run after you load a scene to figure out which things from the previous scene are no longer referenced, so we can unload them
Instantiation
And then we “deserialize” the data onto the new object.
(If the reference is pointing to something “external” (like a texture) we keep that reference as it is, if it is pointing to something “internal” (like a child gameobject), we patch the reference to the corresponding copy).
We then run the same serialization code again in a different variant, where we use it to report which other UnityEngine.Object’s are being referenced. We then check for all referenced UnityEngine.Object’s if they are part of the data being Instantiated().
When you call Instantiate() on either a prefab, or a gameobject that lives in the scene, or on anything else for that matter (everything that derives from UnityEngine.Object can be serialized), we serialize the object, then create a new object
Hot reloading of editor code.
When you change an editor script, we serialize all editor windows
and finally deserialize the datastreams of the windows back onto the new windows
recreate the windows
load the new c# code
unload the old c# code
we then destroy all the windows
(they derive from UnityEngine.Object!),
Prefabs
The concept prefab actually only exists at editor time.
A prefab instance is a list of modifications that should be made on the serialized data for this instance.
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.
Internally, a prefab is the serialized data stream of one (or more) game objects and components
They can contain references to other UnityEngine.Objects and those references get serialized properly.
Serialization happens at the UnityEngine.Object level, each UnityEngine.Object is always serialized as a whole.