码迷,mamicode.com
首页 > 编程语言 > 详细

Unity Object Pool完全体

时间:2017-08-23 17:30:46      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:generic   style   iat   nbu   ati   log   eth   ror   sys   

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



    public class ObjectPool<T>
    {
        private readonly Stack<T> stack = new Stack<T>();
        private readonly Func<T> actionOnNew;
        private readonly UnityAction<T> actionOnGet;
        private readonly UnityAction<T> actionOnRelease;

        public int CountAll { get; private set; }
        public int CountInactive { get { return stack.Count; } }
        public int CountActive { get { return CountAll - CountInactive; } }

        public ObjectPool(Func<T> onNew, UnityAction<T> onGet = null, UnityAction<T> onRelease = null)
        {
            actionOnNew = onNew;
            actionOnGet = onGet;
            actionOnRelease = onRelease;
        }

        public T Get()
        {
            T element;
            if (stack.Count == 0)
            {
                element = actionOnNew == null ? default(T) : actionOnNew.Invoke();
                CountAll++;
            }
            else
            {
                element = stack.Pop();
            }

            if (actionOnGet != null) { actionOnGet.Invoke(element); }
            return element;
        }

        public void Release(T element)
        {
            if (stack.Count > 0 && ReferenceEquals(stack.Peek(), element))
            {
                Debug.LogError("Internal error. Trying to destroy object that is already released to pool.");
            }

            if (actionOnRelease != null) { actionOnRelease.Invoke(element); }

            stack.Push(element);

            if (stack.Count > CountAll)
            {
                CountAll = stack.Count;
            }
        }
    }

 使用泛型和委托简化代码,扩展性强。

使用示例:

Bullet类:

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

public class Bullet : MonoBehaviour
{
    private int speed;

    private int damage;

    private float lifeTime = 10.0f;

    private float lifeTimer = 0;

    public delegate void BulletDestroyEvent(Bullet bullet);

    public event BulletDestroyEvent OnBulletDestroy; 
    public int Speed
    {
        get { return speed; }
        set { speed = value; }
    }

    public int Damage
    {
        get { return damage; }
        set { damage = value; }
    }
        
    public float LifeTime
    {
        get { return lifeTime; }
        set { lifeTime = value; }
    }
        

    void Start ()
    {
        
    }
    
    void Update ()
    {
        if(Input.GetKeyDown(KeyCode.Mouse1))
        {
            OnBulletDestroy.Invoke(this);
        }

        lifeTimer += Time.deltaTime;
        if(lifeTimer >= LifeTime)
        {
            lifeTimer = 0.0f;
            OnBulletDestroy.Invoke(this);
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        OnBulletDestroy.Invoke(this);
    }
}

ObjectPoolManager:

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

public class ObjectPoolManager : MonoBehaviour
{
    public GameObject bulletPrefab;

    public ObjectPool<Bullet> bulletPool;

    public List<Bullet> bulletList; 
    void Start ()
    {
        bulletPool = new ObjectPool<Bullet>(BulletPoolOnNew,BulletOnGet,BulletOnRelease);
    }
    
    void Update ()
    {
        if(Input.GetKeyDown(KeyCode.Mouse0))
        {
            //generate one bullet   
            bulletList.Add(bulletPool.Get());
        }

        if(Input.GetKeyDown(KeyCode.W))
        {
            //delete one bullet
            bulletPool.Release(bulletList[0]);
        }
    }

    //pool method
    Bullet BulletPoolOnNew()
    {
        var bulletObject = Instantiate(bulletPrefab) as GameObject;

        bulletObject.GetComponent<Bullet>().OnBulletDestroy += bulletPool.Release;

        return bulletObject.GetComponent<Bullet>();
    }

    void BulletOnGet(Bullet bullet)
    {
        bullet.gameObject.SetActive(true);
    }

    void BulletOnRelease(Bullet bullet)
    {
        bullet.gameObject.SetActive(false);
    }
}

 

Unity Object Pool完全体

标签:generic   style   iat   nbu   ati   log   eth   ror   sys   

原文地址:http://www.cnblogs.com/litmin/p/7250575.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!