Add BundleSubAssetHolder for packed spritesheets

This commit is contained in:
Atony committed 2026-08-15 13:18:05 -05:00
1 parent 535967475d
commit 6f61a75bb7
4 files changed
+125 -3

No files matched your search

@@ -550,7 +550,6 @@ public static class RoleSettingMenuPatches
new Rect(0, 0, 370, 230),
Vector2.one / 2,
100);
__instance.roleScreenshot.drawMode = SpriteDrawMode.Sliced;
}
__instance.roleHeaderSprite.color = customRole.OptionsMenuColor;
@@ -0,0 +1,37 @@
using System;
using System.Linq;
using UnityEngine;
namespace MiraAPI.Utilities.Assets;
/// <summary>
/// A utility class for loading multiple assets from an <see cref="LoadableBundleSubAssetHolder"/>.
/// </summary>
/// <param name="name">The name of the asset.</param>
/// <param name="assetHolder">The <see cref="LoadableBundleSubAssetHolder"/> that contains the asset.</param>
public class LoadableBundleSubAsset(string name, LoadableBundleSubAssetHolder assetHolder) : LoadableAsset<Sprite>
{
/// <summary>
/// Loads the asset from the <see cref="LoadableBundleSubAssetHolder"/>.
/// </summary>
/// <returns>The asset.</returns>
/// <exception cref="Exception">The asset did not load properly.</exception>
public override Sprite LoadAsset()
{
if (LoadedAsset != null)
{
return LoadedAsset;
}
assetHolder.TryInit();
var loadedAsset = assetHolder.SubSprites.FirstOrDefault(x => x.name == name);
if (loadedAsset == null)
{
throw new InvalidOperationException($"INVALID ASSETS: {name}");
}
LoadedAsset = loadedAsset;
return LoadedAsset;
}
}
@@ -0,0 +1,86 @@
using System;
using System.Linq;
using HarmonyLib;
using Il2CppInterop.Runtime;
using Reactor.Utilities.Extensions;
using UnityEngine;
namespace MiraAPI.Utilities.Assets;
/// <summary>
/// A utility class for loading multiple assets from an <see cref="AssetBundle"/>.
/// </summary>
public class LoadableBundleSubAssetHolder
{
/// <summary>
/// Initializes a new instance of the <see cref="LoadableBundleSubAssetHolder"/> class.
/// </summary>
/// <param name="names">The name of the assets to pull from.</param>
/// <param name="bundle">The <see cref="AssetBundle"/> that contains the assets.</param>
public LoadableBundleSubAssetHolder(string[] names, AssetBundle bundle)
{
Bundle = bundle;
SpriteNames = names;
}
/// <summary>
/// Initializes a new instance of the <see cref="LoadableBundleSubAssetHolder"/> class.
/// </summary>
/// <param name="name">The name of the asset.</param>
/// <param name="bundle">The <see cref="AssetBundle"/> that contains the assets.</param>
public LoadableBundleSubAssetHolder(string name, AssetBundle bundle)
{
Bundle = bundle;
SpriteNames = [name];
}
internal string[] SpriteNames = [];
internal AssetBundle Bundle;
public Sprite[] SubSprites = [];
public virtual void TryInit()
{
if (SubSprites.Length == 0)
{
var newSprites = Array.Empty<Sprite>();
foreach (var name in SpriteNames)
{
var loadedAssets = Bundle.LoadAssetWithSubAssets(name, Il2CppType.From(typeof(Sprite))).ToArray();
if (loadedAssets == null)
{
throw new InvalidOperationException($"INVALID ASSETS: {name}");
}
foreach (var obj in loadedAssets)
{
var img = obj.TryCast<Sprite>();
if (img != null)
{
img.DontDestroy().DontUnload();
newSprites = newSprites.AddToArray(img);
}
}
}
SubSprites = newSprites;
}
}
/// <summary>
/// Unloads an asset.
/// </summary>
/// <returns>True if the asset was unloaded, false otherwise.</returns>
public bool UnloadAsset()
{
if (SubSprites.Length == 0)
{
return false;
}
foreach (var sprite in SubSprites)
{
sprite.DestroyImmediate();
}
SubSprites = [];
return true;
}
}
+2 -2
View File
@@ -71,8 +71,8 @@ public static class TmpSpriteUtils
{
name = spriteName,
hashCode = TMP_TextUtilities.GetSimpleHashCode(spriteName),
x = 0,
y = 0,
x = rect.x,
y = rect.y,
width = rect.width,
height = rect.height,
xOffset = -(rect.width / 3) + (scale - 1) * (rect.width / 2),