【单例模式与对象池】——唐老师

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

/// <summary>
/// --[]--
/// </summary>
public class Test: MonoBehaviour
{
    /// <summary>
    /// ----
    /// </summary>
	void Start ()
    {
        NewBehaviourScript.GetInstance().Test();

    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            PoolManager.GetInstance().Getobj("Prefabs/Cube");
        }
        if (Input.GetMouseButtonDown(1))
        {
            PoolManager.GetInstance().Getobj("Prefabs/Sphere");
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// --[]--
/// </summary>
public class Destroyobj: MonoBehaviour
{
    void OnEnable()
    {
        Invoke("Push",1);
    }
    void Push()
    {
        PoolManager.GetInstance().Pushobj(this.gameObject.name ,this.gameObject);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// --[]--
/// </summary>
public class PoolManager : BaseManager<PoolManager>
{
    //对象池
    public Dictionary<string, List<GameObject>> poolDic = new Dictionary<string, List<GameObject>>();
    /// <summary>
    /// 那对象
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public GameObject Getobj(string name)
    {
        GameObject obj = null;
        if (poolDic.ContainsKey(name) && poolDic[name].Count > 0)
        {
            obj = poolDic[name][0];
            poolDic[name].RemoveAt(0);
        }
        else
        {
            obj = GameObject.Instantiate(Resources.Load<GameObject>(name));
            obj.name = name;
        }
        obj.SetActive(true);
        return obj;
    }

    public void Pushobj(string name, GameObject obj)
    {
        obj.SetActive(false);
        if (poolDic.ContainsKey(name))
        {
            poolDic[name].Add(obj);
        }else
        {
            poolDic.Add(name, new List<GameObject>() {obj});
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// --[]--
/// </summary>
public class BaseManager<T> where T : new()
{
    private static T instance;
    public static T GetInstance()
    {
        if (instance == null)
        {
            instance = new T();
            return instance;
        }
        else { return instance; }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// --[]--
/// </summary>
public class NewBehaviourScript: SingletonAutoMono<NewBehaviourScript>//SingletonMono
{
    #region 
    //protected override void Awake()
    //{
    //    base.Awake();
    //}
    //void Start()
    //{
    //    Debug.Log(NewBehaviourScript.GetInstance().name);
    //}
    #endregion
    public void Test()
    {
        Debug.Log(NewBehaviourScript.GetInstance().name);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// --[]--
/// </summary>
public class SingletonAutoMono<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T instance;
    public static T GetInstance()
    {
        if (instance == null)
        {
            //实例化一个空的对象
            GameObject obj = new GameObject();
            //改变该对象的名字属性,通过反射得到引用。
            obj.name = typeof(T).ToString();
            //切换场景不删除对象
            DontDestroyOnLoad(obj);
            //对该对象添加脚本
            instance = obj.AddComponent<T>();
        }
        return instance;
    }
    protected virtual void Awake()
    {
        instance = this as T;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// --[]--
/// </summary>
public class SingletonMono<T>: MonoBehaviour where T:MonoBehaviour 
{
    private static T instance;
    public static T GetInstance()
    {
        return instance;
    }
    protected virtual void Awake()
    {
        instance = this as T;
    }
}

 

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页