Giter VIP home page Giter VIP logo

unitysingleton's People

Contributors

fcmz avatar hasanbayatme avatar nosloofah avatar starryforest-ymxk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unitysingleton's Issues

Awake DestroyObject with condition?

  protected virtual void Awake()
    {
        if (instance == null)
        {
            instance = this as T;
            DontDestroyOnLoad(gameObject);
        }
        else if (instance != this)
        {
            //If there is already an instance of this class, destroy the object we just created.
            Debug.LogWarning("Attempted to spawn more then one singleton object. destroying new instance of " + gameObject.ToString() + "\nIf you would like more than one instance, ensure that the class you have written does not Inherit from a Singleton class.");
            Destroy(gameObject);
        }
    }

I found another snippet from Naphier which the instance!=this make sense when destroy it.

Could you please explain it why you didn't add condition? Thanks.

Add UPM support

Add Unity Package Manager setup instructions and include package.json file.

Scene Singleton Pattern

In some cases we should use this pattern, it differs from the traditional "Global singleton pattern", the purpose of "Scene Singleton Pattern" is to ensure that there is only one instance of the class in a given scene, but it does not require that instance to persist across scenes. This pattern is useful when you need to manage certain assets or features in a single scene, but you don't want those assets or features to remain present when the scene is switched.

Great job, could even be extended to support editor mode

Thanks for the code, I believe it could be further enhanced to support editor mode, like the following:

using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
using UnityEngine;

namespace Z
{
    [PublicAPI]
    [ExecuteAlways]
    public abstract class Singleton<T> : MonoBehaviour where T : Component
    {
        private static T _instance;

        public static T Instance
        {
            get
            {
                if (_instance != null)
                    return _instance;

                _instance = FindObjectOfType<T>();

                if (_instance != null)
                    return _instance;

                var o = new GameObject($"{typeof(T).Name} (Singleton)");

                _instance = o.AddComponent<T>();

                return _instance;
            }
        }

        [SuppressMessage("ReSharper", "VirtualMemberNeverOverridden.Global")]
        protected virtual void Awake()
        {
            if (_instance == null)
            {
                _instance = this as T;

                if (Application.isPlaying)
                {
                    DontDestroyOnLoad(gameObject);
                }
            }
            else
            {
                if (Application.isPlaying)
                {
                    Destroy(gameObject);
                }
                else
                {
                    DestroyImmediate(gameObject);
                }
            }
        }
    }
}

Not PR-ing, just a suggestion :)

Wouldn't it be nice to "seal" the Awake function?

protected virtual void Awake() {
    if (_instance == null) {
        _instance = this as T;
        DontDestroyOnLoad(gameObject);
    }
    else {
        Destroy(gameObject);
    }
}

The Awake() function is the key part to make the derived singleton class persistent across scenes and prevent duplicates, but it is marked as protected virtual, so that one could accidentally override it but forgot to add the base.Awake() call, which would then break things apart, and it's hard to debug...

I think maybe we can improve by doing sth like this?

private void Awake() {
    if (_instance == null) {
        _instance = this as T;
        DontDestroyOnLoad(gameObject);
        Initialize();
    }
    else {
        Destroy(gameObject);
    }
}

protected virtual void Initialize() { }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.