203 lines
4.6 KiB
C#
203 lines
4.6 KiB
C#
|
using UnityEngine;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using Game;
|
|||
|
using UnityEditor;
|
|||
|
using Axibug.Resources;
|
|||
|
|
|||
|
public enum E_PREFAB_TYPE
|
|||
|
{
|
|||
|
NONE,
|
|||
|
EFFECT,
|
|||
|
}
|
|||
|
|
|||
|
// 局部对象池模板
|
|||
|
public class PrefabPool<T> where T : MonoBehaviour
|
|||
|
{
|
|||
|
private Transform mb_vParentTf; // 创建对象时的父节点
|
|||
|
private List<T> 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<T> vObjList
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (mb_vObjList == null)
|
|||
|
mb_vObjList = new List<T>();
|
|||
|
|
|||
|
return mb_vObjList;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 默认构造 需要配合 Init()使用
|
|||
|
public PrefabPool() { }
|
|||
|
|
|||
|
// 构造 并 Init()
|
|||
|
public PrefabPool(Transform tfParent, E_PREFAB_TYPE type)
|
|||
|
{
|
|||
|
string vPrefabName = typeof(T).FullName;
|
|||
|
mb_vPrefabType = type;
|
|||
|
this.Init(tfParent, vPrefabName);
|
|||
|
}
|
|||
|
|
|||
|
// 初始化
|
|||
|
void Init(Transform tfParent, string vPrefabName)
|
|||
|
{
|
|||
|
mb_vParentTf = tfParent;
|
|||
|
switch (mb_vPrefabType)
|
|||
|
{
|
|||
|
case E_PREFAB_TYPE.EFFECT:
|
|||
|
mb_vPrefabPath = $"Assets/GameAssets/Prefabs/Effect/{vPrefabName}/{vPrefabName}.prefab";
|
|||
|
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 T GetObj(int index)
|
|||
|
{
|
|||
|
if (index < vObjList.Count)
|
|||
|
{
|
|||
|
vObjList[index].gameObject.SetActive(true);
|
|||
|
return vObjList[index];
|
|||
|
}
|
|||
|
|
|||
|
GameObject go = ClonePrefab(mb_vPrefabPath, mb_vParentTf);
|
|||
|
T vObj = go.GetComponent<T>();
|
|||
|
if (vObj == null)
|
|||
|
vObj = go.AddComponent<T>();
|
|||
|
|
|||
|
vObjList.Add(vObj);
|
|||
|
return vObj;
|
|||
|
}
|
|||
|
|
|||
|
// 获取
|
|||
|
public T 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;
|
|||
|
|
|||
|
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();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|