Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

[yaSingleton] ScriptableObject Singletons made easy (Open Source)

Discussion in 'Immediate Mode GUI (IMGUI)' started by jedy, Feb 24, 2019.

  1. jedy

    jedy

    Joined:
    Aug 1, 2010
    Posts:
    578
    Hey all,

    Singletons are by far the most popular design (anti?) pattern I've seen in my years of Unity development. I've also designed them to avoid a few of the popular annoyances that come hand-in-hand with the typical implementation.

    First, here are some of the problems you might often experience while using Singletons in Unity (while using MonoBehaviours as their backbone):
    • Duplicate instances - a leftover instance in one scene can get loaded before our intended copy.
    • Hard to version control - merging scenes is never fun.
    • A "zero" scene - sure, it works, but it adds an extra scene load to every single iteration of the code (the biggest reason I started messing with the Scriptable Objects approach in the first place)
    • Script Execution Order - what runs when and what initializes when can get real messy and waste debugging hours.
    So, trying to resolve all that and the accompanying set of issues that ScriptableObjects bring this library can:
    • Streamline management of Singletons - you create a file anywhere in your project and forget about it.
    • Hassle-free version control - separate files, means small, easy, and few merges. Reverting just your singleton to a week ago is now possible (and extremely handy).
    • No "zero" scene - they initialize before everything else and just work.
    • Unity events - Scriptable Objetcts lack Update calls and application hooks, so I implemented those.
    • Shared Update calls - all your Singletons will share a single Update, LateUpdate, and FixedUpdate call thus reducing their performance footprint.
    • No Resources folder. Everything is added to the preloaded assets automagically.
    • Lazy-loaded (and thread-safe) option.
    • Just works - create your files and reference them as usual.
    I've also included a migration guide and a quick tutorial on how to reference scene objects in the README.

    Here's the repository: yaSingleton

    Thanks for reading that far, any ideas, questions, recommendations, general feedback, or (fingers crossed) amazing pull requests are welcome!

    Stay Awesome,
    - Jordan