1
This commit is contained in:
43
unity/Assets/Script/Tools/Pollsysytem/GameObjectPoll.cs
Normal file
43
unity/Assets/Script/Tools/Pollsysytem/GameObjectPoll.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameObjectPoll : PollBase
|
||||
{
|
||||
public override Object Spwan(string name)
|
||||
{
|
||||
Object obj = base.Spwan(name);//´Ó¸¸ÀàÖÐÈ¡³ö
|
||||
if (obj == null)
|
||||
return null;
|
||||
|
||||
GameObject go = obj as GameObject;
|
||||
go.SetActive(true);
|
||||
return obj;
|
||||
}
|
||||
|
||||
public override void UnSpwan(string name, Object obj)
|
||||
{
|
||||
GameObject go = obj as GameObject;
|
||||
go.SetActive(false);
|
||||
go.transform.SetParent(this.transform, false);
|
||||
base.UnSpwan(name, obj);
|
||||
}
|
||||
|
||||
public override void Release()
|
||||
{
|
||||
base.Release();
|
||||
foreach (PollObject items in m_Objects)
|
||||
{
|
||||
if (System.DateTime.Now.Ticks - items.lastUserTime.Ticks >= m_ReleaseTime * 10000000)
|
||||
{
|
||||
Debug.Log("GameObjectPoll Release item:" + System.DateTime.Now);
|
||||
Destroy(items.Object);
|
||||
|
||||
m_Objects.Remove(items);
|
||||
Release();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
unity/Assets/Script/Tools/Pollsysytem/GameObjectPoll.cs.meta
Normal file
11
unity/Assets/Script/Tools/Pollsysytem/GameObjectPoll.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d0db3f2dc79616b4b90ce958ff538335
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
72
unity/Assets/Script/Tools/Pollsysytem/PollBase.cs
Normal file
72
unity/Assets/Script/Tools/Pollsysytem/PollBase.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PollBase : MonoBehaviour
|
||||
{
|
||||
//自动释放时间/秒
|
||||
protected float m_ReleaseTime;
|
||||
|
||||
//上次释放的时间/毫微秒 10……7;
|
||||
protected long m_LastReleaseTime = 0;
|
||||
|
||||
//对象池
|
||||
protected List<PollObject> m_Objects;
|
||||
public void Start()
|
||||
{
|
||||
m_LastReleaseTime = System.DateTime.Now.Ticks;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化对象池
|
||||
/// </summary>
|
||||
/// <param name="time"></param>
|
||||
public void Init(float time)
|
||||
{
|
||||
m_ReleaseTime = time;
|
||||
m_Objects = new List<PollObject>();
|
||||
}
|
||||
/// <summary>
|
||||
/// 取出对象
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Object Spwan(string name)
|
||||
{
|
||||
foreach (PollObject po in m_Objects)
|
||||
{
|
||||
if (po.Name == name)
|
||||
{
|
||||
m_Objects.Remove(po);
|
||||
return po.Object;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// 回收
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="obj"></param>
|
||||
public virtual void UnSpwan(string name, Object obj)
|
||||
{
|
||||
PollObject po = new PollObject(name, obj);
|
||||
m_Objects.Add(po);
|
||||
}
|
||||
/// <summary>
|
||||
/// 释放
|
||||
/// </summary>
|
||||
public virtual void Release()
|
||||
{
|
||||
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
if (System.DateTime.Now.Ticks - m_LastReleaseTime >= m_ReleaseTime * 10000000)
|
||||
{
|
||||
m_LastReleaseTime = System.DateTime.Now.Ticks;
|
||||
Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
11
unity/Assets/Script/Tools/Pollsysytem/PollBase.cs.meta
Normal file
11
unity/Assets/Script/Tools/Pollsysytem/PollBase.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9ba11d249cb380348b661dda9386a33a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
18
unity/Assets/Script/Tools/Pollsysytem/PollObject.cs
Normal file
18
unity/Assets/Script/Tools/Pollsysytem/PollObject.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class PollObject
|
||||
{
|
||||
//对象
|
||||
public Object Object;
|
||||
//对象名
|
||||
public string Name;
|
||||
//最后一次的使用时间,记录销毁
|
||||
public System.DateTime lastUserTime;
|
||||
|
||||
public PollObject(string name, Object obj)
|
||||
{
|
||||
Name = name;
|
||||
Object = obj;
|
||||
lastUserTime = System.DateTime.Now;
|
||||
}
|
||||
}
|
||||
11
unity/Assets/Script/Tools/Pollsysytem/PollObject.cs.meta
Normal file
11
unity/Assets/Script/Tools/Pollsysytem/PollObject.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3d7860cc304322b41a0233c9cf94ab61
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user