-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathTextureSetComponent.cs
More file actions
137 lines (123 loc) · 4.09 KB
/
TextureSetComponent.cs
File metadata and controls
137 lines (123 loc) · 4.09 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using GameFramework;
using GameFramework.Event;
using GameFramework.FileSystem;
using GameFramework.ObjectPool;
using GameFramework.Resource;
using UnityEngine;
using UnityGameFramework.Runtime;
#if ODIN_INSPECTOR
using Sirenix.OdinInspector;
#endif
namespace UGFExtensions.Texture
{
public partial class TextureSetComponent : GameFrameworkComponent
{
/// <summary>
/// 检查是否可以释放间隔
/// </summary>
[SerializeField] private float m_CheckCanReleaseInterval = 30f;
private float m_CheckCanReleaseTime = 0.0f;
/// <summary>
/// 对象池自动释放时间间隔
/// </summary>
[SerializeField] private float m_AutoReleaseInterval = 60f;
/// <summary>
/// 保存加载的图片对象
/// </summary>
#if ODIN_INSPECTOR
[ShowInInspector]
#endif
private LinkedList<LoadTextureObject> m_LoadTextureObjectsLinkedList;
/// <summary>
/// 散图集合对象池
/// </summary>
private IObjectPool<TextureItemObject> m_TexturePool;
#if UNITY_EDITOR
public LinkedList<LoadTextureObject> LoadTextureObjectsLinkedList
{
get => m_LoadTextureObjectsLinkedList;
set => m_LoadTextureObjectsLinkedList = value;
}
#endif
/// <summary>
/// 序号
/// </summary>
private int m_SerialId = 0;
/// <summary>
/// 取消加载序号集合
/// </summary>
public HashSet<int> m_CancelId;
private IEnumerator Start()
{
yield return new WaitForEndOfFrame();
ObjectPoolComponent objectPoolComponent = GameEntry.GetComponent<ObjectPoolComponent>();
m_TexturePool = objectPoolComponent.CreateMultiSpawnObjectPool<TextureItemObject>(
"TexturePool",
m_AutoReleaseInterval, 16, 60, 0);
m_LoadTextureObjectsLinkedList = new LinkedList<LoadTextureObject>();
m_CancelId = new HashSet<int>();
InitializedFileSystem();
InitializedResources();
InitializedWeb();
}
private void Update()
{
m_CheckCanReleaseTime += Time.unscaledDeltaTime;
if (m_CheckCanReleaseTime < (double)m_CheckCanReleaseInterval)
return;
ReleaseUnused();
}
/// <summary>
/// 回收无引用的Texture。
/// </summary>
#if ODIN_INSPECTOR
[Button("Release Unused")]
#endif
public void ReleaseUnused()
{
LinkedListNode<LoadTextureObject> current = m_LoadTextureObjectsLinkedList.First;
while (current != null)
{
var next = current.Next;
if (current.Value.Texture2dObject.IsCanRelease())
{
m_TexturePool.Unspawn(current.Value.Texture2D);
ReferencePool.Release(current.Value.Texture2dObject);
ReferencePool.Release(current.Value);
m_LoadTextureObjectsLinkedList.Remove(current);
}
current = next;
}
m_CheckCanReleaseTime = 0f;
}
private void SetTexture(ISetTexture2dObject setTexture2dObject, Texture2D texture,int serialId = -1)
{
m_LoadTextureObjectsLinkedList.AddLast(LoadTextureObject.Create(setTexture2dObject, texture));
if (!m_CancelId.Contains(serialId))
{
setTexture2dObject.SetTexture(texture);
}
else
{
m_CancelId.Remove(serialId);
}
}
/// <summary>
/// 取消设置图片。
/// </summary>
/// <param name="id"></param>
public void CancelSetTexture(int id)
{
if (id<0)
{
Log.Error($"Cancel Id:{id} is not invalid! id must >= 0");
return;
}
m_CancelId.Add(id);
}
}
}