scriptable objects

10 Ways to Use Scriptable Objects in Unity

Unity Scriptable Objects are an incredibly useful tool in Unity that can help streamline your game development workflow. They allow you to create reusable data structures that can be easily shared across different game objects and scenes. In this blog post, we will explore 10 different ways to use Scriptable Objects in Unity, each with a full code example. By the end of this post, you will have a better understanding of how to leverage the power of Scriptable Objects to enhance your Unity projects.

1. Storing Game Data

Scriptable Objects can be used to store game data such as player stats, item data, and level data. This allows you to easily edit and update this information without having to manually change every instance of it in your game. Here is an example of how to create a Scriptable Object to store player data:

using UnityEngine;

[CreateAssetMenu(fileName = "NewPlayerData", menuName = "Player Data")]
public class PlayerData : ScriptableObject
{
    public int health;
    public int strength;
    public int agility;
    public int intelligence;
}

2. Managing Game Events

Scriptable Objects can also be used to manage game events such as triggering a cutscene or spawning enemies. Here is an example of how to create a Scriptable Object to manage a game event:

using UnityEngine;
using UnityEngine.Events;

[CreateAssetMenu(fileName = "NewGameEvent", menuName = "Game Event")]
public class GameEvent : ScriptableObject
{
    public UnityEvent onTrigger;
    
    public void Trigger()
    {
        onTrigger.Invoke();
    }
}

3. Creating Custom Inspector Tools

Scriptable Objects can be used to create custom inspector tools that can help streamline your game development workflow. Here is an example of how to create a Scriptable Object to manage a custom inspector tool:

using UnityEditor;
using UnityEngine;

public class CustomInspectorTool : MonoBehaviour
{
    public CustomData data;

    // Add custom inspector code here
}

[CustomEditor(typeof(CustomInspectorTool))]
public class CustomInspector : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        CustomInspectorTool tool = (CustomInspectorTool)target;

        // Add inspector code here
    }
}

public class CustomData : ScriptableObject
{
    // Add data here
}

4. Creating Custom Assets

Scriptable Objects can be used to create custom assets such as materials, textures, and meshes. Here is an example of how to create a Scriptable Object to manage a custom material:

using UnityEngine;

[CreateAssetMenu(fileName = "NewMaterial", menuName = "Material")]
public class MaterialAsset : ScriptableObject
{
    public Material material;
}

5. Creating Global Managers

Scriptable Objects can be used to create global managers that can be accessed from anywhere in your game. This includes things like an AudioManager, a SaveManager, and a GameManager. Here is an example of how to create a Scriptable Object to manage global game settings:

using UnityEngine;

[CreateAssetMenu(fileName = "NewGlobalSettings", menuName = "Global Settings")]
public class GlobalSettings : ScriptableObject
{
    public bool soundEnabled;
    public bool musicEnabled;
    public int currentLevel;
}

6. Creating Custom Audio Controllers

Scriptable Objects can be used to create custom audio controllers that can help manage audio in your game. Here is an example of how to create a Scriptable Object to manage audio:

using UnityEngine;

[CreateAssetMenu(fileName = "NewAudioController", menuName = "Audio Controller")]
public class AudioController : ScriptableObject
{
   public AudioClip audioClip;
   public float volume;
   public bool loop;

   public void Play()
   {
      AudioSource.PlayClipAtPoint(audioClip, Vector3.zero, volume);
   }
}

7. Managing Object Pools

Scriptable Objects can be used to manage object pools in your game. This can help improve performance by reducing the number of times you need to instantiate and destroy objects. Here is an example of how to create a Scriptable Object to manage an object pool:

using UnityEngine;

[CreateAssetMenu(fileName = "NewObjectPool", menuName = "Object Pool")]
public class ObjectPool : ScriptableObject
{
   public GameObject prefab;
   public int poolSize;
   public bool autoExpand;

   private List<GameObject> pool;

   public void Init()
   {
      pool = new List<GameObject>();

      for (int i = 0; i < poolSize; i++)
      {
         GameObject obj = Instantiate(prefab);
         obj.SetActive(false);
         pool.Add(obj);
      }
   }

   public GameObject GetObject()
   {
      for (int i = 0; i < pool.Count; i++)
      {
         if (!pool[i].activeInHierarchy)
         {
            return pool[i];
         }
      }

      if (autoExpand)
      {
        GameObject obj = Instantiate(prefab);
        pool.Add(obj);
        return obj;
      }

      return null;
   }
}

8. Creating Custom Input Systems

Scriptable Objects can be used to create custom input systems that can help manage player input in your game. Here is an example of how to create a Scriptable Object to manage player input:

using UnityEngine;

[CreateAssetMenu(fileName = "NewInputManager", menuName = "Input Manager")]
public class InputManager : ScriptableObject
{
   public float horizontal;
   public float vertical;
   public bool jump;

   public void UpdateInput()
   {
      horizontal = Input.GetAxis("Horizontal");
      vertical = Input.GetAxis("Vertical");
      jump = Input.GetButtonDown("Jump");
   }
}

9. Creating a Dialogue System

Scriptable Objects can be used to create a dialogue system for your game, allowing you to easily manage conversations between characters. Here’s an example of how to create a Scriptable Object to manage a dialogue:

using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "NewDialogue", menuName = "Dialogue")]
public class Dialogue : ScriptableObject
{
    [System.Serializable]
    public class Conversation
    {
        public string speaker;
        [TextArea(3, 10)]
        public string[] lines;
    }

    public Conversation[] conversations;
}

10. Creating Custom Debugging Tools

Scriptable Objects can be used to create custom debugging tools that can help you debug your game. This includes things like a DebugLogManager and a DebugPanelManager. Here is an example of how to create a Scriptable Object to manage a DebugLog:

using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "NewDebugLog", menuName = "Debug Log")]
public class DebugLog : ScriptableObject
{
    private List<string> logs = new List<string>();

    public void AddLog(string log)
    {
        logs.Add(log);
    }

    public void ClearLogs()
    {
        logs.Clear();
    }

    public List<string> GetLogs()
    {
        return logs;
    }
}

In conclusion, Scriptable Objects are a powerful tool in Unity that can help streamline your game development workflow. There are many different ways to use Scriptable Objects, from managing game data and events to creating custom editor windows and animation controllers. Hopefully, this blog post has given you some ideas for how you can use Scriptable Objects in your own Unity projects.

Leave a Comment