001==C#超简单的对象池

//-----对象池
public class MyPools : MonoBehaviour
{
    public static MyPools instance;
    static Queue<GameObject>  Pool = new Queue<GameObject>();

    void Start()
    {
        instance = this;
    }
    //------------------------(预制物,出生点位置,旋转,父物体)
    public GameObject Create(GameObject _prefabs, Vector3 _pos, Quaternion qua)
    {
        GameObject g = null;
        if (Pool.Count > 0)
        {
            g = Pool.Dequeue();
            g.SetActive(true);
            g.transform.position = _pos;
        }
        else
        {
            g = Instantiate(_prefabs, _pos, qua);
            g.transform.SetParent(GameObject.FindGameObjectWithTag("Bullet").transform);
        }
        return g;
    }

    //(预制物,回到出生点位置,旋转)
    public static void Delete(GameObject _object, Vector3 _resoucesPos, Quaternion qua)
    {
        _object.SetActive(false);
        _object.transform.position = _resoucesPos;
        _object.transform.localRotation = qua;

        Pool.Enqueue(_object);

    }
}

猜你喜欢

转载自blog.csdn.net/qq_38104858/article/details/80313537