Advertisement
iyed

Untitled

Mar 2nd, 2021
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using UnityEngine;
  6. using UnityEngine.AddressableAssets;
  7. using UnityEngine.ResourceManagement.AsyncOperations;
  8. using UnityEngine.UI;
  9. using Object = UnityEngine.Object;
  10.  
  11. public class addressableCrashTest : MonoBehaviour
  12. {
  13.     // Start is called before the first frame update
  14.     public int numberToLoad = 100;
  15.     public int totalPrefabs = 10;
  16.     void Start()
  17.     {
  18.         string[] listToLoad = new string[numberToLoad];
  19.  
  20.         for (int s = 0; s < numberToLoad; s++)
  21.         {
  22.             listToLoad[s] = "Assets/ResourcesAddressable/"+(s % totalPrefabs);
  23.         }
  24.  
  25.  
  26.         for (int s = 0; s < numberToLoad; s++)
  27.         {
  28.  
  29.             LoadAssetAsync<GameObject>(listToLoad[s], delegate(Object o)
  30.             {
  31.                 GameObject obj;
  32.                 obj = Instantiate(o as GameObject, null);
  33.                 obj.GetComponent<Rigidbody2D>();
  34.                 obj.GetComponent<Image>();
  35.                 obj.GetComponent<BoxCollider2D>();
  36.                 obj.GetComponent<Collider>();
  37.                 obj.GetComponent<Rigidbody2D>();
  38.                 obj.GetComponent<Image>();
  39.  
  40.                
  41.  
  42.  
  43.  
  44.  
  45.             });
  46.  
  47.  
  48.         }
  49.     }
  50.  
  51.     public static async Task<Object> LoadAssetAsync<T>(string name, Action<UnityEngine.Object> callback=null) where T : UnityEngine.Object
  52.     {
  53.         var task = await LoadAssetAsync(name, typeof(T), callback);
  54.         //await task;
  55.         return task;
  56.     }
  57.  
  58.     public static async Task<Object> LoadAssetAsync(string name, Type T, Action<UnityEngine.Object> callback)
  59.     {
  60.         name = name + getExtension(T);
  61.         string path = name;
  62.         var task = await LoadAddressableAsset(path, T);
  63.         if(callback != null)callback(task);
  64.  
  65.         return task;
  66.     }
  67.  
  68.     public static async Task<Object> LoadAddressableAsset(string name, Type T)
  69.     {
  70.        
  71.             AsyncOperationHandle handle = Addressables.LoadAssetAsync<GameObject>(name);
  72.             object handlerResult = await handle.Task;
  73.             return handlerResult as Object;
  74.  
  75.     }
  76.  
  77.     static string getExtension<T>()
  78.     {
  79.         return getExtension(typeof(T));
  80.     }
  81.     static string getExtension(Type T)
  82.     {
  83.         if (T == typeof(GameObject)) return ".prefab";
  84.         if (T == typeof(Sprite)) return ".png";
  85.         if (T == typeof(Material)) return ".mat";
  86.         if (T == typeof(AudioClip)) return ".mp3";
  87.         return ".asset";
  88.     }
  89.     // Update is called once per frame
  90.     void Update()
  91.     {
  92.        
  93.     }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement