78 lines
2.0 KiB
C#
78 lines
2.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PollManager : MonoBehaviour
|
|
{
|
|
Transform m_PollParent;
|
|
|
|
//对象池字典
|
|
Dictionary<string, PollBase> m_Polls = new Dictionary<string, PollBase>();
|
|
|
|
private void Awake()
|
|
{
|
|
m_PollParent = this.transform.parent.Find("Poll");
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 创建对象池
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="pollName"></param>
|
|
/// <param name="releaseTime"></param>
|
|
private void CreatePoll<T>(string pollName, float releaseTime)
|
|
where T : PollBase
|
|
{
|
|
if (!m_Polls.TryGetValue(pollName, out PollBase poll))
|
|
{
|
|
GameObject go = new GameObject();
|
|
go.name = pollName;
|
|
go.transform.SetParent(m_PollParent);
|
|
poll = go.AddComponent<T>();
|
|
poll.Init(releaseTime);
|
|
m_Polls.Add(pollName, poll);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 创建物体对象池
|
|
/// </summary>
|
|
/// <param name="pollName"></param>
|
|
/// <param name="releaseTime"></param>
|
|
public void CreateGameObjectPoll(string pollName, float releaseTime)
|
|
{
|
|
CreatePoll<GameObjectPoll>(pollName, releaseTime);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 取出对象
|
|
/// </summary>
|
|
/// <param name="pollName"></param>
|
|
/// <param name="asseteName"></param>
|
|
/// <returns></returns>
|
|
public Object Spwan(string pollName, string asseteName)
|
|
{
|
|
if (m_Polls.TryGetValue(pollName, out PollBase poll))
|
|
{
|
|
return poll.Spwan(asseteName);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 回收对象
|
|
/// </summary>
|
|
/// <param name="pollName"></param>
|
|
/// <param name="asseteName"></param>
|
|
/// <returns></returns>
|
|
public void UnSpwan(string pollName, string asseteName, Object assete)
|
|
{
|
|
if (m_Polls.TryGetValue(pollName, out PollBase poll))
|
|
{
|
|
poll.UnSpwan(asseteName, assete);
|
|
}
|
|
}
|
|
}
|
|
|