![]() Add the “Demo2” as a component to any Game Object, such as by creating a new empty game object, or even by attaching it to the camera. IMPORTANT – the editor script must be added to an “Editor” folder or it wont work properly. The second button is labeled “Log Values” and will cause each object in the array to print its values to the console window. The first is labeled “Create Data” and will instantiate each of our data subclasses and assign them to the data array of our script. This script will provide some buttons in the inspector of our component. Each object will share a base class – “Demo2Data”, but will actually be instantiated as a subclass. This script will hold an array of objects. You might encounter this problem with a polymorphic list of objects. This demo will show how Unity fails to properly serialize the type of an object. Demo 2 Loss of object type on serialization Uh oh, the two are no longer referencing the same object! Unity has created a full copy of the original object for both fields. Give it a try, then modify the value of “dataB” once again. Still looking good… at least until you enter and exit play mode. If you modify the value field of “dataB”, you should see the value field of “dataA” update to match accordingly. The value for both fields should return to ‘0’ becuase both fields now refer to the same new instance. If you enter and exit play mode, the values will even persist – so far so good.Įxit play mode (if you havent already), then use the “Create Shared Data” button in the inspector. ![]() You can assign any value you want to each of the “Value” fields. Unity will automatically create new instances of “Demo1Data” for both fields simply by looking at the object in the inspector. ![]() ![]() Add the “Demo1” as a component to any Game Object, such as by creating a new empty game object, or even by attaching it to the camera. This script will provide a button in the inspector of our component that will create a new instance of “Demo1Data” and assign it to both fields on the “Demo1” script. If (GUILayout.Button ("Create Shared Data")) It can be serialized, thanks to the “” tag, but Unity won’t handle it perfectly which will be demonstrated soon. This script shows a very simple standard C# class. We will use an editor script to create and assign its values. This script will hold two copies of the same “Demo1Data” instance. Begin by creating a new script called “Demo1” and another called “Demo1Data”, also add an editor script called “Demo1Inspector”: Let’s begin with some serialization examples. Demo 1 Loss of object references on serialization The following two demos show how Scriptable Objects overcome those same issues. The first two demos show how you might run into problems if you weren’t using Scriptable Objects. I have created several mini demos for clarification on these points. The Serialization benefits are not equally applicable to runtime.You can’t create them using normal constructors, but must use “CreateInstance” instead.You must inherit from Scriptable Object, which may break many of your design or architectural options.They don’t need to be attached to Game Objects.They can handle polymorphism, whereas normal classes end up being treated as the base class.This can help you avoid data duplication. They save by reference, whereas normal classes and structs are serialized as full copies.They can survive an assembly reload (such as any time you build your scripts or enter and exit play mode).Of course you may be wondering “why” you would want to use a Scriptable Object. If you have been using tradtional C# classes or structs for simple data-only objects, you could optionally use these instead. You can think of a scriptable object as an object that is meant only for holding data. In this lesson we will cover what they are and how to use them. They have several important benefits but may not work ideally for every scenario. Scriptable Objects are a special type of data object in Unity. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |