- Home /
Load the contents of an Addressable folder without using labels?
Basically, my game is composed of 1000+ levels, and the content of each level is inside a folder. Each folder contains 3 text files (1.bytes, 2.bytes, data.json) that are used by the level. The goal is to load those text files when they are needed (when a level begins), and then destroy (release) them when they are no longer needed.
This is how I am using Addressables at the moment:
1.) Drag all the level folders inside an Addressable Group titled "Content" and add a label to each folder so it can be loaded later.
2.) Load the text files from a folder via the code below:
private AsyncOperationHandle<IList<TextAsset>> aoh;
// this method is called to load the level assets based on level number
public void LoadLevelAssets(int levelNum)
{
string label = levelNum.ToString();
this.aoh = Addressables.LoadAssetsAsync<TextAsset>(label, result => { print(result); });
this.aoh.Completed += this.OnLevelAssetsLoaded;
}
// oncomplete handler
private void OnLevelAssetsLoaded(AsyncOperationHandle<IList<TextAsset>> obj)
{
var theList = obj.Result;
foreach(var item in theList)
{
print(item.name); // outputs the files names ("1", "2", "data")
print(item.text); // outputs the actual text inside the file
}
}
// method to destroy/release assets
public void ClearLevelAssets()
{
Addressables.Release(this.aoh);
}
The issue:
Labeling each folder is fine for testing, but if my game has 1000+ levels, it would be impractical to add a label to each folder one by one. Is using labels really the only way to load a folder? Is there a better way of doing things?
I know another way would be to just mark all the text files as addressable, and load each one via its address, but there doesn't seem to be an easy way to do this too as I'd have to expand each folder one by one and select the text files inside those.
Any insight on a better way to do this would be appreciated. Thanks.
Answer by AkamaruDesign · Apr 14 at 11:02 PM
@msnz im trying to do the same thing, here is my posible approach
load everything by its address but, for simplicity need to have a clear way to name each asset, for example
Level 1
t1.bytes
t2.bytes
tData.json
Level 2
t1.bytes
t2.bytes
tData.json
now, for loading each level, you need a generic class that choose what to download at run time.
private AsyncOperationHandle<TextAsset> part1, part2, partData;
private List<TextAsset> texts;
public void LoadLevelAssets(int levelNum)
{
texts = new List<TextAsset>();
string label = levelNum.ToString();
part1 = Addressables.LoadAssetAsync<TextAsset>("Assets/_Main/Animations/Content/" + label + "/t1.bytes");
part2 = Addressables.LoadAssetAsync<TextAsset>("Assets/_Main/Animations/Content/" + label + "/t2.bytes");
partData = Addressables.LoadAssetAsync<TextAsset>("Assets/_Main/Animations/Content/" + label + "/tData.json");
part1.Completed += Part1Completed;
part2.Completed += Part2Completed;
partData.Completed += PartDataCompleted;
}
void Part1Completed(AsyncOperationHandle<TextAsset> asset)
{
if (asset.Status == AsyncOperationStatus.Succeeded)
{
texts.Add(asset.Result);
}
}
void Part2Completed(AsyncOperationHandle<TextAsset> asset)
{
if (asset.Status == AsyncOperationStatus.Succeeded)
{
texts.Add(asset.Result);
}
}
void PartDataCompleted(AsyncOperationHandle<TextAsset> asset)
{
if (asset.Status == AsyncOperationStatus.Succeeded)
{
texts.Add(asset.Result);
}
}
//Form here you can do wherever yo want with the list of texts
Your answer
Follow this Question
Related Questions
Selling with standard assets. 2 Answers
Accessing the Asset's setting from code using Wizards/Editor classes? 0 Answers
Best way to import animations from blender? 1 Answer
how to use the water buoyancy asset 0 Answers
Find All Assets by Type 2 Answers