132 lines
3.4 KiB
C#
132 lines
3.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
[AddComponentMenu("AlarmObjectPool/本地告警对象池")]
|
|
/// <summary>
|
|
/// 本地告警对象池
|
|
/// </summary>
|
|
public class AlarmObjectPool : MonoBehaviour
|
|
{
|
|
public static AlarmObjectPool instance;
|
|
|
|
|
|
#region 池对象操作
|
|
|
|
//-------预制体-------
|
|
|
|
/// <summary>
|
|
/// 报警预制体
|
|
/// </summary>
|
|
//public GameObject item_prefab;
|
|
|
|
//-------池对象-------
|
|
|
|
/// <summary>
|
|
/// 报警
|
|
/// </summary>
|
|
public Queue<LocalVideoAlarmItem> alarm_item_pool = new Queue<LocalVideoAlarmItem>();
|
|
|
|
/// <summary>
|
|
/// 按钮
|
|
/// </summary>
|
|
public Queue<Button> buttonImages_pool = new Queue<Button>();
|
|
//-------池操作-------
|
|
|
|
/// <summary>
|
|
/// 从池中取出
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="type"></param>
|
|
/// <returns></returns>
|
|
public T GetItemFromPool<T>(GameObject item_prefab)
|
|
{
|
|
object resoult = null;
|
|
switch (typeof(T).Name)
|
|
{
|
|
case "LocalVideoAlarmItem":
|
|
if (alarm_item_pool.Count > 0)
|
|
{
|
|
var item = alarm_item_pool.Dequeue();
|
|
item.gameObject.SetActive(true);
|
|
resoult = item;
|
|
}
|
|
else
|
|
{
|
|
var temp = Instantiate(item_prefab);
|
|
resoult = temp.GetComponent<LocalVideoAlarmItem>();
|
|
}
|
|
break;
|
|
case "Button":
|
|
{
|
|
if (buttonImages_pool.Count > 0)
|
|
{
|
|
var item = buttonImages_pool.Dequeue();
|
|
item.gameObject.SetActive(true);
|
|
item.transform.GetChild(0).gameObject.SetActive(false);
|
|
resoult = item;
|
|
}
|
|
else
|
|
{
|
|
var temp = Instantiate(CalendarDetails.Inst.preform);
|
|
temp.gameObject.SetActive(true);
|
|
temp.transform.GetChild(0).gameObject.SetActive(false);
|
|
resoult = temp.GetComponent<Button>();
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return (T)resoult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 回收至池中
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="item"></param>
|
|
public void PushItemToPool<T>(T item)
|
|
{
|
|
switch (item)
|
|
{
|
|
case LocalVideoAlarmItem:
|
|
LocalVideoAlarmItem alarm_temp = item as LocalVideoAlarmItem;
|
|
alarm_temp.transform.SetParent(null);
|
|
alarm_temp.gameObject.SetActive(false);
|
|
alarm_temp.sprites.Clear();
|
|
alarm_item_pool.Enqueue(alarm_temp);
|
|
break;
|
|
case Button:
|
|
Button button = item as Button;
|
|
button.onClick.RemoveAllListeners();
|
|
button.transform.SetParent(null);
|
|
button.gameObject.SetActive(false);
|
|
buttonImages_pool.Enqueue(button);
|
|
break;
|
|
default: break;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
private void Awake()
|
|
{
|
|
instance = this;
|
|
}
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
}
|