com.wallstop-studios.unity-helpers 1.0.0-rc9 → 1.0.1-rc02
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Runtime/Core/Extension/DictionaryExtensions.cs +71 -34
- package/Runtime/Utils/SpriteRendererMetadata.cs +273 -0
- package/Runtime/Utils/SpriteRendererMetadata.cs.meta +3 -0
- package/Runtime/Utils/SpriteRendererSyncer.cs +98 -0
- package/Runtime/Utils/SpriteRendererSyncer.cs.meta +3 -0
- package/Tests/Runtime/Extensions/DictionaryExtensionTests.cs +419 -0
- package/Tests/Runtime/Extensions/DictionaryExtensionTests.cs.meta +3 -0
- package/Tests/Runtime/Extensions/StringExtensionTests.cs +1 -1
- package/Tests/Runtime/Utils/SpriteRendererMetadataTests.cs +395 -0
- package/Tests/Runtime/Utils/SpriteRendererMetadataTests.cs.meta +3 -0
- package/Tests/Runtime/Utils.meta +3 -0
- package/package.json +2 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
namespace UnityHelpers.Core.Extension
|
|
2
2
|
{
|
|
3
3
|
using System;
|
|
4
|
+
using System.Collections.Concurrent;
|
|
4
5
|
using System.Collections.Generic;
|
|
5
6
|
using System.Linq;
|
|
6
7
|
|
|
@@ -8,6 +9,11 @@
|
|
|
8
9
|
{
|
|
9
10
|
public static V GetOrAdd<K, V>(this IDictionary<K, V> dictionary, K key, Func<V> valueProducer)
|
|
10
11
|
{
|
|
12
|
+
if (dictionary is ConcurrentDictionary<K, V> concurrentDictionary)
|
|
13
|
+
{
|
|
14
|
+
return concurrentDictionary.GetOrAdd(key, static (_, existing) => existing(), valueProducer);
|
|
15
|
+
}
|
|
16
|
+
|
|
11
17
|
if (dictionary.TryGetValue(key, out V result))
|
|
12
18
|
{
|
|
13
19
|
return result;
|
|
@@ -15,8 +21,14 @@
|
|
|
15
21
|
|
|
16
22
|
return dictionary[key] = valueProducer();
|
|
17
23
|
}
|
|
24
|
+
|
|
18
25
|
public static V GetOrAdd<K, V>(this IDictionary<K, V> dictionary, K key, Func<K, V> valueProducer)
|
|
19
26
|
{
|
|
27
|
+
if (dictionary is ConcurrentDictionary<K, V> concurrentDictionary)
|
|
28
|
+
{
|
|
29
|
+
return concurrentDictionary.GetOrAdd(key, valueProducer);
|
|
30
|
+
}
|
|
31
|
+
|
|
20
32
|
if (dictionary.TryGetValue(key, out V result))
|
|
21
33
|
{
|
|
22
34
|
return result;
|
|
@@ -25,16 +37,17 @@
|
|
|
25
37
|
return dictionary[key] = valueProducer(key);
|
|
26
38
|
}
|
|
27
39
|
|
|
28
|
-
public static V GetOrElse<K, V>(this
|
|
40
|
+
public static V GetOrElse<K, V>(this IReadOnlyDictionary<K, V> dictionary, K key, Func<V> valueProducer)
|
|
29
41
|
{
|
|
30
42
|
if (dictionary.TryGetValue(key, out V value))
|
|
31
43
|
{
|
|
32
44
|
return value;
|
|
33
45
|
}
|
|
46
|
+
|
|
34
47
|
return valueProducer.Invoke();
|
|
35
48
|
}
|
|
36
49
|
|
|
37
|
-
public static V GetOrElse<K, V>(this
|
|
50
|
+
public static V GetOrElse<K, V>(this IReadOnlyDictionary<K, V> dictionary, K key, Func<K, V> valueProducer)
|
|
38
51
|
{
|
|
39
52
|
if (dictionary.TryGetValue(key, out V value))
|
|
40
53
|
{
|
|
@@ -46,6 +59,11 @@
|
|
|
46
59
|
|
|
47
60
|
public static V GetOrAdd<K, V>(this IDictionary<K, V> dictionary, K key) where V : new()
|
|
48
61
|
{
|
|
62
|
+
if (dictionary is ConcurrentDictionary<K, V> concurrentDictionary)
|
|
63
|
+
{
|
|
64
|
+
return concurrentDictionary.AddOrUpdate(key, _ => new V(), (_, existing) => existing);
|
|
65
|
+
}
|
|
66
|
+
|
|
49
67
|
if (dictionary.TryGetValue(key, out V result))
|
|
50
68
|
{
|
|
51
69
|
return result;
|
|
@@ -54,43 +72,58 @@
|
|
|
54
72
|
return dictionary[key] = new V();
|
|
55
73
|
}
|
|
56
74
|
|
|
57
|
-
public static V GetOrElse<K, V>(this
|
|
75
|
+
public static V GetOrElse<K, V>(this IReadOnlyDictionary<K, V> dictionary, K key, V value)
|
|
58
76
|
{
|
|
59
77
|
return GetOrElse(dictionary, key, () => value);
|
|
60
78
|
}
|
|
61
79
|
|
|
62
|
-
public static
|
|
80
|
+
public static V AddOrUpdate<K, V>(
|
|
81
|
+
this IDictionary<K, V> dictionary, K key, Func<K, V> creator, Func<K, V, V> updater)
|
|
63
82
|
{
|
|
64
|
-
if (dictionary
|
|
83
|
+
if (dictionary is ConcurrentDictionary<K, V> concurrentDictionary)
|
|
65
84
|
{
|
|
66
|
-
return
|
|
85
|
+
return concurrentDictionary.AddOrUpdate(key, creator, updater);
|
|
67
86
|
}
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
87
|
|
|
71
|
-
public static V AddOrUpdate<K, V>(this IDictionary<K, V> dictionary, K key, Func<K, V> creator, Func<K, V, V> updater)
|
|
72
|
-
{
|
|
73
88
|
V latest = dictionary.TryGetValue(key, out V value) ? updater(key, value) : creator(key);
|
|
74
89
|
dictionary[key] = latest;
|
|
75
90
|
return latest;
|
|
76
91
|
}
|
|
77
92
|
|
|
78
|
-
public static
|
|
93
|
+
public static V TryAdd<K, V>(this IDictionary<K, V> dictionary, K key, Func<K, V> creator)
|
|
94
|
+
{
|
|
95
|
+
if (dictionary is ConcurrentDictionary<K, V> concurrentDictionary)
|
|
96
|
+
{
|
|
97
|
+
return concurrentDictionary.AddOrUpdate(key, creator, (_, existing) => existing);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (dictionary.TryGetValue(key, out V existing))
|
|
101
|
+
{
|
|
102
|
+
return existing;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
V value = creator(key);
|
|
106
|
+
dictionary[key] = value;
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public static Dictionary<K, V> Merge<K, V>(this IReadOnlyDictionary<K, V> lhs, IReadOnlyDictionary<K, V> rhs)
|
|
79
111
|
{
|
|
80
|
-
Dictionary<K, V> result = new
|
|
81
|
-
|
|
112
|
+
Dictionary<K, V> result = new();
|
|
113
|
+
if (0 < lhs.Count)
|
|
82
114
|
{
|
|
83
|
-
|
|
115
|
+
foreach (KeyValuePair<K, V> kvp in lhs)
|
|
84
116
|
{
|
|
85
|
-
|
|
117
|
+
result[kvp.Key] = kvp.Value;
|
|
86
118
|
}
|
|
87
|
-
|
|
88
|
-
result[kvp.Key] = kvp.Value;
|
|
89
119
|
}
|
|
90
120
|
|
|
91
|
-
|
|
121
|
+
if (0 < rhs.Count)
|
|
92
122
|
{
|
|
93
|
-
|
|
123
|
+
foreach (KeyValuePair<K, V> kvp in rhs)
|
|
124
|
+
{
|
|
125
|
+
result[kvp.Key] = kvp.Value;
|
|
126
|
+
}
|
|
94
127
|
}
|
|
95
128
|
|
|
96
129
|
return result;
|
|
@@ -103,38 +136,36 @@
|
|
|
103
136
|
/// <param name="lhs">Basis dictionary.</param>
|
|
104
137
|
/// <param name="rhs">Changed dictionary.</param>
|
|
105
138
|
/// <returns>All elements of rhs that either don't exist in or are different from lhs</returns>
|
|
106
|
-
public static Dictionary<K, V> Difference<K, V>(
|
|
139
|
+
public static Dictionary<K, V> Difference<K, V>(
|
|
140
|
+
this IReadOnlyDictionary<K, V> lhs, IReadOnlyDictionary<K, V> rhs)
|
|
107
141
|
{
|
|
108
|
-
Dictionary<K, V> result = new
|
|
142
|
+
Dictionary<K, V> result = new(rhs.Count);
|
|
109
143
|
foreach (KeyValuePair<K, V> kvp in rhs)
|
|
110
144
|
{
|
|
111
145
|
K key = kvp.Key;
|
|
112
|
-
V existing
|
|
113
|
-
if (lhs.TryGetValue(key, out existing))
|
|
146
|
+
if (lhs.TryGetValue(key, out V existing) && Equals(existing, kvp.Value))
|
|
114
147
|
{
|
|
115
|
-
|
|
116
|
-
{
|
|
117
|
-
continue;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
result[key] = kvp.Value;
|
|
148
|
+
continue;
|
|
121
149
|
}
|
|
150
|
+
|
|
151
|
+
result[key] = kvp.Value;
|
|
122
152
|
}
|
|
123
153
|
|
|
124
154
|
return result;
|
|
125
155
|
}
|
|
126
156
|
|
|
127
|
-
public static Dictionary<V, K> Reverse<K, V>(this
|
|
157
|
+
public static Dictionary<V, K> Reverse<K, V>(this IReadOnlyDictionary<K, V> dictionary)
|
|
128
158
|
{
|
|
129
|
-
Dictionary<V, K> output = new
|
|
159
|
+
Dictionary<V, K> output = new(dictionary.Count);
|
|
130
160
|
foreach (KeyValuePair<K, V> entry in dictionary)
|
|
131
161
|
{
|
|
132
162
|
output[entry.Value] = entry.Key;
|
|
133
163
|
}
|
|
164
|
+
|
|
134
165
|
return output;
|
|
135
166
|
}
|
|
136
167
|
|
|
137
|
-
public static Dictionary<K, V> ToDictionary<K, V>(this
|
|
168
|
+
public static Dictionary<K, V> ToDictionary<K, V>(this IReadOnlyDictionary<K, V> dictionary)
|
|
138
169
|
{
|
|
139
170
|
return new Dictionary<K, V>(dictionary);
|
|
140
171
|
}
|
|
@@ -144,7 +175,13 @@
|
|
|
144
175
|
return prettyMuchADictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
|
|
145
176
|
}
|
|
146
177
|
|
|
147
|
-
public static
|
|
178
|
+
public static Dictionary<K, V> ToDictionary<K, V>(this IEnumerable<(K, V)> prettyMuchADictionary)
|
|
179
|
+
{
|
|
180
|
+
return prettyMuchADictionary.ToDictionary(kvp => kvp.Item1, kvp => kvp.Item2);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
public static bool ContentEquals<K, V>(
|
|
184
|
+
this IReadOnlyDictionary<K, V> dictionary, IReadOnlyDictionary<K, V> other)
|
|
148
185
|
{
|
|
149
186
|
if (ReferenceEquals(dictionary, other))
|
|
150
187
|
{
|
|
@@ -170,4 +207,4 @@
|
|
|
170
207
|
value = kvp.Value;
|
|
171
208
|
}
|
|
172
209
|
}
|
|
173
|
-
}
|
|
210
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
namespace UnityHelpers.Utils
|
|
2
|
+
{
|
|
3
|
+
using System.Collections.Generic;
|
|
4
|
+
using System.Linq;
|
|
5
|
+
using UnityEngine;
|
|
6
|
+
using Core.Attributes;
|
|
7
|
+
|
|
8
|
+
/// <summary>
|
|
9
|
+
/// Keeps stack-like track of Colors and Materials of SpriteRenderers
|
|
10
|
+
/// </summary>
|
|
11
|
+
[DisallowMultipleComponent]
|
|
12
|
+
public sealed class SpriteRendererMetadata : MonoBehaviour
|
|
13
|
+
{
|
|
14
|
+
private readonly List<(Component component, Color color)> _colorStack = new();
|
|
15
|
+
private readonly List<(Component component, Material material)> _materialStack = new();
|
|
16
|
+
|
|
17
|
+
private readonly List<(Component component, Color color)> _colorStackCache = new();
|
|
18
|
+
private readonly List<(Component component, Material material)> _materialStackCache = new();
|
|
19
|
+
|
|
20
|
+
public Color OriginalColor => _colorStack[0].color;
|
|
21
|
+
|
|
22
|
+
public Color CurrentColor => _colorStack[^1].color;
|
|
23
|
+
|
|
24
|
+
public Material OriginalMaterial => _materialStack[0].material;
|
|
25
|
+
|
|
26
|
+
public Material CurrentMaterial => _materialStack[^1].material;
|
|
27
|
+
|
|
28
|
+
public IEnumerable<Material> Materials => _materialStack.Select(entry => entry.material);
|
|
29
|
+
|
|
30
|
+
public IEnumerable<Color> Colors => _colorStack.Select(entry => entry.color);
|
|
31
|
+
|
|
32
|
+
[SiblingComponent]
|
|
33
|
+
[SerializeField]
|
|
34
|
+
private SpriteRenderer _spriteRenderer;
|
|
35
|
+
|
|
36
|
+
private bool _enabled;
|
|
37
|
+
|
|
38
|
+
public void PushColor(Component component, Color color, bool force = false)
|
|
39
|
+
{
|
|
40
|
+
if (component == this)
|
|
41
|
+
{
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!force && !enabled)
|
|
46
|
+
{
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
InternalPushColor(component, color);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private void InternalPushColor(Component component, Color color)
|
|
54
|
+
{
|
|
55
|
+
RemoveColor(component);
|
|
56
|
+
_colorStack.Add((component, color));
|
|
57
|
+
_spriteRenderer.color = CurrentColor;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public void PushBackColor(Component component, Color color, bool force = false)
|
|
61
|
+
{
|
|
62
|
+
if (component == this)
|
|
63
|
+
{
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!force && !enabled)
|
|
68
|
+
{
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
RemoveColor(component);
|
|
73
|
+
_colorStack.Insert(1, (component, color));
|
|
74
|
+
_spriteRenderer.color = CurrentColor;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public void PopColor(Component component)
|
|
78
|
+
{
|
|
79
|
+
RemoveColor(component);
|
|
80
|
+
_spriteRenderer.color = CurrentColor;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// <summary>
|
|
84
|
+
/// Inserts a material as "first in the queue".
|
|
85
|
+
/// </summary>
|
|
86
|
+
/// <param name="component">Component that owns the material.</param>
|
|
87
|
+
/// <param name="material">Material to use.</param>
|
|
88
|
+
/// <param name="force">If true, overrides the enable check.</param>
|
|
89
|
+
/// <returns>The instanced material, if possible.</returns>
|
|
90
|
+
public Material PushMaterial(Component component, Material material, bool force = false)
|
|
91
|
+
{
|
|
92
|
+
if (component == this)
|
|
93
|
+
{
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (!force && !enabled)
|
|
98
|
+
{
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
#if UNITY_EDITOR
|
|
103
|
+
if (!Application.isPlaying)
|
|
104
|
+
{
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
#endif
|
|
108
|
+
return InternalPushMaterial(component, material);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private Material InternalPushMaterial(Component component, Material material)
|
|
112
|
+
{
|
|
113
|
+
RemoveMaterial(component);
|
|
114
|
+
_spriteRenderer.material = material;
|
|
115
|
+
Material instanced = _spriteRenderer.material;
|
|
116
|
+
_materialStack.Add((component, instanced));
|
|
117
|
+
return instanced;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/// <summary>
|
|
121
|
+
/// Inserts a material as "last in the queue".
|
|
122
|
+
/// </summary>
|
|
123
|
+
/// <param name="component">Component that owns the material.</param>
|
|
124
|
+
/// <param name="material">Material to use.</param>
|
|
125
|
+
/// <param name="force">If true, overrides the enable check.</param>
|
|
126
|
+
/// <returns>The instanced material, if possible.</returns>
|
|
127
|
+
public Material PushBackMaterial(Component component, Material material, bool force = false)
|
|
128
|
+
{
|
|
129
|
+
if (component == this)
|
|
130
|
+
{
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (!force && !enabled)
|
|
135
|
+
{
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
#if UNITY_EDITOR
|
|
140
|
+
if (!Application.isPlaying)
|
|
141
|
+
{
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
#endif
|
|
145
|
+
|
|
146
|
+
RemoveMaterial(component);
|
|
147
|
+
Material instanced = material;
|
|
148
|
+
if (_materialStack.Count <= 1)
|
|
149
|
+
{
|
|
150
|
+
_spriteRenderer.material = material;
|
|
151
|
+
instanced = _spriteRenderer.material;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
_materialStack.Insert(1, (component, instanced));
|
|
155
|
+
return instanced;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public void PopMaterial(Component component)
|
|
159
|
+
{
|
|
160
|
+
#if UNITY_EDITOR
|
|
161
|
+
if (!Application.isPlaying)
|
|
162
|
+
{
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
#endif
|
|
166
|
+
|
|
167
|
+
RemoveMaterial(component);
|
|
168
|
+
_spriteRenderer.material = CurrentMaterial;
|
|
169
|
+
Material instanced = _spriteRenderer.material;
|
|
170
|
+
Component currentComponent = _materialStack[^1].component;
|
|
171
|
+
_materialStack[^1] = (currentComponent, instanced);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private void Awake()
|
|
175
|
+
{
|
|
176
|
+
if (_spriteRenderer == null)
|
|
177
|
+
{
|
|
178
|
+
this.AssignSiblingComponents();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
InternalPushColor(this, _spriteRenderer.color);
|
|
182
|
+
_colorStackCache.AddRange(_colorStack);
|
|
183
|
+
_ = InternalPushMaterial(this, _spriteRenderer.material);
|
|
184
|
+
_materialStackCache.AddRange(_materialStack);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private void OnEnable()
|
|
188
|
+
{
|
|
189
|
+
// Ignore the OnEnable call from when the object is first initialized
|
|
190
|
+
if (!_enabled)
|
|
191
|
+
{
|
|
192
|
+
_enabled = true;
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
_colorStack.Clear();
|
|
197
|
+
_colorStack.Add(_colorStackCache[0]);
|
|
198
|
+
List<(Component component, Color color)> colorBuffer = Buffers<(Component component, Color color)>.List;
|
|
199
|
+
colorBuffer.Clear();
|
|
200
|
+
colorBuffer.AddRange(_colorStackCache);
|
|
201
|
+
for (int i = 1; i < colorBuffer.Count; ++i)
|
|
202
|
+
{
|
|
203
|
+
(Component component, Color color) entry = colorBuffer[i];
|
|
204
|
+
PushColor(entry.component, entry.color, force: true);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
_materialStack.Clear();
|
|
208
|
+
_materialStack.Add(_materialStackCache[0]);
|
|
209
|
+
List<(Component component, Material material)> materialBuffer =
|
|
210
|
+
Buffers<(Component component, Material material)>.List;
|
|
211
|
+
materialBuffer.Clear();
|
|
212
|
+
materialBuffer.AddRange(_materialStackCache);
|
|
213
|
+
for (int i = 1; i < materialBuffer.Count; ++i)
|
|
214
|
+
{
|
|
215
|
+
(Component component, Material material) entry = materialBuffer[i];
|
|
216
|
+
PushMaterial(entry.component, entry.material, force: true);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private void OnDisable()
|
|
221
|
+
{
|
|
222
|
+
List<(Component component, Color color)> colorBuffer = Buffers<(Component component, Color color)>.List;
|
|
223
|
+
colorBuffer.Clear();
|
|
224
|
+
colorBuffer.AddRange(_colorStack);
|
|
225
|
+
for (int i = colorBuffer.Count - 1; 1 <= i; --i)
|
|
226
|
+
{
|
|
227
|
+
PopColor(colorBuffer[i].component);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
_colorStackCache.Clear();
|
|
231
|
+
_colorStackCache.AddRange(colorBuffer);
|
|
232
|
+
|
|
233
|
+
List<(Component component, Material material)> materialBuffer =
|
|
234
|
+
Buffers<(Component component, Material material)>.List;
|
|
235
|
+
materialBuffer.Clear();
|
|
236
|
+
materialBuffer.AddRange(_materialStack);
|
|
237
|
+
|
|
238
|
+
for (int i = materialBuffer.Count - 1; 1 <= i; --i)
|
|
239
|
+
{
|
|
240
|
+
PopMaterial(materialBuffer[i].component);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
_materialStackCache.Clear();
|
|
244
|
+
_materialStackCache.AddRange(materialBuffer);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
private void RemoveColor(Component component)
|
|
248
|
+
{
|
|
249
|
+
if (component == this)
|
|
250
|
+
{
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
_ = _colorStack.RemoveAll(
|
|
255
|
+
existingComponent => existingComponent.component == component || existingComponent.component == null);
|
|
256
|
+
_ = _colorStackCache.RemoveAll(
|
|
257
|
+
existingComponent => existingComponent.component == component || existingComponent.component == null);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private void RemoveMaterial(Component component)
|
|
261
|
+
{
|
|
262
|
+
if (component == this)
|
|
263
|
+
{
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
_ = _materialStack.RemoveAll(
|
|
268
|
+
existingComponent => existingComponent.component == component || existingComponent.component == null);
|
|
269
|
+
_ = _materialStackCache.RemoveAll(
|
|
270
|
+
existingComponent => existingComponent.component == component || existingComponent.component == null);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
namespace UnityHelpers.Utils
|
|
2
|
+
{
|
|
3
|
+
using System;
|
|
4
|
+
using Core.Attributes;
|
|
5
|
+
using UnityEngine;
|
|
6
|
+
using UnityEngine.Serialization;
|
|
7
|
+
|
|
8
|
+
[DisallowMultipleComponent]
|
|
9
|
+
public sealed class SpriteRendererSync : MonoBehaviour
|
|
10
|
+
{
|
|
11
|
+
public int? DynamicSortingOrderOverride { get; set; }
|
|
12
|
+
|
|
13
|
+
public SpriteRenderer DynamicToMatch
|
|
14
|
+
{
|
|
15
|
+
get => GetDynamicSpriteRenderer();
|
|
16
|
+
set => _cachedSpriteRenderer = value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
[FormerlySerializedAs("_toMatch")]
|
|
20
|
+
public SpriteRenderer toMatch;
|
|
21
|
+
|
|
22
|
+
[FormerlySerializedAs("_matchColor")]
|
|
23
|
+
public bool matchColor;
|
|
24
|
+
|
|
25
|
+
[FormerlySerializedAs("_matchMaterial")]
|
|
26
|
+
public bool matchMaterial;
|
|
27
|
+
|
|
28
|
+
public bool matchSortingLayer = true;
|
|
29
|
+
|
|
30
|
+
public bool matchOrderInLayer = true;
|
|
31
|
+
|
|
32
|
+
public Func<SpriteRenderer> dynamicToMatch;
|
|
33
|
+
|
|
34
|
+
[SerializeField]
|
|
35
|
+
[SiblingComponent]
|
|
36
|
+
private SpriteRenderer _spriteRenderer;
|
|
37
|
+
|
|
38
|
+
private SpriteRenderer _cachedSpriteRenderer;
|
|
39
|
+
|
|
40
|
+
private void Awake()
|
|
41
|
+
{
|
|
42
|
+
if (_spriteRenderer == null)
|
|
43
|
+
{
|
|
44
|
+
this.AssignSiblingComponents();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private void LateUpdate()
|
|
49
|
+
{
|
|
50
|
+
SpriteRenderer localToMatch = dynamicToMatch != null ? GetDynamicSpriteRenderer() : toMatch;
|
|
51
|
+
if (localToMatch == null)
|
|
52
|
+
{
|
|
53
|
+
_spriteRenderer.sprite = null;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
_spriteRenderer.sprite = localToMatch.sprite;
|
|
58
|
+
_spriteRenderer.enabled = localToMatch.enabled;
|
|
59
|
+
_spriteRenderer.flipX = localToMatch.flipX;
|
|
60
|
+
_spriteRenderer.flipY = localToMatch.flipY;
|
|
61
|
+
if (matchColor)
|
|
62
|
+
{
|
|
63
|
+
_spriteRenderer.color = localToMatch.color;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (matchMaterial)
|
|
67
|
+
{
|
|
68
|
+
_spriteRenderer.material = localToMatch.material;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (matchSortingLayer)
|
|
72
|
+
{
|
|
73
|
+
_spriteRenderer.sortingLayerName = localToMatch.sortingLayerName;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (matchOrderInLayer)
|
|
77
|
+
{
|
|
78
|
+
_spriteRenderer.sortingOrder = DynamicSortingOrderOverride ?? localToMatch.sortingOrder;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
_spriteRenderer.size = localToMatch.size;
|
|
82
|
+
_spriteRenderer.spriteSortPoint = localToMatch.spriteSortPoint;
|
|
83
|
+
_spriteRenderer.drawMode = localToMatch.drawMode;
|
|
84
|
+
_spriteRenderer.tileMode = localToMatch.tileMode;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private SpriteRenderer GetDynamicSpriteRenderer()
|
|
88
|
+
{
|
|
89
|
+
if (_cachedSpriteRenderer != null && _cachedSpriteRenderer.gameObject.activeSelf)
|
|
90
|
+
{
|
|
91
|
+
return _cachedSpriteRenderer;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
_cachedSpriteRenderer = dynamicToMatch();
|
|
95
|
+
return _cachedSpriteRenderer;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|