ResMgr.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. return Resources.Load(assetName);
  34. }
  35. public T Load<T>(string assetName) where T : UnityEngine.Object
  36. {
  37. return Resources.Load<T>(assetName);
  38. }
  39. public ResourceRequest LoadAsync(string assetName)
  40. {
  41. return Resources.LoadAsync(assetName);
  42. }
  43. public ResourceRequest LoadAsync<T>(string assetName) where T : UnityEngine.Object
  44. {
  45. return Resources.LoadAsync<T>(assetName);
  46. }
  47. public void LoadAsync(string assetName, ResMgr.LoadAyncCallback callback)
  48. {
  49. //base.StartCoroutine(this.ResourcesLoadAsync(assetName, callback));
  50. }
  51. [DebuggerHidden]
  52. //private IEnumerator ResourcesLoadAsync(string assetName, ResMgr.LoadAyncCallback callback)
  53. //{
  54. // ResMgr.<ResourcesLoadAsync>c__Iterator0 <ResourcesLoadAsync>c__Iterator = new ResMgr.<ResourcesLoadAsync>c__Iterator0();
  55. // <ResourcesLoadAsync>c__Iterator.assetName = assetName;
  56. // <ResourcesLoadAsync>c__Iterator.callback = callback;
  57. // return <ResourcesLoadAsync>c__Iterator;
  58. //}
  59. public Sprite GetSprite(string name)
  60. {
  61. if (string.IsNullOrEmpty(name))
  62. {
  63. return null;
  64. }
  65. GameObject gameObject = this.Load<GameObject>(name);
  66. if (null == gameObject)
  67. {
  68. return null;
  69. }
  70. SpriteRenderer component = gameObject.GetComponent<SpriteRenderer>();
  71. if (null == component)
  72. {
  73. return null;
  74. }
  75. return component.sprite;
  76. }
  77. public void GetSpriteAsync(string name, Action<Sprite> callback)
  78. {
  79. if (!string.IsNullOrEmpty(name))
  80. {
  81. this.LoadAsync(name, delegate (UnityEngine.Object obj)
  82. {
  83. if (callback == null)
  84. {
  85. return;
  86. }
  87. GameObject gameObject = obj as GameObject;
  88. Sprite obj2 = null;
  89. if (null != gameObject)
  90. {
  91. SpriteRenderer component = gameObject.GetComponent<SpriteRenderer>();
  92. if (null != component)
  93. {
  94. obj2 = component.sprite;
  95. }
  96. }
  97. callback(obj2);
  98. });
  99. }
  100. }
  101. }