ResMgr.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using UnityEngine;
  5. public class ResMgr : MonoBehaviour
  6. {
  7. public delegate void LoadAyncCallback(UnityEngine.Object obj);
  8. public const string TAG = "ResMgr";
  9. private static ResMgr m_Instance;
  10. public static ResMgr Instance
  11. {
  12. get
  13. {
  14. if (null == ResMgr.m_Instance)
  15. {
  16. GameObject gameObject = GameObject.Find("ResMgr");
  17. if (null == gameObject)
  18. {
  19. gameObject = new GameObject("ResMgr");
  20. gameObject.transform.localPosition = new Vector3(1000f, 0f, 0f);
  21. DontDestroyOnLoad(gameObject);
  22. }
  23. ResMgr.m_Instance = gameObject.AddComponent<ResMgr>();
  24. }
  25. return ResMgr.m_Instance;
  26. }
  27. }
  28. public void Init()
  29. {
  30. }
  31. public UnityEngine.Object Load(string assetName)
  32. {
  33. #if UNITY_EDITOR
  34. return Resources.Load(assetName);
  35. #else
  36. return GameInit.ablist.LoadAsset(assetName);
  37. #endif
  38. }
  39. public T Load<T>(string assetName) where T : UnityEngine.Object
  40. {
  41. #if UNITY_EDITOR
  42. return Resources.Load<T>(assetName);
  43. #else
  44. return GameInit.ablist.LoadAsset<T>(assetName);
  45. #endif
  46. //
  47. }
  48. public ResourceRequest LoadAsync(string assetName)
  49. {
  50. // return GameInit.ablist(assetName);
  51. return Resources.LoadAsync(assetName);
  52. }
  53. public ResourceRequest LoadAsync<T>(string assetName) where T : UnityEngine.Object
  54. {
  55. #if UNITY_EDITOR
  56. return Resources.LoadAsync<T>(assetName);
  57. #else
  58. return GameInit.ablist.LoadAssetAsync<T>(assetName);
  59. #endif
  60. }
  61. public void LoadAsync(string assetName, ResMgr.LoadAyncCallback callback)
  62. {
  63. //base.StartCoroutine(this.ResourcesLoadAsync(assetName, callback));
  64. }
  65. [DebuggerHidden]
  66. //private IEnumerator ResourcesLoadAsync(string assetName, ResMgr.LoadAyncCallback callback)
  67. //{
  68. // ResMgr.<ResourcesLoadAsync>c__Iterator0 <ResourcesLoadAsync>c__Iterator = new ResMgr.<ResourcesLoadAsync>c__Iterator0();
  69. // <ResourcesLoadAsync>c__Iterator.assetName = assetName;
  70. // <ResourcesLoadAsync>c__Iterator.callback = callback;
  71. // return <ResourcesLoadAsync>c__Iterator;
  72. //}
  73. public Sprite GetSprite(string name)
  74. {
  75. if (string.IsNullOrEmpty(name))
  76. {
  77. return null;
  78. }
  79. GameObject gameObject = this.Load<GameObject>(name);
  80. if (null == gameObject)
  81. {
  82. return null;
  83. }
  84. SpriteRenderer component = gameObject.GetComponent<SpriteRenderer>();
  85. if (null == component)
  86. {
  87. return null;
  88. }
  89. return component.sprite;
  90. }
  91. public void GetSpriteAsync(string name, Action<Sprite> callback)
  92. {
  93. if (!string.IsNullOrEmpty(name))
  94. {
  95. this.LoadAsync(name, delegate (UnityEngine.Object obj)
  96. {
  97. if (callback == null)
  98. {
  99. return;
  100. }
  101. GameObject gameObject = obj as GameObject;
  102. Sprite obj2 = null;
  103. if (null != gameObject)
  104. {
  105. SpriteRenderer component = gameObject.GetComponent<SpriteRenderer>();
  106. if (null != component)
  107. {
  108. obj2 = component.sprite;
  109. }
  110. }
  111. callback(obj2);
  112. });
  113. }
  114. }
  115. }