Mobile Project

Mobile Project

First Unity project on mobile to learn about Unity mobile development
Project Source Code

Singleton
                    
namespace Code.Tools
{
    public class Singleton : MonoBehaviour where T : Component
    {
        private static T instance;

        public static T Instance
        {
            get
            {
                if (instance) 
                    return instance;

                instance = FindObjectOfType();
                
                if (!instance)
                {
                    return instance = (new GameObject {name = nameof(T), hideFlags = HideFlags.None}).AddComponent();
                }
                return instance;
            }
        }

        private void OnDestroy()
        {
            if (instance == this) { instance = null; }
        }
    }
}


Objectpool
                    
namespace Code.Tools
{
    public class ObjectPool
    {
        private readonly uint m_ExpandBy;
        private readonly GameObject m_Prefab;
        private readonly Transform m_Parent;
        private readonly Stack objects = new();

        public ObjectPool(uint initSize, GameObject prefab, Transform parent = null, uint expandBy = 1)
        {
            m_ExpandBy = expandBy < 1 ? 1 : expandBy;
            m_Prefab = prefab;
            m_Parent = parent;
            Expand(initSize < 1 ? 1 : initSize);
        }

        private void Expand(uint amount)
        {
            for (int i = 0; i < amount; i++)
            {
                var instance = Object.Instantiate(m_Prefab, m_Parent);
                instance.SetActive(false);

                var emitOnDisable = instance.AddComponent();
                emitOnDisable.OnDisableGameObject += UnRent;
                objects.Push(instance);
            }
        }

        private void UnRent(GameObject gameObject)
        {
            objects.Push(gameObject);
        }

        public GameObject Rent(bool activate)
        {
            if (objects.Count == 0)
            {
                Expand(m_ExpandBy);
            }

            var instance = objects.Pop();
            instance = instance != null ? instance : Rent(activate);
            instance.SetActive(activate);
            return instance;
        }
    }
                            
    [ExecuteAlways]
    public class EmitOnDisable : MonoBehaviour
    {
        public event Action OnDisableGameObject;

        private void OnDisable()
        {
            OnDisableGameObject?.Invoke(gameObject);
        }

        public void ClearAction()
        {
            OnDisableGameObject = null;
        }
    }
}