Search Unity

ScriptableObject Container package

Discussion in 'Assets and Asset Store' started by Peter77, Aug 26, 2023.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    The package is available at https://github.com/pschraut/UnityScriptableObjectContainer

    Introduction

    The ScriptableObject type in Unity is a powerful concept with many different applications.
    However, it lacks a feature that would greatly increase its usefulness for me: the ability to add "Components" to it.

    While Unity allows you to add "Components" to a ScriptableObject asset through code, it does not provide functionality to do so through the Inspector.

    The ScriptableObject Container package aims to solve this issue. It allows you to work with ScriptableObjects in a similar manner to how you work with Components and GameObjects.

    To add a ScriptableObject to a ScriptableObjectContainer, you can use the Inspector, just like adding a Component to a GameObject.

    On the scripting side, you can access the ScriptableObject from the ScriptableObjectContainer in a similar way to how you access a Component from a GameObject.

    upload_2023-8-26_9-50-48.png

    You can think of a ScriptableObjectContainer as "GameObject" and its sub-assets (or objects) would be the Components on a GameObject.


    Example

    FruitContainer.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using Oddworm.Framework;
    3.  
    4. // Inherit a new container type from ScriptableObjectContainer.
    5. // The CreateAssetMenu attribute lets you create the container from the Assets/Create menu.
    6. [UnityEngine.CreateAssetMenu(menuName = "FruitContainer")]
    7. public class FruitContainer : Oddworm.Framework.ScriptableObjectContainer
    8. {
    9. }
    Fruit.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using Oddworm.Framework;
    3.  
    4. // Create a new type that adds itself to the "FruitContainer" Inspector.
    5. // The CreateSubAssetMenu attribute lets you add objects to the container through in the Inspector.
    6. [Oddworm.Framework.CreateSubAssetMenu(typeof(FruitContainer))]
    7. public class Fruit : UnityEngine.ScriptableObject
    8. {
    9.     [SerializeField] int m_HelloWorld = 123;
    10. }
    When you create the
    FruitContainer
    asset via "Assets/Create/FruitContainer" it displays an empty ScriptableObjectContainer in a similar manner as a GameObject without Components.

    Inspector-FruitExample-1.png


    You can then add sub-assets via the "Add Object" button. The menu displays all ScriptableObject that have been added to the container via the
    CreateSubAssetMenu
    attribute.

    Inspector-FruitExample-2.png


    After adding the sub-asset it's shown in the Inspector as demonstrated in the image below.

    Inspector-FruitExample-3.png


    Download

    The package is available at https://github.com/pschraut/UnityScriptableObjectContainer