Skip to content

Commit 9b5ec37

Browse files
committed
Add dictionary extension methods and replace ConcurrentDictionary with Dictionary in ImageLoader for older Unity version support
1 parent a0d6f15 commit 9b5ec37

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Extensions.Unity.ImageLoader
5+
{
6+
static class Extensions
7+
{
8+
public static void AddOrUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
9+
{
10+
lock (dictionary)
11+
{
12+
if (dictionary.TryGetValue(key, out var existingValue))
13+
{
14+
dictionary[key] = updateValueFactory(key, existingValue);
15+
}
16+
else
17+
{
18+
dictionary[key] = addValue;
19+
}
20+
}
21+
}
22+
}
23+
}

Assets/_PackageRoot/Runtime/Extensions.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/_PackageRoot/Runtime/ImageLoader.MemoryCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Extensions.Unity.ImageLoader
77
{
88
public static partial class ImageLoader
99
{
10-
internal static volatile ConcurrentDictionary<string, Sprite> memorySpriteCache = new ConcurrentDictionary<string, Sprite>();
10+
internal static volatile Dictionary<string, Sprite> memorySpriteCache = new Dictionary<string, Sprite>();
1111

1212
#if UNITY_EDITOR
1313
[UnityEditor.InitializeOnEnterPlayMode]

Assets/_PackageRoot/Runtime/Reference.Counter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using System;
2-
using System.Collections.Concurrent;
32
using System.Collections.Generic;
43

54
namespace Extensions.Unity.ImageLoader
65
{
76
public partial class Reference<T> : IDisposable
87
{
9-
private volatile static ConcurrentDictionary<string, int> referenceCounters = new ConcurrentDictionary<string, int>();
8+
private volatile static Dictionary<string, int> referenceCounters = new Dictionary<string, int>();
109
internal static void Clear()
1110
{
1211
lock (referenceCounters) referenceCounters.Clear();
@@ -15,7 +14,7 @@ internal static void Clear()
1514
internal static bool Clear(string url)
1615
{
1716
var result = false;
18-
lock (referenceCounters) result = referenceCounters.Remove(url, out var _);
17+
lock (referenceCounters) result = referenceCounters.Remove(url);
1918
EventOnClearUrl?.Invoke(url);
2019
return result;
2120
}

0 commit comments

Comments
 (0)