avoid over-allocating objects

when reading sprite sheets
This commit is contained in:
Az committed 2026-10-08 00:30:57 -04:00
1 parent a36f010207
commit f5a994abc3
1 file changed
+17 -23
@@ -1,7 +1,5 @@
using System; using System;
using System.Diagnostics.CodeAnalysis; using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using Il2CppInterop.Runtime; using Il2CppInterop.Runtime;
using Reactor.Utilities.Extensions; using Reactor.Utilities.Extensions;
using UnityEngine; using UnityEngine;
@@ -47,35 +45,31 @@ public class LoadableBundleSubAssetHolder
/// Attempts to load all of the sprite assets within the sprite sheet. /// Attempts to load all of the sprite assets within the sprite sheet.
/// </summary> /// </summary>
/// <exception cref="InvalidOperationException">Thrown if the asset is not actually a sprite sheet.</exception> /// <exception cref="InvalidOperationException">Thrown if the asset is not actually a sprite sheet.</exception>
[SuppressMessage("Style", "IDE0270:Use coalesce expression", Justification = "Null coalescing bypasses Unity lifetime checks.")]
public void TryInit() public void TryInit()
{ {
if (SubSprites.Length == 0) if (SubSprites.Length != 0)
return;
var newSprites = new List<Sprite>();
foreach (var name in spriteNames)
{ {
var newSprites = Array.Empty<Sprite>(); var loadedAssets = bundle.LoadAssetWithSubAssets(name, Il2CppType.From(typeof(Sprite)))
?? throw new InvalidOperationException($"INVALID ASSETS: {name}");
foreach (var name in spriteNames) foreach (var obj in loadedAssets)
{ {
var loadedAssets = bundle.LoadAssetWithSubAssets(name, Il2CppType.From(typeof(Sprite))).ToArray(); var img = obj.TryCast<Sprite>();
if (loadedAssets == null) if (img == null)
{ continue;
throw new InvalidOperationException($"INVALID ASSETS: {name}");
}
foreach (var obj in loadedAssets) img.DontDestroy().DontUnload();
{ newSprites.Add(img);
var img = obj.TryCast<Sprite>();
if (img != null)
{
img.DontDestroy().DontUnload();
newSprites = newSprites.AddToArray(img);
}
}
} }
SubSprites = newSprites;
} }
SubSprites = [.. newSprites];
} }
/// <summary> /// <summary>