CS2335
Master_v2
Master_v2
  • Introduction
  • Introduction
    • Introduction
      • Design
      • Game Design
    • Unity - Download
    • Visual Studio - IDE
    • Hero's Journey
  • Unity Basics
    • Unity Editor Windows
    • MonoBehavior - Base-Class
    • Unity Engine - Event Functions
  • Getting Started
    • UI-Elements
    • Animator Controller
      • Animation Steps
    • PlayerController Flow Chart
    • PlayerController Code
      • PlayerController - V1 - S20
      • PlayerController V2 S20
      • PlayerController V3 S20
  • Project 1 - Simple Game
    • Overview
    • Project 1 - Get Started
      • UML Class Diagram
    • Player GameObject
      • PlayerController.cs V2
      • PlayerController.cs V3
    • Create 2D Sprite Prefab: Rock
    • Sorting Layers
  • Project1 Code
    • PickUp PreFabs
    • Player GameObject
    • PlayerController - jump
    • GameData Version1
    • PlayerStats Version1
    • MiniGameManager
      • Logic Diagram
    • Simple Spawner
    • Utility Class
  • Project1 Enhancements
    • PickUp - SelfDestruct
    • Spawn from List of Prefabs
  • Project 2 - StateManager
    • Project 2 - Learning Objectives
    • Project 2 - Starter Assets
    • Project 2
      • State Machine Framework
        • Singleton Pattern
      • StateManager - Singleton Design Pattern
      • IStateBase, BeginState
      • Project 2 -Steps: Create new Scene and State
      • Project 2 - List of Steps
        • Project 2 - Starter Code
  • Project 2 -Dialog
    • Hide_Show_Panel Script
    • Configure TitlePanel, DecisionPanel
    • Simple Dialog Prefab
    • Conversation Scriptable Objects
    • DialogManager_ConvList
    • Image Transitions for Buttons
  • UI Components
    • Finding Game Objects
    • Game Objects: UI vs. 2D Sprite
    • UI Elements
      • Canvas: Screen-Space Render-Mode
      • UI-Buttons To Change Scene
      • Text Input
  • Project2 Resources
    • Visual Novel in Unity-Links
    • Scriptable Object Factory
      • ScriptableObjects
    • Dialog Prefab Packages
  • Project 3 - Overview
    • Branching Story Structures
    • Dictionary Data-Structure
      • Unity PlayerPrefs Dictionary
    • Dictionary: User-Choice Data
      • User-Choices - Example
        • Dictionary Value to Disable Options
    • Simplified Mini-Game
      • PlayerController_v2 Mods
        • PlayerController_v2_final
      • MiniGameManager_v2
  • Proj3: Inventory System
    • Inventory-System
      • Install and Configure
      • Diagrams, Resources
        • Item, Gem, Potion Classes
        • Inventory Class
      • InventoryDisplay, Slot UI
        • InventoryDisplay Class
        • Slot Class
        • Hazard Class
        • Layout Groups
      • Customization Steps
        • Configure Animation
        • AddItem Button
        • Concrete Class: Food
        • MiniGame Mods
          • PlayerController Mods
      • Code: InventorySystem
        • GameData, PickUp Mods
      • Resources: Data Structures
  • Proj3: Custom UnityEvents
    • Event Publishing Patterns
    • Custom Event Messaging
  • Proj3: Mini-Game
    • MiniGame-Overview-Proj3
    • LevelManager
      • LevelManager Logic Diagram
      • LevelManager FSM
      • LoadLevel, StartLevel Logic
      • Code Framework
    • Timer
  • Project 3 - Code Mods
    • Project 3 - Steps
    • Project 3 - Code
      • Code: Final Versions
        • PlayerController Mods
          • PlayerController_v2 Mods
        • GameData - Final
        • LevelManager
        • PlayerStats - Final
        • PickUp, Hazard, ScorePickUp
        • Spawner - Final
        • CameraFollow
        • ScreenFader
        • MiniGameState
        • Example: EndState
      • MiniGameWin Logic
  • Optional, Supplemental Content
    • Optional Content
      • Adding Audio
      • Screen Fading and Reloading
      • ScriptableObjects
      • Disable Debug Logging
      • Events and Actions
      • Saving Data - Serialization
      • Parallax Scrolling
      • Change Sprites
  • C# Language
    • C# Language
      • Variables
      • Enum
      • Encapsulation
        • C# Properties
        • Access Modifiers
      • Inheritance
      • Polymorphism
      • Interface
      • Switch-Case
      • List< T >
      • Queue< T >
      • Dictionary
      • Foreach
      • Static
      • Ternary Operator: ?
      • this
      • Delegates
    • Diagrams
      • State Machine Framework
      • UML Class Diagrams
      • Level Manager Logic Diagram
      • Flow-Chart: NumberGame
      • FSM: NumberGame
    • Glossary
    • References and Resources
    • Random Thoughts
Powered by GitBook
On this page
  • Conversation Scriptable Objects
  • Install the Scriptable Object Factory - Unity Package
  • Class ConversationEntry
  • Class ConversationList
  • Using the ConversationList
  • Use in DialogManager Class
  • Create a ConversationList Asset
  • Select ScriptableObject Instance in Inspector
  • Download Scriptable Object Factory Unity Package

Was this helpful?

  1. Project 2 -Dialog

Conversation Scriptable Objects

PreviousSimple Dialog PrefabNextDialogManager_ConvList

Last updated 5 years ago

Was this helpful?

Conversation Scriptable Objects

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.

  • Define a custom class that inherits from the Scriptable Object class. See details in full 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

Install the Scriptable Object Factory - Unity Package

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

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.

    public class DialogManager : MonoBehaviour {
    
       public ConversationList convList; //attach scriptable object in Inspector
    
       //more class definition code below
  • Use inspector to select a ConversationList scriptableObject from your project's assets, to populate the convList variable defined above.

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.

Class ConversationEntry

using UnityEngine;
using System.Collections;

[System.Serializable]  //attribute so this custom class can be displayed as an item in the inspector
public class ConversationEntry  {

    public string SpeakingCharacterName;
    public string ConversationText;
    public Sprite DisplayImg;
}

Class ConversationList

This class

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ConversationList : ScriptableObject
{
    public List<ConversationEntry> Conversation;
}

Using the ConversationList

Use in DialogManager 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.

public class DialogManager : MonoBehaviour {

     public ConversationList convList; //attach scriptable object in Inspector

     //more class definition code below

Create a ConversationList Asset

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.

Select ScriptableObject Instance in Inspector

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.

Download Scriptable Object Factory Unity Package

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.

Class ConversationEntry

public class ConversationList : ScriptableObject

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.

Resource Links:

[See code below]
[See code below]
Scriptable Object Factory - Article - Tal Lior
ScriptableObjectFactory Unity Package Download Link - Dropbox
ScriptableObjectFactory Link - Box.com
Adam Buckner - Official Unity Tutorial
Scriptable Objects - Unity Manual, Scripting API
http://www.tallior.com/unity-scriptableobject-factory/
Scriptableobject-factory - Unity Package
Scriptable Object Article