22using System . IO ;
33using FairyGUI ;
44using GameFrameX . Runtime ;
5+ using YooAsset ;
6+ using System . Collections . Generic ;
57using UnityEngine ;
68
7- namespace Unity . Startup
9+ namespace Unity . Startup . Procedure
810{
9- using System . Collections . Generic ;
10- using UnityEngine ;
11-
12- public class LRUCache
11+ public sealed class FairyGuiExtensionLoader : GLoader
1312 {
14- private class CacheItem
15- {
16- public string key ;
17- public NTexture texture ;
18-
19- public CacheItem ( string key , NTexture texture )
20- {
21- this . key = key ;
22- this . texture = texture ;
23- }
24- }
25-
26- private readonly int maxSize ;
27- private Dictionary < string , CacheItem > cache ;
28- private LinkedList < CacheItem > lruList ;
13+ private static string _cachePath ;
2914
30- public LRUCache ( int maxSize )
15+ private sealed class LoaderItem
3116 {
32- this . maxSize = maxSize ;
33- cache = new Dictionary < string , CacheItem > ( ) ;
34- lruList = new LinkedList < CacheItem > ( ) ;
35- }
17+ public readonly string Key ;
18+ public readonly NTexture Texture ;
19+ public readonly AssetHandle AssetHandle ;
3620
37- public NTexture Get ( string key )
38- {
39- if ( cache . TryGetValue ( key , out CacheItem item ) )
21+ public LoaderItem ( string key , NTexture texture , AssetHandle assetHandle = default )
4022 {
41- // 移动到最近使用的位置
42- lruList . Remove ( item ) ;
43- lruList . AddFirst ( item ) ;
44- return item . texture ;
23+ Key = key ;
24+ Texture = texture ;
25+ AssetHandle = assetHandle ;
4526 }
46-
47- return null ; // 纹理未找到
4827 }
4928
50- public void Put ( string key , NTexture texture )
51- {
52- if ( key == null )
53- {
54- return ;
55- }
29+ private readonly Dictionary < string , LoaderItem > _cache = new Dictionary < string , LoaderItem > ( ) ;
5630
57- if ( cache . ContainsKey ( key ) )
58- {
59- // 更新已有项并移动到最近使用位置
60- CacheItem existingItem = cache [ key ] ;
61- lruList . Remove ( existingItem ) ;
62- existingItem . texture = texture ; // 更新纹理
63- lruList . AddFirst ( existingItem ) ;
64- }
65- else
66- {
67- // 如果超过最大数量,则移除最少使用的项
68- if ( cache . Count >= maxSize )
69- {
70- RemoveLeastRecentlyUsed ( ) ;
71- }
72-
73- // 添加新项
74- CacheItem newItem = new CacheItem ( key , texture ) ;
75- cache [ key ] = newItem ;
76- lruList . AddFirst ( newItem ) ;
77- }
78- }
79-
80- private void RemoveLeastRecentlyUsed ( )
31+ public FairyGuiExtensionLoader ( )
8132 {
82- if ( lruList . Count > 0 )
33+ _cachePath = PathHelper . AppHotfixResPath + "/cache/images/" ;
34+ if ( ! Directory . Exists ( _cachePath ) )
8335 {
84- CacheItem leastUsedItem = lruList . Last . Value ;
85- lruList . RemoveLast ( ) ;
86- cache . Remove ( leastUsedItem . key ) ;
87- leastUsedItem . texture . Dispose ( ) ;
88- if ( leastUsedItem . texture . nativeTexture . IsNotNull ( ) )
89- {
90- Object . Destroy ( leastUsedItem . texture . nativeTexture ) ; // 释放纹理资源
91- }
92-
93- if ( leastUsedItem . texture . alphaTexture . IsNotNull ( ) )
94- {
95- Object . Destroy ( leastUsedItem . texture . alphaTexture ) ; // 释放纹理资源
96- }
36+ Directory . CreateDirectory ( _cachePath ) ;
9737 }
9838 }
9939
100- public void Clear ( )
40+ protected override void FreeExternal ( NTexture nTexture )
10141 {
102- foreach ( var item in lruList )
42+ foreach ( var loaderItem in _cache )
10343 {
104- item . texture . Dispose ( ) ;
105- if ( item . texture . nativeTexture . IsNotNull ( ) )
44+ if ( loaderItem . Value . Texture != nTexture )
10645 {
107- Object . Destroy ( item . texture . nativeTexture ) ; // 释放纹理资源
46+ continue ;
10847 }
10948
110- if ( item . texture . alphaTexture . IsNotNull ( ) )
111- {
112- Object . Destroy ( item . texture . alphaTexture ) ; // 释放纹理资源
113- }
49+ loaderItem . Value . AssetHandle ? . Release ( ) ;
50+ break ;
11451 }
11552
116- cache . Clear ( ) ;
117- lruList . Clear ( ) ;
118- }
119- }
120-
121-
122- public sealed class FairyGuiExtensionLoader : GLoader
123- {
124- private static LRUCache cache = new LRUCache ( 100 ) ;
125- private static string _cachePath ;
126-
127- public FairyGuiExtensionLoader ( )
128- {
129- _cachePath = PathHelper . AppHotfixResPath + "/cache/images/" ;
130- if ( ! Directory . Exists ( _cachePath ) )
131- {
132- Directory . CreateDirectory ( _cachePath ) ;
133- }
53+ base . FreeExternal ( nTexture ) ;
13454 }
13555
13656 protected override async void LoadExternal ( )
@@ -144,69 +64,60 @@ protected override async void LoadExternal()
14464 NTexture tempTexture = null ;
14565 if ( url . StartsWithFast ( "http://" ) || url . StartsWithFast ( "https://" ) )
14666 {
147- // 网络资源
148- var nTexture = cache . Get ( url ) ;
149- if ( nTexture . IsNull ( ) )
150- {
151- var hash = Utility . Hash . MD5 . Hash ( url ) ;
67+ var hash = Utility . Hash . MD5 . Hash ( url ) ;
15268
153- var path = $ "{ _cachePath } { hash } .png";
154- var isExists = FileHelper . IsExists ( path ) ;
155- var texture2D = Texture2D . whiteTexture ;
156- if ( isExists )
157- {
158- var buffer = FileHelper . ReadAllBytes ( path ) ;
159- texture2D . LoadImage ( buffer ) ;
160- }
161- else
69+ var path = $ "{ _cachePath } { hash } { Utility . Const . FileNameSuffix . PNG } ";
70+ var isExists = FileHelper . IsExists ( path ) ;
71+ var texture2D = new Texture2D ( 1 , 1 , TextureFormat . RGBA32 , false , false ) ;
72+ if ( isExists )
73+ {
74+ var buffer = FileHelper . ReadAllBytes ( path ) ;
75+ if ( texture2D . LoadImage ( buffer ) )
16276 {
163- var webBufferResult = await GameApp . Web . GetToBytes ( url , null ) ;
164- FileHelper . WriteAllBytes ( path , webBufferResult . Result ) ;
165- texture2D . LoadImage ( webBufferResult . Result ) ;
77+ Log . Debug ( "加载成功" + url + "\n " + path ) ;
16678 }
167-
168- tempTexture = new NTexture ( texture2D ) ;
169- cache . Put ( url , tempTexture ) ;
17079 }
17180 else
17281 {
173- tempTexture = nTexture ;
82+ var webBufferResult = await GameApp . Download . Download ( path , url ) ;
83+ if ( webBufferResult )
84+ {
85+ var buffer = FileHelper . ReadAllBytes ( path ) ;
86+ if ( texture2D . LoadImage ( buffer ) )
87+ {
88+ // Log.Debug("加载成功" + url + "\n " + path);
89+ }
90+ }
17491 }
175- }
176- else if ( url . StartsWithFast ( "ui://" ) )
177- {
178- // 包内资源
179- LoadContent ( ) ;
92+
93+ tempTexture = new NTexture ( texture2D ) ;
94+ _cache [ url ] = new LoaderItem ( url , tempTexture ) ;
18095 }
18196 else
18297 {
183- var nTexture = cache . Get ( url ) ;
184- if ( nTexture . IsNotNull ( ) )
98+ var assetInfo = GameApp . Asset . GetAssetInfo ( url ) ;
99+ if ( assetInfo != null )
185100 {
186- tempTexture = nTexture ;
101+ // 包内文件
102+ var assetHandle = await GameApp . Asset . LoadAssetAsync < Texture2D > ( url ) ;
103+ if ( assetHandle . IsSucceed ( ) )
104+ {
105+ tempTexture = new NTexture ( assetHandle . GetAssetObject < Texture2D > ( ) ) ;
106+ _cache [ url ] = new LoaderItem ( url , tempTexture , assetHandle ) ;
107+ // Cache.Put(url, tempTexture);
108+ }
187109 }
188110 else
189111 {
190- var assetInfo = GameApp . Asset . GetAssetInfo ( url ) ;
191- if ( assetInfo . IsInvalid == false )
112+ if ( FileHelper . IsExists ( url ) )
192113 {
193- var assetHandle = await GameApp . Asset . LoadAssetAsync < Texture2D > ( url ) ;
194- if ( assetHandle . IsSucceed )
114+ // 本地文件
115+ var buffer = FileHelper . ReadAllBytes ( url ) ;
116+ var texture2D = new Texture2D ( 1 , 1 , TextureFormat . ARGB32 , false , false ) ;
117+ if ( texture2D . LoadImage ( buffer ) )
195118 {
196- tempTexture = new NTexture ( assetHandle . GetAssetObject < Texture2D > ( ) ) ;
197- cache . Put ( url , tempTexture ) ;
198- }
199- }
200- else
201- {
202- if ( FileHelper . IsExists ( url ) )
203- {
204- var buffer = FileHelper . ReadAllBytes ( url ) ;
205-
206- var texture2D = new Texture2D ( Screen . width , Screen . height , TextureFormat . ARGB32 , false , false ) ;
207- texture2D . LoadImage ( buffer ) ;
208119 tempTexture = new NTexture ( texture2D ) ;
209- cache . Put ( url , tempTexture ) ;
120+ _cache [ url ] = new LoaderItem ( url , tempTexture ) ;
210121 }
211122 }
212123 }
0 commit comments