Best Practice for Using Buttons to Access Singletons?

I know the hurdle is well known: Can’t set the UI OnClick() shortcut target singleton objects due to losing connection when loading/reloading into screens. Unfortunately it seems pertinent threads are over a year old, so I want to get a bit of updated info, particularly if my current approach would be considered best practice, and if there are any possible limitations/hurdles my approach may create in the future.

Currently, I’m using a singleton to manage data persistence and game loading (inventively named “Game Controller”).
My buttons each have a script to call the method. Rather than take the public variable (or FindObject) approach and adding listeners, I’ve inherited IPointerEventHandler and call the Game Controller method needed:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class gameButton_Launch : MonoBehaviour, IPointerClickHandler {
    public void OnPointerClick (PointerEventData pointerEventData)
    {
        GameController.Singleton.LoadIntoGame ();
    }
}

If it’s not obvious by the existence of this thread, my experience with Unity is limited at best, but right now, the only downside I see at all is cluttering my asset folder with scripts (though I consider myself a connoisseur of the arts of “creating subdirectories for everything” ^_^)

Are there obvious limitations I’m overlooking, or even hidden hurdles I won’t notice until I end up jumping into large scale projects? Or has a different, newer approach altogether that eliminates the dozen(s) of < 10 line scripts?

That approach works fine and it lets you just drag the script onto the button without having to do anything else. Having a bunch of small scripts isn’t bad; it’s somewhat preferable most of the time. That said, if you want to combine them, you can have a button call anything by using the OnClick section in the button’s inspector; you don’t need to use IPointerClickHandler. You could have a script like this:

public class MyHelperClass : MonoBehaviour {
    public void LoadTheGame() {
        GameController.Singleton.LoadIntoGame();
    }

    public void SaveTheGame() {
         GameController.Singleton.SaveGame();
    }

    public void DoSomeOtherThing() {
         AnotherSingleton.DoTheThing();
    }
}

Attach that to the button or to anything in the scene really, then add an event by clicking the plus button in the OnClick section in the button’s Inspector. Drag the object you attached this script into the field, and then you can select any function within it from the dropdown that you want to call.

4 Likes

Yeah, I normally take @makeshiftwings 's approach. I normally throw a script with all of the one liners on the Canvas of a UI. That way I can find it easily.

3 Likes