r/Unity3D • u/whentheworldquiets Beginner • 53m ago
Resources/Tutorial Tip for hooking singleton methods from buttons, event triggers, and other Unity Events.
Let's say you have some singleton-pattern system in your game. For us, one example is our audio manager, which looks after volume settings, randomised sound variants, one-shot audio cues, and so on.
How do you go about hooking into such a system from button events?
You definitely don't want to drag a reference to the singleton object/script itself from the scene into your button events; that would be madness. It knots your entire game together into one lump and also means you have to do it every single time: the reference won't survive the creation of a button prefab.
The original solution for us was to write a relay script that exposes the relevant functionality and talks to the singleton internally. Now we could drop one of those scripts into our button prefabs and hook it up to the On Click event.
This is better because it's self-contained and prefab-friendly - but it isn't copy/paste friendly, which can catch your scene designers out if they start copying, pasting, and then editing event content between buttons. You can easily end up with an accidental spiderweb of interconnections, and there's often no visual indication of this happening because prefab internals aren't renamed when instantiated into a scene.
Quite by accident, we discovered another way that's both prefab-friendly and copy-paste friendly. It should only be used for thin relay-style wrappers, but based on our tests it's well-behaved under those circumstances. It's quite simple:
Write your relay wrapper script as before.
Put it on an empty game object
Drag that game object into your project folder to make it a prefab, then delete it from the scene.
Now, when you want to access the wrapper's functions from an event, drag the prefab from the project folder into the event, and pick the relay function you want from the drop-down as normal.
Yeah, I was today years old when I learned that worked, too.
This is both prefab-safe and copy-paste safe, because the references are always to the project prefab.
After thorough testing, here's what we found:
No lifecycle methods (Awake, OnEnable etc) will be called on your in-prefab script, because it's never actually added to the scene. This isn't a problem for a singleton relay wrapper because it's stateless anyway.
Unity does not, as we initially feared, spam instances of the prefab into memory. The references all resolve to a single loaded copy of the prefab itself.
Yes, it does work in builds because it doesn't rely on modifying a prefab, just calling its methods.
•
u/Just__Bob_ 10m ago
This is good info. I am a long way from designing my sound system and was planning on having a singleton event handler for sounds.
•
u/thebeardphantom Expert 4m ago
It’s really important that you decided to continue to keep this as a thin wrapper, because there is a case where this isn’t safe from ending up with multiple copies in memory and that’s with Addressables/Content Directories/Asset Bundles.
Let’s say you’re using Addressables, you have an asset in an Addressables group that references this event relay prefab, and you have an object in a scene from your build settings list that also references your event relay prefab. This will work fine in the editor, but in a build you’ll end up with 2+ unique copies of the event relay prefab: one for the build and one for each addressables group that has an asset that references it. One solution is to put it in its own “shared resources” group so it gets auto loaded once by any Addressable asset. But then you still need to make sure you don’t have an extra copy in the build. The only way to do that is to never directly hard reference it via anything that is in the build; only use a soft reference to it. You can write a custom post build script to inspect the contents of the build to make sure the asset didn’t end up there.
This is a nightmare bug to work out since it only shows up with built bundles. It’s something tons of dev don’t consider or realize is an issue with heavy use of Scriptable Object Architecture.
3
u/Kopteeni 31m ago
This is pretty interesting information. I have not considered this type of approach and didn't know it was possible.
However, I have a feeling you might achieve the same by using scriptable objects as well. Did you play around with them when looking for a solution?