Skip to content

Commit

Permalink
Add the option to keep addressables in memory after loading them by l…
Browse files Browse the repository at this point in the history
…abel

Useful to load UXML templates instead of using an UI asset bundle.
  • Loading branch information
rockfactory committed Jan 31, 2024
1 parent 029775c commit ab1fae2
Showing 1 changed file with 15 additions and 1 deletion.
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 ab1fae2

Please sign in to comment.