zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Model/Game/Pool/AssetBundlePool.cs

176 lines
4.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ET
{
public class AssetBundlePoolAwakeSystem : AwakeSystem<AssetBundlePool, string>
{
public override void Awake(AssetBundlePool self,string name)
{
self.Awake(name);
}
}
public class AssetBundlePoolDestroySystem : DestroySystem<AssetBundlePool>
{
public override void Destroy(AssetBundlePool self)
{
self.Destroy();
}
}
public class AssetBundlePool : Entity
{
#if UNITY_EDITOR
public Dictionary<string, ResourceEntity> InspectorDic = new Dictionary<string, ResourceEntity>();
#endif
public string PoolName { get; private set; }
/// <summary>
/// 资源池字典
/// </summary>
private Dictionary<string, ResourceEntity> m_ResourceDic;
/// <summary>
/// 需要移除的Key链表
/// </summary>
private LinkedList<string> m_NeedRemoveKeyList;
public void Awake(string poolName)
{
PoolName = poolName;
m_ResourceDic = new Dictionary<string, ResourceEntity>();
m_NeedRemoveKeyList = new LinkedList<string>();
}
public void Regist(ResourceEntity entity)
{
entity.Spawn();
#if UNITY_EDITOR
InspectorDic[entity.ResourceName] = entity;
#endif
m_ResourceDic[entity.ResourceName] = entity;
}
/// <summary>
/// 资源取池
/// </summary>
/// <param name="resourceName"></param>
/// <returns></returns>
public ResourceEntity Spawn(string resourceName)
{
if (m_ResourceDic.TryGetValue(resourceName, out ResourceEntity resourceEntity))
{
resourceEntity.Spawn();
#if UNITY_EDITOR
if (InspectorDic.ContainsKey(resourceEntity.ResourceName))
{
InspectorDic[resourceEntity.ResourceName] = resourceEntity;
}
#endif
}
return resourceEntity;
}
/// <summary>
/// 资源回池
/// </summary>
public void Despawn(string resourceName)
{
if (m_ResourceDic.TryGetValue(resourceName, out ResourceEntity resourceEntity))
{
resourceEntity.Despawn();
#if UNITY_EDITOR
if (InspectorDic.ContainsKey(resourceEntity.ResourceName))
{
InspectorDic[resourceEntity.ResourceName] = resourceEntity;
}
#endif
}
}
/// <summary>
/// 释放可释放的资源
/// </summary>
public void Release()
{
var enumerator = m_ResourceDic.GetEnumerator();
while (enumerator.MoveNext())
{
ResourceEntity entity = enumerator.Current.Value;
// if (entity.CanRelease)
// {
//#if UNITY_EDITOR
// if (InspectorDic.ContainsKey(entity.ResourceName))
// InspectorDic.Remove(entity.ResourceName);
//#endif
// m_NeedRemoveKeyList.AddFirst(entity.ResourceName);
// entity.Release();
// }
}
//删除
var curr = m_NeedRemoveKeyList.First;
while (curr != null)
{
string key = curr.Value;
m_ResourceDic.Remove(key);
var next = curr.Next;
m_NeedRemoveKeyList.Remove(curr);
curr = next;
}
}
public void Release(ResourceEntity resourceEntity)
{
if (m_ResourceDic.TryGetValue(resourceEntity.ResourceName, out ResourceEntity resource))
{
#if UNITY_EDITOR
if (InspectorDic.ContainsKey(resourceEntity.ResourceName))
InspectorDic.Remove(resourceEntity.ResourceName);
#endif
m_ResourceDic.Remove(resource.ResourceName);
resource.Release();
}
}
/// <summary>
/// 强制释放所有资源
/// </summary>
public void ReleaseAll()
{
var enumerator = m_ResourceDic.GetEnumerator();
while (enumerator.MoveNext())
{
ResourceEntity entity = enumerator.Current.Value;
#if UNITY_EDITOR
if (InspectorDic.ContainsKey(entity.ResourceName))
InspectorDic.Remove(entity.ResourceName);
#endif
m_NeedRemoveKeyList.AddFirst(entity.ResourceName);
entity.Release();
}
//删除
var curr = m_NeedRemoveKeyList.First;
while (curr != null)
{
string key = curr.Value;
m_ResourceDic.Remove(key);
var next = curr.Next;
m_NeedRemoveKeyList.Remove(curr);
curr = next;
}
}
public void Destroy()
{
m_ResourceDic.Clear();
m_ResourceDic = null;
m_NeedRemoveKeyList.Clear();
}
}
}