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

public class ObjectPool : MonoBehaviour
{
public static ObjectPool instence;//单例模式用于外部调用

public GameObject shado;//要生成的预制体(可通过资源管理类获取)
/*[CreateAssetMenu(menuName ="CreatManagerVarsContainer")]

public class ManagerVars : ScriptableObject
{
public static ManagerVars GetManagerVars()
{
return Resources.Load("ManagerVarsContainer");
}*/

//要生成的预制体集合
private List playerShado = new List();

private int InstentCount = 5;//在对象池中默认生成多少个

private void Awake()
{
    instence = this;
    Init();//初始生成
}

public void Init()
{
    for (int i = 0; i < InstentCount ; i++)
    {
        //生成默认个数预制体(go.SetActive(false);,并添加到目标预制体List中
        InstantceObject(shado,ref playerShado );
    }
}
//生成与添加入集合的方法
//(返回值GameObject便于外部调用时,如果对象池中同类物体都处于占用激活状态,则再生成新的预制体并添加到集合中)
private GameObject InstantceObject(GameObject prefab, ref List<GameObject > addlist  )
{
    GameObject go = Instantiate(prefab, transform);
    go.SetActive(false);
    addlist.Add(go);
    return go;
}

//外部调用方法
public GameObject GetShado()
{
    //遍历集合中的物体知否激活/占用,没有则返回应用该物体。
    for (int i = 0; i < InstentCount ; i++)
    {
        if(playerShado [i].activeInHierarchy==false )
        {
            return playerShado[i];
        }
    }
    //如List中的物体均处于占用状态,(对象池中同类物体不够)则生成新的对象返回并添加到List集合(对象池)中
    return InstantceObject(shado ,ref playerShado );
}

//gameObject.SetActive(false)写在预制体/外部脚本上

}

--------------------------------------------------------------------------
例:生成残影

残影预制体脚本


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class PlayerShado : MonoBehaviour
{
[SerializeField]
private bool reset;

private void FixedUpdate()
{
    if (reset)
    {
        gameObject.GetComponent<SpriteRenderer>().sprite = GameObject.FindGameObjectWithTag("Player").GetComponent<SpriteRenderer>().sprite;//精灵动作保持一致
        transform.GetComponent<SpriteRenderer>().DOColor(new Color(transform.GetComponent<SpriteRenderer>().color.r,
          transform.GetComponent<SpriteRenderer>().color.g, transform.GetComponent<SpriteRenderer>().color.b, 0), 0.2f).OnComplete(() =>
          {
              gameObject.SetActive(false);   
          });
        reset = false;
        print("FixedUpdate");
    }
}
//物体未启用时执行(在Update后执行)
private void OnDisable()
{
    transform.GetComponent<SpriteRenderer>().color = new Color(transform.GetComponent<SpriteRenderer>().color.r,
            transform.GetComponent<SpriteRenderer>().color.g, transform.GetComponent<SpriteRenderer>().color.b, 1);
    reset = true;
    print("Onsisable");

}

}

//物体启用时执行
private void OnEnable()
{
gameObject.GetComponent().sprite = GameObject.FindGameObjectWithTag("Player").GetComponent().sprite;
transform.GetComponent().DOColor(new Color(transform.GetComponent().color.r,
transform.GetComponent().color.g, transform.GetComponent().color.b, 0), 0.2f).OnComplete(() =>
{
gameObject.SetActive(false);
});
}

//物体未启用时执行
private void OnDisable()
{
    transform.GetComponent<SpriteRenderer>().color = new Color(transform.GetComponent<SpriteRenderer>().color.r,
            transform.GetComponent<SpriteRenderer>().color.g, transform.GetComponent<SpriteRenderer>().color.b, 1);
    //reset = true;
    //print("Onsisable");

}