天天看點

01.基礎架構單利和對象池

01.基礎架構單利和對象池

01.單利模式基類

using System.Collections;
using System.Collections.Generic;
/// <summary>
///不繼承mono的單利基類
/// </summary>
/// <typeparam name="T"></typeparam>
public class BaseManager<T> where T: class,new()
{

    private static T _instance;

    public static T GetInstance
    {
        get {
            if (_instance==null)
            {
                _instance=new T();
            }

            return _instance;
        }
    }

}      
/// <summary>
/// 繼承mono的單利基類
/// </summary>
/// <typeparam name="T"></typeparam>
public class BaseManagerMono<T> : MonoBehaviour where T:MonoBehaviour
{
    private static T _instance;

    public static T GetInstance
    {
        get
        {
            if (_instance==null)
            {
                _instance = GameObject.FindObjectOfType<T>();
            }

            return _instance;
        }
    }
}      

02.緩存池子產品

01.基礎架構單利和對象池
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum ObjType
{
    Bullet,
    Fire,
    Hole,
}

public class Pool
{
    private Transform parent;
    private ObjType type;
    private Queue<Transform> queues;
    private Type[] types;
    public Pool(string poolName,Transform parent,ObjType type,params Type[] types)
    {
        GameObject game = new GameObject(poolName);
        game.transform.SetParent(parent);
        this.parent = game.transform;
        queues = new Queue<Transform>();
        this.type = type;
        this.types = types;
    }

    public Transform GetObj()
    {
        if (queues.Count>0)
        {
            return queues.Dequeue();
        }
        else
        {
            return SpawnNew();
        }
    }

    public void Push(Transform obj)
    {
        obj.SetParent(parent);
        queues.Enqueue(obj);
    }
    private Transform SpawnNew()
    {
        GameObject item = UnityEngine.Object.Instantiate(Resources.Load<GameObject>(type.ToString())).gameObject;
        item.name = parent.gameObject.name;
        foreach (Type type in types)
        {
            item.AddComponent(type);
        }
        item.transform.parent = null;
        return item.transform;
    }
}      
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 對象池管理類
/// </summary>
public class PoolMgr:BaseManager<PoolMgr>
{
    private Transform poolParent; 
    private Dictionary<string,Pool> pools;
    public void Init()
   {
        GameObject game = new GameObject("Pools");
        game.SetActive(false);
        poolParent = game.transform;
        pools = new Dictionary<string, Pool>();
        string[] objNames = Enum.GetNames(typeof(ObjType));
        for (int i = 0; i < objNames.Length; i++)
        {
            pools.Add(objNames[i],new Pool(objNames[i],poolParent,(ObjType)Enum.Parse(typeof(ObjType),objNames[i]), typeof(HideSelf)));
        }
   }


    public void GetObjByInput()
    {
        string[] objNames = Enum.GetNames(typeof(ObjType));
        string name = objNames[UnityEngine.Random.Range(0, objNames.Length)];
        GetObj(name);
    }

    public Transform GetObj(string name)
    {
        if (pools.ContainsKey(name))
        {
            Transform obj = pools[name].GetObj();
            obj.parent = null;
            return obj;
        }
        else
        {
            Debug.Log("不包含這個key:" + name);
            return null;
        }
    }

    public void Push(string name,Transform obj)
    {
        if (pools.ContainsKey(name))
        {
            pools[name].Push(obj);
        }
        else
        {
            Debug.Log("不包含這個key:"+name);
        }
    }

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

public class HideSelf : MonoBehaviour
{

    private void OnEnable()
    {
        Invoke("Hide", 1.2f);
    }

    private void Hide()
    {
        PoolMgr.GetInstance.Push(transform.name,transform);
    }

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

public class GameManager : MonoBehaviour
{
   
    void Start()
    {
        PoolMgr.GetInstance.Init();
    }

   
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            PoolMgr.GetInstance.GetObjByInput();
        }
    }
}