mycj_demo/mycj/Assets/Game/Scripts/FB-Framework/FB-Pool/FBSubPool.cs

102 lines
2.6 KiB
C#
Raw Normal View History

2024-12-02 09:37:47 +08:00
// Felix-BangFBSubPool
//   へ     /|
//  /7    ∠_/
//  / │    
//  Z _,    /`ヽ
// │     ヽ   /  〉
//  Y     `  /  /
// イ● 、 ●  ⊂⊃〈  /
// ()  へ    | \〈
//  >ー 、_  ィ  │
//  / へ   / ノ<|
//  ヽ_ノ  (_  │//
//  7       |
//  ―r ̄ ̄`ー―_
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace FBFramework
{
public class FBSubPool
{
Transform f_parent;
GameObject f_prefab;
List<GameObject> f_objects = new List<GameObject>();
/// <summary> 名称标识 </summary>
public string Name
{
get { return f_prefab.name; }
}
/// <summary>
/// 构造
/// </summary>
/// <param name="prefab"> 预制件 </param>
public FBSubPool(Transform parent, GameObject prefab)
{
f_parent = parent;
f_prefab = prefab;
}
/// <summary> 创建对象 </summary>
public GameObject Spawn()
{
GameObject go = null;
foreach (var obj in f_objects)
{
if (!obj.activeSelf)
{
go = obj;
break;
}
}
if (go == null)
{
go = GameObject.Instantiate<GameObject>(f_prefab);
go.transform.parent = f_parent;
f_objects.Add(go);
}
go.SetActive(true);
go.SendMessage("OnSpawn", SendMessageOptions.DontRequireReceiver);
return go;
}
/// <summary> 回收对象 </summary>
public void Unspawn(GameObject go)
{
if (IsContains(go))
{
go.SendMessage("OnUnspawn", SendMessageOptions.DontRequireReceiver);
go.SetActive(false);
}
}
/// <summary> 回收本对象池全部对象 </summary>
public void UnspawnAll()
{
foreach (var item in f_objects)
{
if (item.activeSelf)
Unspawn(item);
}
}
/// <summary>
/// 对象池是否包含对象
/// </summary>
/// <param name="go">目标对象</param>
/// <returns></returns>
public bool IsContains(GameObject go)
{
return f_objects.Contains(go);
}
}
}