using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace ET { public class AssetBundlePoolAwakeSystem : AwakeSystem { public override void Awake(AssetBundlePool self,string name) { self.Awake(name); } } public class AssetBundlePoolDestroySystem : DestroySystem { public override void Destroy(AssetBundlePool self) { self.Destroy(); } } public class AssetBundlePool : Entity { #if UNITY_EDITOR public Dictionary InspectorDic = new Dictionary(); #endif public string PoolName { get; private set; } /// /// 资源池字典 /// private Dictionary m_ResourceDic; /// /// 需要移除的Key链表 /// private LinkedList m_NeedRemoveKeyList; public void Awake(string poolName) { PoolName = poolName; m_ResourceDic = new Dictionary(); m_NeedRemoveKeyList = new LinkedList(); } public void Regist(ResourceEntity entity) { entity.Spawn(); #if UNITY_EDITOR InspectorDic[entity.ResourceName] = entity; #endif m_ResourceDic[entity.ResourceName] = entity; } /// /// 资源取池 /// /// /// 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; } /// /// 资源回池 /// 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 } } /// /// 释放可释放的资源 /// public void Release() { Dictionary.Enumerator 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(); } } /// /// 强制释放所有资源 /// public void ReleaseAll() { Dictionary.Enumerator 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(); } } }