Created
November 18, 2019 22:30
-
-
Save Badgerdox/953237cc3afc13b6cd5fcc3a5d980eba to your computer and use it in GitHub Desktop.
Create Easily and Quickly load an Addressable Asset in Unity3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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