diff --git a/src/SpaceWarp.Core/API/Loading/Loading.cs b/src/SpaceWarp.Core/API/Loading/Loading.cs index d85b3b0..90d9d31 100644 --- a/src/SpaceWarp.Core/API/Loading/Loading.cs +++ b/src/SpaceWarp.Core/API/Loading/Loading.cs @@ -80,6 +80,21 @@ public static void AddAddressablesLoadingAction(string name, string label, Ac AddGeneralLoadingAction(() => new AddressableAction(name, label, action)); } + /// + /// 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. + /// + /// The name of the action + /// The addressables label to hook into + /// Indicates if assets should be kept in memory after loading them. + /// The action to be done on each addressables asset + /// The type of asset that this action is done upon + public static void AddAddressablesLoadingAction(string name, string label, bool keepAssets, Action action) + where T : UnityObject + { + AddGeneralLoadingAction(() => new AddressableAction(name, label, keepAssets, action)); + } + private static Action CreateAssetLoadingActionWithExtensions(string subfolder, Func> importFunction, string[] extensions) diff --git a/src/SpaceWarp.Core/Patching/LoadingActions/AddressableAction.cs b/src/SpaceWarp.Core/Patching/LoadingActions/AddressableAction.cs index 6f1b6cf..2fc6fe7 100644 --- a/src/SpaceWarp.Core/Patching/LoadingActions/AddressableAction.cs +++ b/src/SpaceWarp.Core/Patching/LoadingActions/AddressableAction.cs @@ -18,6 +18,7 @@ public class AddressableAction : FlowAction where T : UnityObject { private string _label; private Action _action; + private bool _keepAssets; /// /// Creates a new addressable loading action. @@ -30,6 +31,19 @@ public AddressableAction(string name, string label, Action action) : base(nam _label = label; _action = action; } + + /// + /// 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. + /// + /// Name of the action. + /// Label of the asset to load. + /// Action to perform on the loaded asset. + /// Allows to keep asset in memory after loading them. + public AddressableAction(string name, string label, bool keepAssets, Action action) : this(name, label, action) + { + _keepAssets = keepAssets; + } private bool DoesLabelExist(object label) { @@ -55,7 +69,7 @@ public override void DoAction(Action resolve, Action reject) { GameManager.Instance.Assets.LoadByLabel(_label,_action,delegate(IList assetLocations) { - if (assetLocations != null) + if (assetLocations != null && !_keepAssets) { Addressables.Release(assetLocations); }