Skip to content

Commit 5a48244

Browse files
committed
Explicitly decide on an order when loading non-matching textures
The new order is now: - choose by priority, then, - pick the texture that matches the size, otherwise, - pick the unsized texture if available, otherwise, - pick the largest texture.
1 parent 9dcab1a commit 5a48244

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

src/HUDReplacer/HUDReplacer.cs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using UnityEngine;
77
using UnityEngine.EventSystems;
8+
using UnityEngine.Rendering;
89
using UnityEngine.SceneManagement;
910
using UnityEngine.UI;
1011

@@ -41,30 +42,53 @@ public SizedReplacementInfo GetMatchingReplacement(Texture2D tex)
4142
if (replacements.Count == 0)
4243
return null;
4344

44-
SizedReplacementInfo replacement = null;
45+
var sized = GetSizedReplacement(tex);
46+
var unsized = GetUnsizedReplacement(tex);
4547

46-
// Try to find a texture replacement with matching dimensions.
48+
if (sized is not null)
49+
{
50+
// If priorities are equal then prefer the sized texture
51+
if (unsized.priority <= sized.priority)
52+
return sized;
53+
}
54+
55+
return unsized;
56+
}
57+
58+
SizedReplacementInfo GetSizedReplacement(Texture2D tex)
59+
{
4760
foreach (var info in replacements)
4861
{
4962
if (info.width == tex.width && info.height == tex.height)
63+
return info;
64+
}
65+
66+
return null;
67+
}
68+
69+
SizedReplacementInfo GetUnsizedReplacement(Texture2D tex)
70+
{
71+
SizedReplacementInfo found = replacements[0];
72+
73+
foreach (var info in replacements)
74+
{
75+
if (info.priority < found.priority)
76+
break;
77+
78+
// Prefer textures without a specific size if we have one
79+
// available.
80+
if (info.width == 0 && info.height == 0)
5081
{
51-
replacement = info;
82+
found = info;
5283
break;
5384
}
54-
}
55-
56-
var unsized = replacements[0];
5785

58-
// If no matching sized replacements just return the highest
59-
// priority replacement.
60-
if (replacement is null)
61-
return unsized;
86+
// Otherwise use the biggest texture we have
87+
if (info.width > found.width && info.height > found.height)
88+
found = info;
89+
}
6290

63-
// Prefer the wrong-sized texture if it is higher priority, but
64-
// otherwise use the replacement.
65-
if (unsized.priority > replacement.priority)
66-
return unsized;
67-
return replacement;
91+
return found;
6892
}
6993
}
7094

0 commit comments

Comments
 (0)