ResourceManager.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. public class ResourceManager : BaseManager<ResourceManager>
  6. {
  7. public T Load<T>(string name) where T : Object
  8. {
  9. T res = Resources.Load<T>(name);
  10. return res;
  11. }
  12. public void LoadAsync<T>(string name, UnityAction<T> action = null) where T : Object
  13. {
  14. MonoManager.GetInstance().StartCoroutine(ReallyLoadResourceAsyn<T>(name, action));
  15. }
  16. private IEnumerator ReallyLoadResourceAsyn<T>(string name, UnityAction<T> action = null) where T : Object
  17. {
  18. ResourceRequest r = Resources.LoadAsync<T>(name);
  19. yield return r;
  20. if (r.asset != null)
  21. {
  22. if (action != null)
  23. {
  24. if (r.asset is GameObject)
  25. {
  26. action((GameObject.Instantiate(r.asset)) as T);
  27. }
  28. else
  29. {
  30. action(r.asset as T);
  31. }
  32. }
  33. }
  34. else
  35. {
  36. Debug.Log(name + ": null");
  37. }
  38. }
  39. }