using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InstanceBase<T> : BaseObject where T : BaseObject, new()
{
    public static T _instance;
    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new T();
            }
            return _instance;
        }
    }

    protected static NewT Reinstance<NewT>() where NewT : BaseObject, new()
    {
        if (_instance is NewT)
        {

        }
        else
        {
            if (_instance != null)
                _instance.DoDestroy();

            _instance = new NewT() as T;
        }

        return _instance as NewT;
    }

    public InstanceBase()
    {
        if (this is DIDAMain)
        {
        }
        else
        {
#if UNITY_EDITOR
            if (DIDAMain.Instance.nextFrameInstanceAdd == null)
            {
                DIDAMain.Instance.Init();
            }
#endif
            DIDAMain.Instance.AddInstanceNextFrame(this);
        }
    }

    public override void DoDestroy()
    {
        DIDAMain.Instance.RemoveInstanceNextFrame(this);

        _instance = null;
    }
}
