Introduction to Scriptable Objects

Проверено с версией:: 5.3

-

Сложность: Базовая

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. We won't cover serialization in depth in this session, but will just touch on the subject and how Scriptable Objects can help us. We will cover not only what Scriptable Objects are, but show some very simple examples using Scriptable Objects. Tutor: Adam Buckner

InventoryItem

Code snippet

using UnityEngine;
using System.Collections;
[System.Serializable] // Our Representation of an InventoryItem
public class InventoryItem
{
public string itemName = "New Item"; // What the item will be called in the inventory
public Texture2D itemIcon = null; // What the item will look like in the inventory
public Rigidbody itemObject = null; // Optional slot for a PreFab to instantiate when discarding
public bool isUnique = false; // Optional checkbox to indicate that there should only be one of these items per game
public bool isIndestructible = false; // Optional checkbox to prevent an item from being destroyed by the player (unimplemented)
public bool isQuestItem = false; // Examples of additional information that could be held in InventoryItem
public bool isStackable = false; // Examples of additional information that could be held in InventoryItem
public bool destroyOnUse = false; // Examples of additional information that could be held in InventoryItem
public float encumbranceValue = 0; // Examples of additional information that could be held in InventoryItem !!!
}

InventoryItemList

Code snippet

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class InventoryItemList : ScriptableObject
{
public List<InventoryItem> itemList;
}