I’m trying to be a good Unity-ite and use AssetBundles instead of Resources, but I’m having an issue.
I use the AssetBundle Browser to build AssetBundles out of ScriptableObjects. Then, I am dynamically loading them using the following code:
public static async void LoadAll()
{
Currencies = (await GetAssetsFromAssetBundleAsync<Currency>(Path.Combine(Application.streamingAssetsPath, "currencies"))).ToList();
}
private static Task<IEnumerable<T>> GetAssetsFromAssetBundleAsync<T>(string path) where T : class
{
TaskCompletionSource<IEnumerable<T>> taskCompletionSource = new TaskCompletionSource<IEnumerable<T>>();
var assetBundleRequest = AssetBundle.LoadFromFileAsync(path);
assetBundleRequest.completed += (x) =>
{
var assetRequest = assetBundleRequest.assetBundle.LoadAllAssetsAsync<T>();
assetRequest.completed += (y) =>
{
taskCompletionSource.TrySetResult(assetRequest.allAssets.Select(asset => asset as T));
};
};
return taskCompletionSource.Task;
}
I am using this to load a ScriptableObject, and it all works. Except when I modify a serialized field at runtime. This data change fails to serialize, so when I exit the scene, the changes I make do not persist.
A direct reference works perfectly, and changes persist when I make them at runtime. It only breaks when the reference comes from the AssetBundle. Is there some explicit call I’m missing to save the changed data inside of the AssetBundle?
Thanks in advance!