Skip to content

Commit

Permalink
Merge pull request #289 from rockfactory/feat-allows-addressables-loa…
Browse files Browse the repository at this point in the history
…ding-no-release

Add the option to keep addressables in memory after loading them by label
  • Loading branch information
cheese3660 authored Jan 31, 2024
2 parents 029775c + cb7834a commit 91ef880
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/SpaceWarp.Core/API/Loading/Loading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ public static void AddAddressablesLoadingAction<T>(string name, string label, Ac
AddGeneralLoadingAction(() => new AddressableAction<T>(name, label, action));
}

/// <summary>
/// Registers an action to be done on addressables after addressables have been loaded. Should be added either on Awake() or Start().
/// Allows to keep asset in memory after loading them. This is useful for textures or UXML templates, for example.
/// </summary>
/// <param name="name">The name of the action</param>
/// <param name="label">The addressables label to hook into</param>
/// <param name="keepAssets">Indicates if assets should be kept in memory after loading them.</param>
/// <param name="action">The action to be done on each addressables asset</param>
/// <typeparam name="T">The type of asset that this action is done upon</typeparam>
public static void AddAddressablesLoadingAction<T>(string name, string label, bool keepAssets, Action<T> action)
where T : UnityObject
{
AddGeneralLoadingAction(() => new AddressableAction<T>(name, label, keepAssets, action));
}


private static Action<BaseSpaceWarpPlugin> CreateAssetLoadingActionWithExtensions(string subfolder,
Func<string, string, List<(string name, UnityObject asset)>> importFunction, string[] extensions)
Expand Down
16 changes: 15 additions & 1 deletion src/SpaceWarp.Core/Patching/LoadingActions/AddressableAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class AddressableAction<T> : FlowAction where T : UnityObject
{
private string _label;
private Action<T> _action;
private bool _keepAssets;

/// <summary>
/// Creates a new addressable loading action.
Expand All @@ -30,6 +31,19 @@ public AddressableAction(string name, string label, Action<T> action) : base(nam
_label = label;
_action = action;
}

/// <summary>
/// Creates a new addressable loading action, with the option to keep the asset in memory after loading.
/// This is useful for textures or UXML templates, for example.
/// </summary>
/// <param name="name">Name of the action.</param>
/// <param name="label">Label of the asset to load.</param>
/// <param name="action">Action to perform on the loaded asset.</param>
/// <param name="keepAssets">Allows to keep asset in memory after loading them.</param>
public AddressableAction(string name, string label, bool keepAssets, Action<T> action) : this(name, label, action)
{
_keepAssets = keepAssets;
}

private bool DoesLabelExist(object label)
{
Expand All @@ -55,7 +69,7 @@ public override void DoAction(Action resolve, Action<string> reject)
{
GameManager.Instance.Assets.LoadByLabel(_label,_action,delegate(IList<T> assetLocations)
{
if (assetLocations != null)
if (assetLocations != null && !_keepAssets)
{
Addressables.Release(assetLocations);
}
Expand Down

0 comments on commit 91ef880

Please sign in to comment.