You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enhance IFuture interface to implement IDisposable; add DisposeOn methods for reference management and update sample scripts for improved usage examples
// Clear memory Memory and Disk cache for all images
352
365
ImageLoader.ClearCacheAll();
353
366
367
+
// Clear only Memory and Disk cache for specific image
368
+
ImageLoader.ClearCache(url);
369
+
354
370
// Clear only Memory cache for all images
355
371
ImageLoader.ClearMemoryCacheAll();
356
372
@@ -368,40 +384,71 @@ Memory cache could be cleared automatically if to use `Reference<T>` and the hea
368
384
369
385
## Texture Memory Management
370
386
371
-
ImageLoader can manager memory usage of loaded textures. To use it need to call `ImageLoader.LoadSpriteRef` instead of `ImageLoader.LoadSprite`. It will return `Reference<Sprite>` object which contains `Sprite` and `Url` objects. When `Reference<Sprite>` object is not needed anymore, call `Dispose` method to release memory, or just don't save the reference on it. It is `IDisposable` and it will clean itself automatically. Each new instance of `Reference<Sprite>` increments reference counter of the texture. When the last reference is disposed, the texture will be unloaded from memory. Also the all related References will be automatically disposed if you call `ImageLoader.ClearMemoryCache` or `ImageLoader.ClearCache`.
387
+
Texture2D objects consume a lot of memory. Ignoring it may impact performance or even trigger `OutOfMemory` crash by operation system. To avoid it, let's dig deeper into tools the package provides. We worry less about Disk cache, because it doesn't impact game performance directly. Let's focus on the Memory cache.
388
+
389
+
### Manual Memory cache cleaning
390
+
391
+
It is simple, just executing this line of code would release memory of a single Texture2D in the case if no other `Reference` pointing on it exists. Before doing that please make sure that no Unity component is using the texture.
392
+
393
+
```C#
394
+
ImageLoader.ClearMemoryCache(url);
395
+
```
396
+
397
+
Under the hood it calls `UnityEngine.Object.DestroyImmediate(texture)`.
398
+
399
+
> :warning: Releasing `Texture2D` from memory while any Unity's component uses it may trigger native app crash or even Unity Editor crash
400
+
401
+
### Automatic Memory cache cleaning
402
+
403
+
ImageLoader can manager memory releasing of loaded textures. To use it need to call `ImageLoader.LoadSpriteRef` instead of `ImageLoader.LoadSprite`. It returns `Reference<Sprite>` object which contains `Sprite` and `Url`. When `Reference<Sprite>` object is not needed anymore, call `reference.Dispose()` method to release memory, or just don't save the reference on it. It is `IDisposable` and it will be disposed by Garbage Collector. Each new instance of `Reference<Sprite>` increments reference counter of the texture. When the last reference is disposed, the texture memory releases. Also, if any reference is alive, calling `ImageLoader.ClearMemoryCache` or `ImageLoader.ClearCache` would have zero effect for only referenced textures. It prints warning messages about it.
// Dispose `reference` when you don't need the texture anymore
413
+
reference.Dispose();
414
+
415
+
// You may also nullify the reference to let Garbage Collector at some point to Dispose it for you
416
+
reference=null;
379
417
```
380
418
381
419
> :warning: Releasing `Texture2D` from memory while any Unity's component uses it may trigger native app crash or even Unity Editor crash. Please pay enough attention to manage `Reference<T>` instances in a proper way. Or do not use them.
`Reference<T>.ThenSet` has a unique feature to attach the reference to the target consumer if consumer is `UnityEngine.Component`. The reference would be disposed as only the consumer gets destroyed.
426
+
387
427
```C#
388
-
____________________________
428
+
ImageLoader.LoadSpriteRef(imageURL) // load sprite using Reference
429
+
.ThenSet(image) // if success set sprite into image, also creates binding to `image`
430
+
.Forget();
389
431
```
390
432
391
-
###____________________
433
+
#### Dispose `Reference<T>` on `Component` destroy event
0 commit comments