diff --git a/MiraAPI/Utilities/Assets/LoadableAsset.cs b/MiraAPI/Utilities/Assets/LoadableAsset.cs index e2e63bc..853581d 100644 --- a/MiraAPI/Utilities/Assets/LoadableAsset.cs +++ b/MiraAPI/Utilities/Assets/LoadableAsset.cs @@ -1,8 +1,22 @@ namespace MiraAPI.Utilities.Assets { + /// + /// An abstract class that provides a simple pattern for loading assets. + /// Mira uses the pattern in various locations. + /// You can create your own implementation of this class to load assets in different ways. + /// + /// The type of the asset to be loaded public abstract class LoadableAsset where T : UnityEngine.Object { - protected T _loadedAsset; + /// + /// A reference to the loaded asset. Intended to be used for caching purposes. + /// + protected T LoadedAsset; + + /// + /// The method that loads the asset. + /// + /// The loaded asset public abstract T LoadAsset(); } } diff --git a/MiraAPI/Utilities/Assets/LoadableBundleAsset.cs b/MiraAPI/Utilities/Assets/LoadableBundleAsset.cs index 7d820c3..aac95d1 100644 --- a/MiraAPI/Utilities/Assets/LoadableBundleAsset.cs +++ b/MiraAPI/Utilities/Assets/LoadableBundleAsset.cs @@ -4,17 +4,34 @@ using UnityEngine; namespace MiraAPI.Utilities.Assets { + /// + /// A utility class for loading assets from an asset bundle. + /// + /// The name of the asset + /// The AssetBundle that contains the asset + /// The type of the asset to be loaded public class LoadableBundleAsset(string name, AssetBundle bundle) : LoadableAsset where T : UnityEngine.Object { + /// + /// Loads the asset from the asset bundle. + /// + /// The asset + /// The asset did not load properly. public override T LoadAsset() { - if (_loadedAsset != null) + if (LoadedAsset) { - return _loadedAsset; + return LoadedAsset; } - return _loadedAsset = bundle.LoadAsset(name); - throw new Exception($"INVALID ASSET: {name}"); + LoadedAsset = bundle.LoadAsset(name); + + if (!LoadedAsset) + { + throw new Exception($"INVALID ASSET: {name}"); + } + + return LoadedAsset; } } } diff --git a/MiraAPI/Utilities/Assets/LoadableResourceAsset.cs b/MiraAPI/Utilities/Assets/LoadableResourceAsset.cs index c868c9a..ad0b3ae 100644 --- a/MiraAPI/Utilities/Assets/LoadableResourceAsset.cs +++ b/MiraAPI/Utilities/Assets/LoadableResourceAsset.cs @@ -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 { diff --git a/MiraAPI/Utilities/Assets/SpriteTools.cs b/MiraAPI/Utilities/Assets/SpriteTools.cs index 4d016c9..1ca0261 100644 --- a/MiraAPI/Utilities/Assets/SpriteTools.cs +++ b/MiraAPI/Utilities/Assets/SpriteTools.cs @@ -7,6 +7,9 @@ using UnityEngine; namespace MiraAPI.Utilities.Assets { + /// + /// A utility class for various sprite-related operations. + /// 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); } + /// + /// Load a sprite from a resource path. + /// + /// The path to the resource. + /// A sprite made from the resource + /// The resource cannot be found. 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)); }