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

102 lines
2.6 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-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);
}
}
}