mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-20 01:46:15 +00:00
fix loadable asset code docs and rework logic slightly
This commit is contained in:
1 parent
9665188a18
commit
812cef5f2b
3 files changed
+25
-29
No files matched your search
@@ -5,17 +5,17 @@
|
||||
/// 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>
|
||||
/// <typeparam name="T">The type of the asset to be loaded.</typeparam>
|
||||
public abstract class LoadableAsset<T> where T : UnityEngine.Object
|
||||
{
|
||||
/// <summary>
|
||||
/// A reference to the loaded asset. Intended to be used for caching purposes.
|
||||
/// Gets or sets reference to the loaded asset. Intended to be used for caching purposes.
|
||||
/// </summary>
|
||||
protected T LoadedAsset;
|
||||
|
||||
protected T? LoadedAsset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The method that loads the asset.
|
||||
/// Loads the asset from the source.
|
||||
/// </summary>
|
||||
/// <returns>The loaded asset</returns>
|
||||
/// <returns>The loaded asset.</returns>
|
||||
public abstract T LoadAsset();
|
||||
}
|
||||
}
|
||||
@@ -7,30 +7,30 @@ 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>
|
||||
/// <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>
|
||||
/// <returns>The asset.</returns>
|
||||
/// <exception cref="Exception">The asset did not load properly.</exception>
|
||||
public override T LoadAsset()
|
||||
{
|
||||
if (LoadedAsset)
|
||||
if (LoadedAsset != null)
|
||||
{
|
||||
return LoadedAsset;
|
||||
}
|
||||
|
||||
LoadedAsset = bundle.LoadAsset<T>(name);
|
||||
|
||||
if (!LoadedAsset)
|
||||
|
||||
if (LoadedAsset == null)
|
||||
{
|
||||
throw new Exception($"INVALID ASSET: {name}");
|
||||
throw new InvalidOperationException($"INVALID ASSET: {name}");
|
||||
}
|
||||
|
||||
|
||||
return LoadedAsset;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,24 @@
|
||||
using System.Reflection;
|
||||
using Reactor.Utilities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MiraAPI.Utilities.Assets;
|
||||
|
||||
/// <summary>
|
||||
/// A utility class for loading assets from embedded resources.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to the embedded resource.</param>
|
||||
public class LoadableResourceAsset(string path) : LoadableAsset<Sprite>
|
||||
{
|
||||
private readonly Assembly _assembly = Assembly.GetCallingAssembly();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Sprite LoadAsset()
|
||||
{
|
||||
if (LoadedAsset)
|
||||
if (LoadedAsset != null)
|
||||
{
|
||||
return LoadedAsset;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return LoadedAsset = SpriteTools.LoadSpriteFromPath(path, _assembly);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Not loading, invalid asset: {path}");
|
||||
return null;
|
||||
}
|
||||
return LoadedAsset = SpriteTools.LoadSpriteFromPath(path, _assembly);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user