Skip to content

Instantly share code, notes, and snippets.

@Badgerdox
Created November 18, 2019 22:30
Show Gist options
  • Save Badgerdox/953237cc3afc13b6cd5fcc3a5d980eba to your computer and use it in GitHub Desktop.
Save Badgerdox/953237cc3afc13b6cd5fcc3a5d980eba to your computer and use it in GitHub Desktop.
Create Easily and Quickly load an Addressable Asset in Unity3D
public static async Task SearchByLabels<T>(List<string> labels, bool removeDupes, List<T> createdObjs)
where T : Object
{
IList<IResourceLocation> locations = new List<IResourceLocation>();
foreach (var label in labels)
{
foreach (var resourceLocation in await Addressables.LoadResourceLocationsAsync(label).Task)
locations.Add(resourceLocation);
}
if (removeDupes)
await FindDuplicateEntries(locations, true);
foreach (var location in locations)
{
createdObjs.Add(await Addressables.InstantiateAsync(location).Task as T);
}
}
private static async Task FindDuplicateEntries(IList<IResourceLocation> locations, bool toRemove = false)
{
var dupes = locations.GroupBy(s => s)
.Where(g => g.Count() > 1)
.Select(y => y.Key)
.ToList();
if (toRemove)
foreach (var dupe in dupes)
{
locations.Remove(dupe);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment