AkiraPixelWind/Assets/Scripts/Main/ObjPool/PrefabPoolForPath.cs

187 lines
4.4 KiB
C#
Raw Normal View History

2023-01-16 18:31:12 +08:00
using UnityEngine;
using System.Collections.Generic;
using Game;
using UnityEditor;
using Axibug.Resources;
// 局部对象池模板
public class PrefabPoolForPath
{
private Transform mb_vParentTf; // 创建对象时的父节点
private List<GameObject> mb_vObjList; // 对象列表
private string mb_vPrefabPath; // 对象预制名称
private E_PREFAB_TYPE mb_vPrefabType;
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;
}
}
public List<GameObject> vObjList
{
get
{
if (mb_vObjList == null)
mb_vObjList = new List<GameObject>();
return mb_vObjList;
}
}
// 构造 并 Init()
public PrefabPoolForPath(Transform tfParent, E_PREFAB_TYPE type,string prefabPath)
{
mb_vPrefabType = type;
this.Init(tfParent, prefabPath);
}
// 初始化
void Init(Transform tfParent, string prefabPath)
{
mb_vParentTf = tfParent;
switch (mb_vPrefabType)
{
case E_PREFAB_TYPE.EFFECT:
mb_vPrefabPath = prefabPath;
break;
}
}
// 隐藏
public 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 GetObj(int index)
{
if (index < vObjList.Count)
{
vObjList[index].gameObject.SetActive(true);
return vObjList[index];
}
GameObject go = ClonePrefab(mb_vPrefabPath, mb_vParentTf);
vObjList.Add(go);
return go;
}
// 获取
public GameObject GetAnyHide()
{
int count = vObjList.Count;
for (int i = 0; i < count; i++)
{
if (!vObjList[i].gameObject.activeSelf)
{
vObjList[i].gameObject.SetActive(true);
return vObjList[i];
}
}
return GetObj(count);
}
public void Remove(int index)
{
if (index >= vObjList.Count)
return;
GameObject vObj = vObjList[index];
GameObject.Destroy(vObj.gameObject);
vObjList.RemoveAt(index);
}
public void Remove(GameObject 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++)
{
GameObject vObj = vObjList[i];
GameObject.Destroy(vObj.gameObject);
}
vObjList.Clear();
}
private GameObject ClonePrefab(string path, Transform parent)
{
string rootPath = "Assets/GameAssets";
string MapName = path;
string tmp = MapName.Remove(0, rootPath.Length + 1);
int idx = tmp.LastIndexOf('/');
string bundleName = tmp.Substring(0, idx);
UnityEngine.Object asset = null;
if (AppEntry.Base.EditorResourceMode)
{
#if UNITY_EDITOR
asset = AssetDatabase.LoadAssetAtPath<GameObject>(MapName);
#endif
}
else
{
int id = MapName.GetHashCode();
asset = PrefabManager.LoadPrefab<UnityEngine.Object>(bundleName.ToLower(), MapName, id, parent);
}
if (asset == null)
{
Debug.LogError($"asset加载失败path={MapName}");
return null;
}
GameObject go = GameObject.Instantiate(asset, parent) as GameObject;
if (go == null)
{
Debug.LogError("LoadPrefabByEditor2. go == null. asset:" + asset);
}
return go;
}
}