TheInitialProject/Assets/Scripts/UI/UIMgr/UnitPool.cs
2024-10-23 16:59:02 +08:00

236 lines
6.0 KiB
C#

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Game;
using Game.HotFix;
using CaoCao.XAsset;
public abstract class UnitPool
{
public abstract void Hide();
public abstract MonoBehaviour GetAnyHideObject();
}
// 局部对象池模板
public class UnitPool<T> : UnitPool where T : MonoBehaviour
{
private Transform mb_vParentTf; // 创建对象时的父节点
private string mb_vPrefabName; // 对象预制名称
private string mb_vPrefabPath; // 路径
private List<T> mb_vObjList; // 对象列表
public MonoBehaviour mb_Component; // 所属脚本
public Transform Getmb_vParentTf()
{
return mb_vParentTf;
}
public void Setmb_vParentTf(Transform pTran)
{
mb_vParentTf = pTran;
}
// 显示的Unit个数
public int iActivityCount
{
get
{
int retCount = 0;
if (mb_vObjList != null)
{
for (int i = 0; i < mb_vObjList.Count; i++)
{
if (mb_vObjList[i].gameObject.activeSelf)
retCount++;
}
}
return retCount;
}
}
// Unit列表
public List<T> vObjList
{
get
{
if (mb_vObjList == null)
mb_vObjList = new List<T>();
return mb_vObjList;
}
}
/// <summary>
/// 构造 用类型名称去UIMgr.AllUIPath取unit.Prefab路径
/// </summary>
/// <param name="tfParent">父节点</param>
public UnitPool(MonoBehaviour _component, Transform tfParent)
{
mb_Component = _component;
mb_vParentTf = tfParent;
string vPrefabName = typeof(T).FullName;
var strNameAry = vPrefabName.Split('.');
mb_vPrefabName = strNameAry[strNameAry.Length - 1];
mb_vPrefabPath = UIMgr.AllUIPath[mb_vPrefabName];
//Debug.Log("mb_vPrefabName:" + mb_vPrefabName + ", mb_vPrefabPath:" + mb_vPrefabPath);
}
/// <summary>
/// 构造 指定unit.Prefab路径
/// </summary>
/// <param name="tfParent">父节点</param>
/// <param name="vPrefabPath">Prefab路径</param>
public UnitPool(MonoBehaviour _component, Transform tfParent, string vPrefabPath)
{
mb_Component = _component;
mb_vParentTf = tfParent;
string vPrefabName = typeof(T).FullName;
var strNameAry = vPrefabName.Split('.');
mb_vPrefabName = strNameAry[strNameAry.Length - 1];
mb_vPrefabPath = vPrefabPath;
//Debug.Log("mb_vPrefabName:" + mb_vPrefabName + ", mb_vPrefabPath:" + mb_vPrefabPath);
}
// 隐藏
public override void Hide()
{
for (int i = 0; i < vObjList.Count; i++)
{
vObjList[i].gameObject.SetActive(false);
}
}
// 隐藏父节点
public void HideNode()
{
mb_vParentTf.gameObject.SetActive(false);
}
// 显示父节点
public void ShowNode()
{
mb_vParentTf.gameObject.SetActive(true);
}
public GameObject Clone(string path, Transform t = null)
{
var request = Asset.InstantiateAsync(path, t);
if (request == null)
{
Debug.LogError("request == null. path:" + path + ", t is null:" + (t == null));
return null;
}
request.WaitForCompletion();
return request.gameObject;
}
// 获取
public T GetObj(int index)
{
if (index < vObjList.Count)
{
vObjList[index].gameObject.SetActive(true);
return vObjList[index];
}
//string uiName = typeof(T).FullName;
//var strNameAry = uiName.Split('.');
//Debug.Log("uiName1:" + uiName);
//uiName = strNameAry[strNameAry.Length - 1];
//Debug.Log("uiName2:" + uiName);
////var strNameAry = mb_vPrefabName.Split('.');
////string uiName = strNameAry[strNameAry.Length - 1];
//var uiPath = UIMgr.AllUIPath[uiName];
//Debug.Log("uiPath:" + uiPath);
//GameObject go = UIMgr.Instance.Clone(uiPath, mb_vParentTf);
GameObject go = Clone(mb_vPrefabPath, mb_vParentTf);
if (go == null)
return null;
go.name = mb_vPrefabName;
T vObj = go.GetComponent<T>();
if (vObj == null)
vObj = go.AddComponent<T>();
vObjList.Add(vObj);
//添加对不需要按钮的统一处理
//UIUnActive.BatchInit(vObj);
return vObj;
}
// 获取
public T GetAnyHide()
{
int count = vObjList.Count;
for (int i = 0; i < count; i++)
{
//if (vObjList[i].gameObject == null)
// continue;
if (!vObjList[i].gameObject.activeSelf)
{
vObjList[i].gameObject.SetActive(true);
return vObjList[i];
}
}
return GetObj(count);
}
public override MonoBehaviour GetAnyHideObject()
{
return GetAnyHide() as T;
}
public void Remove(int index)
{
if (index >= vObjList.Count)
return;
T vObj = vObjList[index];
GameObject.Destroy(vObj.gameObject);
vObjList.RemoveAt(index);
}
public void Remove(T obj)
{
for (int i = 0; i < vObjList.Count; i++)
{
if (vObjList[i] == obj)
{
GameObject.Destroy(obj.gameObject);
vObjList.RemoveAt(i);
return;
}
}
}
// 销毁
public void Destory()
{
for (int i = 0; i < vObjList.Count; i++)
{
T vObj = vObjList[i];
GameObject.Destroy(vObj.gameObject);
}
vObjList.Clear();
}
}