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;
}