/// <summary> /// Load Resource by IResourceLocation, and pass back the resourceType /// </summary> /// <param name="rl"></param> /// <param name="callback"></param> public async Task LoadResourceByLocationAsync(IResourceLocation rl, ResourceLoadedCallback callback) { if (rl.ResourceType == typeof(SceneInstance)) { //Scene GetInstance().LoadResourceByLocationAsync <SceneInstance>(rl); } else if (rl.ResourceType == typeof(Texture2D)) { //Texture callback(rl.PrimaryKey, await GetInstance().LoadResourceByLocationAsync <Texture2D>(rl), rl.ResourceType); } else if (rl.ResourceType == typeof(GameObject)) { //Prefab callback(rl.PrimaryKey, await GetInstance().LoadResourceByLocationAsync <GameObject>(rl), rl.ResourceType); } else { Debug.LogWarning(string.Format("Unsupported type {0} of resource {1} at {2}, please add the code to handle it!!!", rl.ResourceType, rl.PrimaryKey, rl.InternalId)); } }
private static extern void ResourceManager_loadModel(IntPtr resourceManager, IntPtr context, string fileName, [MarshalAs(UnmanagedType.FunctionPtr)] ResourceLoadedCallback callback);
public void LoadModel(ResourceContext context, string fileName, ResourceLoadedCallback callback) { ResourceManager_loadModel(NativePtr, context.NativePtr, fileName, callback); }
/// <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); } } } }
/// <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> /// Apply the pass in resource to the target gameobject /// </summary> /// <typeparam name="T"></typeparam> /// <param name="target"></param> /// <param name="position"></param> /// <param name="rotation"></param> /// <param name="name"></param> /// <param name="resource"></param> /// <param name="callback"></param> public void ApplyResource <T>(GameObject target, Vector3 position, Quaternion rotation, string name, object resource, ResourceLoadedCallback <T> callback) { ApplyResource(target, position, rotation, name, resource, typeof(T), (name, resource, type) => { if (callback != null) { callback(name, (T)resource); } }); }
/// <summary> /// Apply the pass in resource to the target gameobject /// </summary> /// <param name="target"></param> /// <param name="position"></param> /// <param name="rotation"></param> /// <param name="name"></param> /// <param name="resource"></param> /// <param name="type"></param> /// <param name="callback"></param> public void ApplyResource(GameObject target, Vector3 position, Quaternion rotation, string name, object resource, Type type, ResourceLoadedCallback callback) { if (type == typeof(Texture2D)) { //Texture TryToApplyTexture2DOnChildren(target, name, (Texture2D)resource); } else if (type == typeof(GameObject)) { //Prefab if (target == null) { resource = Instantiate((GameObject)resource, position, rotation); } else { resource = Instantiate((GameObject)resource, position, rotation, target.transform); } } else { Debug.LogWarning(string.Format("Unsupported type {0} of resource {1}, please add the code to handle it!!!", type, name)); } if (callback != null) { callback(name, resource, type); } }
/// <summary> /// Apply the pass in resource to the target gameobject /// </summary> /// <typeparam name="T"></typeparam> /// <param name="target"></param> /// <param name="name"></param> /// <param name="resource"></param> /// <param name="instantiateInWorldSpace"></param> /// <param name="callback"></param> private void ApplyResource <T>(GameObject target, string name, object resource, bool instantiateInWorldSpace, ResourceLoadedCallback <T> callback) { ApplyResource(target, name, resource, typeof(T), instantiateInWorldSpace, (name, resource, type) => { if (callback != null) { callback(name, (T)resource); } }); }