-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathSpriteCollectionComponent.cs
More file actions
93 lines (83 loc) · 3.08 KB
/
SpriteCollectionComponent.cs
File metadata and controls
93 lines (83 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using GameFramework;
using GameFramework.ObjectPool;
#if ODIN_INSPECTOR
using Sirenix.OdinInspector;
#endif
using UGFExtensions.Await;
using UGFExtensions.Timer;
using UnityEngine;
using UnityGameFramework.Runtime;
namespace UGFExtensions.SpriteCollection
{
public partial class SpriteCollectionComponent : GameFrameworkComponent
{
/// <summary>
/// 散图集合对象池
/// </summary>
private IObjectPool<SpriteCollectionItemObject> m_SpriteCollectionPool;
/// <summary>
/// 检查是否可以释放间隔
/// </summary>
[SerializeField] private float m_CheckCanReleaseInterval = 30f;
private float m_CheckCanReleaseTime = 0.0f;
/// <summary>
/// 对象池自动释放时间间隔
/// </summary>
[SerializeField] private float m_AutoReleaseInterval = 60f;
#if ODIN_INSPECTOR
[ReadOnly] [ShowInInspector]
#endif
private LinkedList<LoadSpriteObject> m_LoadSpriteObjectsLinkedList;
private HashSet<string> m_SpriteCollectionBeingLoaded;
private Dictionary<string, LinkedList<ISetSpriteObject>> m_WaitSetObjects;
#if UNITY_EDITOR
public LinkedList<LoadSpriteObject> LoadSpriteObjectsLinkedList
{
get => m_LoadSpriteObjectsLinkedList;
set => m_LoadSpriteObjectsLinkedList = value;
}
#endif
private void Start()
{
ObjectPoolComponent objectPoolComponent = UnityGameFramework.Runtime.GameEntry.GetComponent<ObjectPoolComponent>();
m_SpriteCollectionPool = objectPoolComponent.CreateMultiSpawnObjectPool<SpriteCollectionItemObject>(
"SpriteCollection",
m_AutoReleaseInterval, 16, 60, 0);
m_LoadSpriteObjectsLinkedList = new LinkedList<LoadSpriteObject>();
m_SpriteCollectionBeingLoaded = new HashSet<string>();
m_WaitSetObjects = new Dictionary<string, LinkedList<ISetSpriteObject>>();
InitializedResources();
}
private void Update()
{
m_CheckCanReleaseTime += Time.unscaledDeltaTime;
if (m_CheckCanReleaseTime < (double)m_CheckCanReleaseInterval)
return;
ReleaseUnused();
}
/// <summary>
/// 回收无引用的 Image 对应图集。
/// </summary>
#if ODIN_INSPECTOR
[Button("Release Unused")]
#endif
public void ReleaseUnused()
{
LinkedListNode<LoadSpriteObject> current = m_LoadSpriteObjectsLinkedList.First;
while (current != null)
{
var next = current.Next;
if (current.Value.SpriteObject.IsCanRelease())
{
m_SpriteCollectionPool.Unspawn(current.Value.Collection);
ReferencePool.Release(current.Value.SpriteObject);
m_LoadSpriteObjectsLinkedList.Remove(current);
}
current = next;
}
m_CheckCanReleaseTime = 0;
}
}
}