Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the option to keep addressables in memory after loading them by label #289

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading