Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

[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:
    579
    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
     
  2. InitusInteractive

    InitusInteractive

    Joined:
    Aug 16, 2018
    Posts:
    124
    I was implementing yaS, it was working before but not it seems it has stopped.

    Unity Version : 2019.3.0f3
    Latest Version of yaS.

    I get 1 warning and 1 info message

    Warning.
    Disabled

    Info
    All MasterSceneLoader (Singleton) instances must be in the Resource folder. Otherwise they can't be found by the engine.

    The thing is, the code does exist in the Resource folder under "Singletons"

    As I mentioned, it was working but it has stopped, likely due to a Uniyy update?

    Please help.
     
  3. InitusInteractive

    InitusInteractive

    Joined:
    Aug 16, 2018
    Posts:
    124
    Ok this seems to have resolved the error messages. I added the altSeparatorChar to check as well.

    I am on Windows 10 and it was looking for \\ and not /, so I included both checks.

    Otherwise, it was still working as expected, false alarm.


    protected void DrawSingletonValidation() {
    if(!isSingletonEditor) {
    return;
    }

    var path = AssetDatabase.GetAssetPath(target);
    var separator = System.IO.Path.DirectorySeparatorChar;
    var altSeparator = System.IO.Path.AltDirectorySeparatorChar;

    if (!string.IsNullOrEmpty(path) && (!path.Contains(separator + "Resources" + separator) && !path.Contains(altSeparator + "Resources" + altSeparator))) {
    EditorGUILayout.HelpBox("Disabled.", MessageType.Warning, true);
    EditorGUILayout.HelpBox(
    "All " + target.GetType().Name +
    " (Singleton) instances must be in the Resources folder. Otherwise they can't be found by the engine.",
    MessageType.Info, true);
    }
    }
     
  4. jedy

    jedy

    Joined:
    Aug 1, 2010
    Posts:
    579
    This shouldn't be necessary - I've removed the need to use the Resources folder in the later versions. Did you clone the master branch from GitHub?
     
  5. InitusInteractive

    InitusInteractive

    Joined:
    Aug 16, 2018
    Posts:
    124
    i used the version from the Asset Store