YooAssetManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Cysharp.Threading.Tasks;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. using YooAsset;
  7. public class YooAssetManager : MonoBehaviour
  8. {
  9. public static YooAssetManager Instance { get; private set; }
  10. /// <summary> 栗都관 </summary>
  11. public ResourcePackage package;
  12. /// <summary> Asset얌깨 </summary>
  13. public Dictionary<string, AssetHandle> handles = new Dictionary<string, AssetHandle>();
  14. /// <summary> AllAsset얌깨 </summary>
  15. public Dictionary<string,AllAssetsHandle>allHandles=new Dictionary<string,AllAssetsHandle>();
  16. /// <summary> SceneAsset얌깨 </summary>
  17. public Dictionary<string, SceneHandle> sceneHandles = new Dictionary<string, SceneHandle>();
  18. private void Awake()
  19. {
  20. Instance = this;
  21. package = YooAssets.GetPackage("DefaultPackage");
  22. //await this.gameObject.AddComponent<Boot>().InitYooAsset();
  23. //package = YooAssets.GetPackage("DefaultPackage");
  24. //var ui = await LoadAsset<GameObject>("DlgFaceBuildUI");
  25. //var go =GameObject.Instantiate(ui);
  26. //go.transform.SetParent(GameObject.Find("Canvas").transform);
  27. //go.transform.localPosition = Vector3.zero;
  28. }
  29. /// <summary>
  30. /// 속潼栗都
  31. /// </summary>
  32. /// <typeparam name="T"></typeparam>
  33. /// <param name="location"></param>
  34. /// <returns></returns>
  35. public async UniTask<T> LoadAsset<T>(string location) where T : Object
  36. {
  37. AssetHandle handler;
  38. if (!handles.TryGetValue(location,out handler))
  39. {
  40. handler = package.LoadAssetAsync<T>(location);
  41. await handler.Task;
  42. handles.Add(location, handler);
  43. }
  44. return (T)((AssetHandle)handler).AssetObject;
  45. }
  46. /// <summary>
  47. /// 속潼杰唐栗都
  48. /// </summary>
  49. /// <typeparam name="T"></typeparam>
  50. /// <param name="location"></param>
  51. /// <returns></returns>
  52. public async UniTask<Dictionary<string, T>> LoadAllAsset<T>(string location) where T : Object
  53. {
  54. AllAssetsHandle handler;
  55. if (!allHandles.TryGetValue(location, out handler))
  56. {
  57. handler = package.LoadAllAssetsAsync<T>(location);
  58. await handler.Task;
  59. allHandles.Add(location, handler);
  60. }
  61. Dictionary<string, T> dictionary = new Dictionary<string, T>();
  62. foreach (UnityEngine.Object assetObj in ((AllAssetsHandle)handler).AllAssetObjects)
  63. {
  64. T t = assetObj as T;
  65. dictionary.Add(t.name, t);
  66. }
  67. return dictionary;
  68. }
  69. /// <summary>
  70. /// 속潼끝쒼
  71. /// </summary>
  72. /// <param name="location"></param>
  73. /// <returns></returns>
  74. public async UniTask LoadScene(string location)
  75. {
  76. if (handles.ContainsKey(location))
  77. return;
  78. bool activeOnLoad = true;
  79. SceneHandle handle = package.LoadSceneAsync(location, LoadSceneMode.Additive, activeOnLoad);
  80. ScenesManager.Instance.handle = handle;
  81. sceneHandles.Add(location, handle);
  82. await handle.Task;
  83. }
  84. public async UniTask ActivateScene(SceneHandle handle)
  85. {
  86. if (handle.Progress >= 0.9)
  87. {
  88. handle.ActivateScene();
  89. await handle.Task;
  90. }
  91. }
  92. }