Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using UnityEngine;
- using UnityEngine.AddressableAssets;
- using UnityEngine.ResourceManagement.AsyncOperations;
- using UnityEngine.UI;
- using Object = UnityEngine.Object;
- public class addressableCrashTest : MonoBehaviour
- {
- // Start is called before the first frame update
- public int numberToLoad = 100;
- public int totalPrefabs = 10;
- void Start()
- {
- string[] listToLoad = new string[numberToLoad];
- for (int s = 0; s < numberToLoad; s++)
- {
- listToLoad[s] = "Assets/ResourcesAddressable/"+(s % totalPrefabs);
- }
- for (int s = 0; s < numberToLoad; s++)
- {
- LoadAssetAsync<GameObject>(listToLoad[s], delegate(Object o)
- {
- GameObject obj;
- obj = Instantiate(o as GameObject, null);
- obj.GetComponent<Rigidbody2D>();
- obj.GetComponent<Image>();
- obj.GetComponent<BoxCollider2D>();
- obj.GetComponent<Collider>();
- obj.GetComponent<Rigidbody2D>();
- obj.GetComponent<Image>();
- });
- }
- }
- public static async Task<Object> LoadAssetAsync<T>(string name, Action<UnityEngine.Object> callback=null) where T : UnityEngine.Object
- {
- var task = await LoadAssetAsync(name, typeof(T), callback);
- //await task;
- return task;
- }
- public static async Task<Object> LoadAssetAsync(string name, Type T, Action<UnityEngine.Object> callback)
- {
- name = name + getExtension(T);
- string path = name;
- var task = await LoadAddressableAsset(path, T);
- if(callback != null)callback(task);
- return task;
- }
- public static async Task<Object> LoadAddressableAsset(string name, Type T)
- {
- AsyncOperationHandle handle = Addressables.LoadAssetAsync<GameObject>(name);
- object handlerResult = await handle.Task;
- return handlerResult as Object;
- }
- static string getExtension<T>()
- {
- return getExtension(typeof(T));
- }
- static string getExtension(Type T)
- {
- if (T == typeof(GameObject)) return ".prefab";
- if (T == typeof(Sprite)) return ".png";
- if (T == typeof(Material)) return ".mat";
- if (T == typeof(AudioClip)) return ".mp3";
- return ".asset";
- }
- // Update is called once per frame
- void Update()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement