public async Task PreloadScripts() { var scripts = await AddressableResService.GetInstance().LoadResourcesAsync <TextAsset>("Script"); foreach (var script in scripts) { Scripts[script.name] = script.text; } }
/// <summary> /// Apply the resources in the AddressableGameItem to the target GameObject. If the resource is texture, will try to replace the texture with same name. /// If the resource is prefab, will be instantiated as a child object under the given target. If the target is null, then the instantiated /// object will be a stand alone object /// </summary> /// <param name="target"></param> /// <param name="position"></param> /// <param name="rotation"></param> /// <param name="names"></param> /// <param name="label"></param> /// <param name="callback"></param> public async Task Apply(GameObject target, Slot slot, Vector3 position, Quaternion rotation, List <string> names = null, string label = null, ResourceLoadedCallback callback = null) { target = FindSlotTarget(target, slot) ?? target; bool isEmptyName = StringUtil.IsListEmpty(names); if (isEmptyName && string.IsNullOrEmpty(label)) { Debug.LogError("Names and Label must have at least one piece of valid data"); } else { if (string.IsNullOrEmpty(label)) { //Label is null foreach (string key in names) { var rls = await AddressableResService.GetInstance().LoadResourceLocationsAsync(key); await Task.WhenAll(rls.Select(rl => AddressableResService.GetInstance().LoadResourceByLocationAsync(rl, (name, resource, type) => { ApplyResource(target, position, rotation, name, resource, type, callback); }))); } } else { //Apply resources under same label as a batch var rls = await AddressableResService.GetInstance().LoadResourceLocationsForLabelAsync(label); await Task.WhenAll(rls.Where(rl => isEmptyName || names.Contains(rl.PrimaryKey)) .Select(rl => AddressableResService.GetInstance().LoadResourceByLocationAsync(rl, (name, resource, type) => { ApplyResource(target, position, rotation, name, resource, type, callback); }))); } } }
/// <summary> /// Play an audio clip in the global addressable assets /// </summary> /// /// <param name="clipName">The resource name of the clip</param> /// <param name="label">The addressable label of the clip</param> public async Task PlayGlobalAddressableAudioClip(string clipName, string label = "Audio") { AudioClip clip = await AddressableResService.GetInstance().LoadResourceAsync <AudioClip>(clipName, label); Play(clip); }
/// <summary> /// Apply the resources in the AddressableGameItem to the target GameObject. If the resource is texture, will try to replace the texture with same name. /// If the resource is prefab, will be instantiated as a child object under the given target. If the target is null, then the instantiated /// object will be a stand alone object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="target"></param> /// <param name="position"></param> /// <param name="rotation"></param> /// <param name="names"></param> /// <param name="label"></param> /// <param name="callback"></param> public async Task Apply <T>(GameObject target, Slot slot, Vector3 position, Quaternion rotation, List <string> names = null, string label = null, ResourceLoadedCallback <T> callback = null) { target = FindSlotTarget(target, slot) ?? target; bool isEmptyName = StringUtil.IsListEmpty(names); if (isEmptyName && string.IsNullOrEmpty(label)) { Debug.LogError("Names and Label must have at least one piece of valid data"); } else { if (string.IsNullOrEmpty(label)) { //Label is null foreach (string key in names) { var rls = await AddressableResService.GetInstance().LoadResourceLocationsAsync(key); Dictionary <string, Task <T> > tasks = new Dictionary <string, Task <T> >(); foreach (var rl in rls) { if (rl.ResourceType == typeof(T)) { //Load resources tasks.Add(rl.PrimaryKey, AddressableResService.GetInstance().LoadResourceByLocationAsync <T>(rl)); } } //Wait for completion await Task.WhenAll(tasks.Values); foreach (var pair in tasks) { //Apply ApplyResource <T>(target, position, rotation, pair.Key, await pair.Value, callback); } } } else { //Apply resources under same label as a batch var rls = await AddressableResService.GetInstance().LoadResourceLocationsForLabelAsync(label); Dictionary <string, Task <T> > tasks = new Dictionary <string, Task <T> >(); foreach (var rl in rls) { if (rl.ResourceType == typeof(T) && (isEmptyName || names.Contains(rl.PrimaryKey))) { //Load resources tasks.Add(rl.PrimaryKey, AddressableResService.GetInstance().LoadResourceByLocationAsync <T>(rl)); } } //Wait for completion await Task.WhenAll(tasks.Values); foreach (var pair in tasks) { //Apply ApplyResource <T>(target, position, rotation, pair.Key, await pair.Value, callback); } } } }
private void OnDestroy() { AddressableResService.GetInstance().Clear(); }