mycj_demo/mycj/Assets/Game/Scripts/FB-Framework/FB-Pool/FBObjectPool.cs
2024-12-02 09:37:47 +08:00

86 lines
2.2 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Felix-BangFBObjectPool
//   へ     /|
//  /7    ∠_/
//  / │    
//  Z _,    /`ヽ
// │     ヽ   /  〉
//  Y     `  /  /
// イ● 、 ●  ⊂⊃〈  /
// ()  へ    | \〈
//  >ー 、_  ィ  │
//  / へ   / ノ<|
//  ヽ_ノ  (_  │//
//  7       |
//  ―r ̄ ̄`ー―_
// Describe对象池
// Createtime2018/9/18
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace FBFramework
{
public class FBObjectPool : FBSingleton<FBObjectPool>
{
public string ResourceDir = "";
Dictionary<string, FBSubPool> f_pools = new Dictionary<string, FBSubPool>();
/// <summary>
/// 创建对象
/// </summary>
/// <param name="name"> 对象名</param>
/// <returns></returns>
public GameObject Spawn(string name)
{
if (!f_pools.ContainsKey(name))
RegisterNewPool(name);
FBSubPool pool = f_pools[name];
return pool.Spawn();
}
/// <summary> 回收对象 </summary>
public void Unspawn(GameObject go)
{
FBSubPool pool = null;
foreach (FBSubPool p in f_pools.Values)
{
if (p.IsContains(go))
{
pool = p;
break;
}
}
pool.Unspawn(go);
}
/// <summary> 回收所有对象 </summary>
public void UnspawnAll()
{
foreach (FBSubPool p in f_pools.Values)
p.UnspawnAll();
}
/// <summary> 创建一个新的SubPool </summary>
void RegisterNewPool(string name)
{
string path = "";
if (string.IsNullOrEmpty(ResourceDir))
path = name;
else
path = ResourceDir + "/" + name;
GameObject prefab = Resources.Load<GameObject>(path);
FBSubPool pool = new FBSubPool(transform, prefab);
f_pools.Add(pool.Name, pool);
}
}
}