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.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

[FREE] Singleton Liberation - Inherit from your own MonoBehaviour

Discussion in 'Assets and Asset Store' started by Wanzyee, Feb 19, 2016.

  1. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135



    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:

    http://u3d.as/nYJ


    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!
     
    Last edited: Oct 14, 2018
    mgear likes this.
  2. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135
    New version 1.0.1 is currently "Published".

    - Fix usage of Object.DontDestroyOnLoad() since it only applies to the root object.
    - Fix the FramerateCounter's API to allow access while editing.
     
  3. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135
    Hi guys, I decide to release this for free.
    It's currently "Pending Review", just wait for few days... LOL
    Thanks for your support~~
     
  4. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135
    New version 1.0.3 is currently "Published".

    - Change to check accessing class in editor only, since it's for avoiding programming mistakes.
    - The change above may fix some issue when building for iOS.
     
  5. tcmeric

    tcmeric

    Joined:
    Dec 21, 2016
    Posts:
    190
    Great, thanks for sharing this!
     
  6. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135
    New version 1.0.4 is currently "Pblished".

    - Provide assembly definition files, please be aware the script files were moved therefore.
    - Note, it can't identify objects belong to DontDestroyOnLoad or AssetBundle until Unity 2017.
     
    Last edited: Oct 15, 2018