对象池应用

45 篇文章 0 订阅
using UnityEngine;
using System.Collections.Generic;
using System.Text;
public enum EnemyType
{
    FirstEnemy,
    SecnodEnemy,
    ThereEnemy,
    FourEnemy,
    FiveEnemy,
}
public class ObjectPool : Singleton < ObjectPool > {
    //敌人池
    private   Dictionary < string , List < GameObject >> mPools;
    GameObject _result = null ;
    #region singleton
    public ObjectPool()
    {
        mPools = new Dictionary < string , List < GameObject >>();
     
    }
    #endregion
    public GameObject SwpanObject( EnemyType type, Vector3 pos, Quaternion rotation)
    {
        //key 是集合的主键
        _result = null ;
        string _key = CreatEnemy(type).name.Replace( "(Clone)" , "" );
        //
        if (mPools.ContainsKey(_key))
        {
            if (mPools[_key].Count > 0)
            {
                 _result = mPools[_key][0];
                mPools[_key].Remove(_result);
                if (_result)
                {
                    _result.SetActive( true );
                    _result.transform.position = pos;
                    _result.transform.rotation = rotation;
                    return _result;
                }
            }
        }
            //创建一个对象
            GameObject _res = GameObject .Instantiate(CreatEnemy(type), pos, rotation) as GameObject ;
            return _res;
    }
    public GameObject SwpanObject( GameObject type, Vector3 pos, Quaternion rotation)
    {
        //key 是集合的主键
        _result = null ;
        string _key = type.tag;
        //
        if (mPools.ContainsKey(_key))
        {
            if (mPools[_key].Count > 0)
            {
                _result = mPools[_key][0];
                mPools[_key].Remove(_result);
                if (_result)
                {
                    _result.SetActive( true );
                    _result.transform.position = pos;
                    _result.transform.rotation = rotation;
                    return _result;
                }
            }
        }
        //创建一个对象
        GameObject _res = GameObject .Instantiate(type, pos, rotation) as GameObject ;
        return _res;
    }
    public GameObject CreatEnemy( EnemyType type) {
        StringBuilder path = new StringBuilder ();
        switch (type)
        {
            case EnemyType .FirstEnemy:
                path.Append( "Enemy/FirstEnemy" );
                break ;
            case EnemyType .SecnodEnemy:
                path.Append( "Enemy/SecondEnemy" );
                break ;
            case EnemyType .ThereEnemy:
                path.Append( "Enemy/ThereEnemy" );
                break ;
            case EnemyType .FourEnemy:
                path.Append( "Enemy/FourEnemy" );
                break ;
            default :
                break ;
        }
        GameObject obj = Resources .Load(path.ToString()) as GameObject ;
        return obj;
    }
    public void DestoryObject( GameObject gameObject)
    {
        gameObject.SetActive( false );
        string _key = gameObject.name.Replace( "(Clone)" , "" );
        //如果没有这个类型的集合
        if (!mPools.ContainsKey(_key))
        {
            mPools.Add(_key, new List < GameObject >());
        }
        mPools[_key].Add(gameObject);
     //   Debug.Log("死亡");
     //  Destory(_key);
    }
    /// <summary>
    /// 死的怪物才会放池子里
    /// </summary>
    /// <param name=" name "></param>
    private void Destory( string name)
    {
        List < GameObject > _pool = new List < GameObject >();
        if (Instance.mPools.Count > 0)
        {
            _pool = Instance.mPools[name];
        }
        if (_pool.Count > 3)
        {
            for ( int i = 2; i > 0; i--)
            {
                GameObject .Destroy(_pool[i]);
                _pool.Remove(_pool[i]);
                mPools[name].Remove(mPools[name][i]);
            }
        }
    }
}
-----------------------------------------------------------
运用
  public void CreatEnemy( EnemyType type, Transform target) {
        mEnemy = ObjectPool .Instance.SwpanObject(type,transform.position, Quaternion .identity);
        //这句话必须有 否则报错
        if (mEnemy== null )
        {
            mEnemy = ObjectPool .Instance.CreatEnemy(type);
            mEnemy = ObjectPool .Instance.SwpanObject(mEnemy, transform.position, Quaternion .identity);
        }
      //  mEnemy.GetComponent<CharBase>().mTarTrans = target;
        initLife = mEnemy;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值