Conversation Scriptable Objects
Last updated
Last updated
Scriptable Objects provide an easy way to create and populate custom data elements. In this course, will use scriptable objects to store dialog data and inventory items.
In the image below, we've created a ConversationList scriptableObject, to permanently store date associated with a Conversation.
There are several steps required when creating a new type of scriptable object. Detailed example provided below using our Dialog Manager example:
Define a custom class (or struct) that has variables for each of the items you'll have associated with each scriptable object. This class must be defined using the [System.Serializable]
attribute: See details in full code below.
Class ConversationEntry
[See code below]
Define a custom class that inherits from the Scriptable Object class. See details in full code below.
public class ConversationList : ScriptableObject
[See code below]
Define a collection data-structure, within this class the to hold a collection of the above defined composite elements. This is code within ConversationList class.
public List<ConversationEntry> Conversation
Scriptable Object Factory - Article - Tal Lior
A ScriptableObject is a serializable Unity object that is not attached to a GameObject (like MonoBehaviours are). They are typically used for storing data and they can also be saved as an asset file in you project, just like any other texture or audio clip. Tal Lior
ScriptableObjectFactory Unity Package Download Link - Dropbox
ScriptableObjectFactory Link - Box.com
Any class that inherits from ScriptableObject will now show up as an option in the Project Assets Menu:
The image below shows that the Scriptable Object Factory places a new folder: Editor, with 2 scripts: ScriptableObjectFactory.cs, ScriptableObjectWindow.cs.
Add a public instance of the Scriptable Object to a custom script that will be attached to a gameObject in a scene. In our case this is the DialogManager.cs script.
Use inspector to select a ConversationList scriptableObject from your project's assets, to populate the convList variable defined above.
Scriptable Objects are amazing data containers. They don't need to be attached to a GameObject in a scene. They can be saved as assets in our project. Most often, they are used as assets which are only meant to store data, but can also be used to help serialize objects and can be instantiated in our scenes. Adam Buckner - Official Unity Tutorial
Scriptable Objects - Unity Manual, Scripting API
ScriptableObject is a class that allows you to store large quantities of shared data independent from script instances.
A class you can derive from if you want to create objects that don't need to be attached to game objects. This is most useful for assets which are only meant to store data.
This class
Use as a public object-reference variable in a custom class, like DialogManager, that inherits from MonoBehaviour: The scriptable object instance will be connected in the inspector panel.
To create an instance of a Conversation asset, we right click in the project panel, or right click on the assets folder and select the bottom-most menu option: create -> ScriptableObject, where our custom asset: Conversation, now shows up as an option at the bottom of the menu. After clicking on Conversation, we now have a new item in our Assets panel, we should give it a unique name so we can reference it in our code. I've named the example one: ConversationList_CatScene1
.
The image below shows that the public ConversationList variable: convList has been set in the inspector by selecting the circle-icon to the right of the ConvList item.
Lior Tal has created a Unity package that makes it easy to create scriptable object instances. His website below provides a link to his github account where he provides a download link to the Unity Plugin. I have included these files in the Dialog Unity_Package
Import this Unity Package into a Unity project and you'll be able to create any scriptableObject using the project-panel's context menu.
http://www.tallior.com/unity-scriptableobject-factory/
Resource Links: Scriptableobject-factory - Unity Package