ResourLoadMananger.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ResourLoadMananger : MonoBehaviour {
  5. private static ResourLoadMananger instance;
  6. public static ResourLoadMananger GetInstance()
  7. {
  8. if(instance==null)
  9. {
  10. instance = new GameObject("ResourLoadMananger").AddComponent<ResourLoadMananger>();
  11. }
  12. return instance;
  13. }
  14. private Hashtable ht = null;
  15. void Awake()
  16. {
  17. ht = new Hashtable();
  18. }
  19. /// <summary>
  20. /// 调用资源(带对象缓冲技术)
  21. /// </summary>
  22. /// <typeparam name="T"></typeparam>
  23. /// <param name="path"></param>
  24. /// <param name="isCatch"></param>
  25. /// <returns></returns>
  26. public T LoadResource<T>(string path, bool isCatch) where T : UnityEngine.Object
  27. {
  28. if (ht.Contains(path))
  29. {
  30. return ht[path] as T;
  31. }
  32. T TResource = Resources.Load<T>(path);
  33. if (TResource == null)
  34. {
  35. Debug.LogError(GetType() + "/GetInstance()/TResource 提取的资源找不到,请检查。 path=" + path);
  36. }
  37. else if (isCatch)
  38. {
  39. ht.Add(path, TResource);
  40. }
  41. return TResource;
  42. }
  43. /// <summary>
  44. /// 调用资源
  45. /// </summary>
  46. /// <param name="path"></param>
  47. /// <param name="isCatch"></param>
  48. /// <returns></returns>
  49. public GameObject LoadAsset(string path, bool isCatch)
  50. {
  51. GameObject goObj = LoadResource<GameObject>(path, isCatch);
  52. GameObject goObjClone = GameObject.Instantiate<GameObject>(goObj);
  53. if (goObjClone == null)
  54. {
  55. Debug.LogError(GetType() + "/LoadAsset()/克隆资源不成功,请检查。 path=" + path);
  56. }
  57. //goObj = null;//??????????
  58. return goObjClone;
  59. }
  60. }