You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
1.8 KiB

2 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
public class SubPool
{
//集合
List<GameObject> m_objecs = new List<GameObject>();
//预设
GameObject m_prefab;
//名字
public string Name
{
get
{
return m_prefab.name;
}
}
public SubPool( GameObject go)
{
m_prefab = go;
}
//取出物体
public GameObject Spawn(Transform m_parent)
{
GameObject go = null;
foreach (var obj in m_objecs)
{
if (!obj.activeSelf)
{
go = obj;
}
}
if (go == null)
{
go = GameObject.Instantiate<GameObject>(m_prefab, m_parent.position,Quaternion.identity,m_parent);
go.transform.eulerAngles = m_parent.eulerAngles;
m_objecs.Add(go);
}
else
{
go.transform.SetParent(m_parent);
go.transform.position = m_parent.position;
go.transform.eulerAngles = m_parent.eulerAngles;
}
go.SetActive(true);
go.SendMessage("OnSpawn", SendMessageOptions.DontRequireReceiver);
return go;
}
//回收物体
public void UnSpawn(GameObject go)
{
if (Contain(go))
{
go.SendMessage("OnUnSpawn", SendMessageOptions.DontRequireReceiver);
go.SetActive(false);
}
}
//回收所有
public void UnspawnAll()
{
foreach (var obj in m_objecs)
{
if (obj.activeSelf)
{
UnSpawn(obj);
}
}
}
//判断是否属于list里边
public bool Contain(GameObject go) {
return m_objecs.Contains(go);
}
}