some Asset related changes and documentation

This commit is contained in:
XtraCube committed 2024-08-19 10:59:07 -04:00
1 parent 3f01429e0f
commit b2350fe264
4 files changed
+52 -8

No files matched your search

+15 -1
View File
@@ -1,8 +1,22 @@
namespace MiraAPI.Utilities.Assets
{
/// <summary>
/// An abstract class that provides a simple pattern for loading assets.
/// Mira uses the <see cref="LoadableAsset{T}"/> pattern in various locations.
/// You can create your own implementation of this class to load assets in different ways.
/// </summary>
/// <typeparam name="T">The type of the asset to be loaded</typeparam>
public abstract class LoadableAsset<T> where T : UnityEngine.Object
{
protected T _loadedAsset;
/// <summary>
/// A reference to the loaded asset. Intended to be used for caching purposes.
/// </summary>
protected T LoadedAsset;
/// <summary>
/// The method that loads the asset.
/// </summary>
/// <returns>The loaded asset</returns>
public abstract T LoadAsset();
}
}
@@ -4,17 +4,34 @@ using UnityEngine;
namespace MiraAPI.Utilities.Assets
{
/// <summary>
/// A utility class for loading assets from an asset bundle.
/// </summary>
/// <param name="name">The name of the asset</param>
/// <param name="bundle">The AssetBundle that contains the asset</param>
/// <typeparam name="T">The type of the asset to be loaded</typeparam>
public class LoadableBundleAsset<T>(string name, AssetBundle bundle) : LoadableAsset<T> where T : UnityEngine.Object
{
/// <summary>
/// Loads the asset from the asset bundle.
/// </summary>
/// <returns>The asset</returns>
/// <exception cref="Exception">The asset did not load properly.</exception>
public override T LoadAsset()
{
if (_loadedAsset != null)
if (LoadedAsset)
{
return _loadedAsset;
return LoadedAsset;
}
return _loadedAsset = bundle.LoadAsset<T>(name);
throw new Exception($"INVALID ASSET: {name}");
LoadedAsset = bundle.LoadAsset<T>(name);
if (!LoadedAsset)
{
throw new Exception($"INVALID ASSET: {name}");
}
return LoadedAsset;
}
}
}
@@ -6,14 +6,14 @@ namespace MiraAPI.Utilities.Assets
{
public override Sprite LoadAsset()
{
if (_loadedAsset != null)
if (LoadedAsset != null)
{
return _loadedAsset;
return LoadedAsset;
}
try
{
return _loadedAsset = SpriteTools.LoadSpriteFromPath(path);
return LoadedAsset = SpriteTools.LoadSpriteFromPath(path);
}
catch
{
+13
View File
@@ -7,6 +7,9 @@ using UnityEngine;
namespace MiraAPI.Utilities.Assets
{
/// <summary>
/// A utility class for various sprite-related operations.
/// </summary>
public static class SpriteTools
{
private delegate bool DLoadImage(IntPtr tex, IntPtr data, bool markNonReadable);
@@ -22,6 +25,12 @@ namespace MiraAPI.Utilities.Assets
return _iCallLoadImage.Invoke(tex.Pointer, il2CPPArray.Pointer, markNonReadable);
}
/// <summary>
/// Load a sprite from a resource path.
/// </summary>
/// <param name="resourcePath">The path to the resource.</param>
/// <returns>A sprite made from the resource</returns>
/// <exception cref="Exception">The resource cannot be found.</exception>
public static Sprite LoadSpriteFromPath(string resourcePath)
{
var tex = new Texture2D(1, 1, TextureFormat.ARGB32, false);
@@ -32,6 +41,10 @@ namespace MiraAPI.Utilities.Assets
var buttonTexture = myStream.ReadFully();
LoadImage(tex, buttonTexture, false);
}
else
{
throw new Exception($"Resource not found: {resourcePath}");
}
return Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
}