Let’s free your hands.
- Need MonoBehaviour becomes singleton? ~~~Yep!
- Want a more considerate implementation? ~~~If possible.
- Have to make it from scratch? ~~~Better not.
- Costs your only chance to inherit in C#? ~~~Definitely not!
Overview:
- Full source code and examples included, and for MonoBehaviour in C# only.
- Auto find existing instance, and warning if duplicates found.
- Auto create if none, and ask user if to create when edit mode, without making a ghost silently.
- Not required to derive from any specific class, since C# inheritance is so precious.
The further technical documentation is available here.
And the tutorial is right below.
Coding:
- All you need to do is to declare a property.
- If you wanna do more for safe, check it in Awake().
- Or if you don’t need custom inheritance, it becomes much easier.
- Nothing more to do, just access the singleton and keep creating.
Notifications:
- For preventing mistakes, this’ll notify when things below happen.
- Throw exception when coding incorrectly, see the documentation for detail.
- Throw exception or log error when multiple instances found existing.
- Pop up a dialog to make sure user allow the script to create a new instance in edit mode.
Examples:
- The class BaseSingleton is also the example for Singleton.
- And a simple FramerateCounter is the example for BaseSingleton.
Release:
Singleton Liberation - Inherit from your own MonoBehaviour | Integration | Unity Asset Store
This thread is also for customer support, feel free to leave me a comment if you need.
Please let me know if there’s any question or suggestion.
I’d also forward important messages between the forum and my site to have them visible.
Thanks, enjoy!