ResourcesMgr.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /***
  2. *
  3. * Title: "SUIFW" UI框架项目
  4. * 主题: 资源加载管理器
  5. * Description:
  6. * 功能: 本功能是在Unity的Resources类的基础之上,增加了“缓存”的处理。
  7. * 本脚本适用于
  8. * Date: 2017
  9. * Version: 0.1版本
  10. * Modify Recoder:
  11. *
  12. *
  13. */
  14. using UnityEngine;
  15. using UnityEngine.UI;
  16. using System;
  17. using System.Collections;
  18. using System.Collections.Generic;
  19. namespace SUIFW
  20. {
  21. public class ResourcesMgr : MonoBehaviour
  22. {
  23. /* 字段 */
  24. private static ResourcesMgr _Instance; //本脚本私有单例实例
  25. private Hashtable ht = null; //容器键值对集合
  26. /// <summary>
  27. /// 得到实例(单例)
  28. /// </summary>
  29. /// <returns></returns>
  30. public static ResourcesMgr GetInstance()
  31. {
  32. if (_Instance == null)
  33. {
  34. _Instance = new GameObject("_ResourceMgr").AddComponent<ResourcesMgr>();
  35. }
  36. return _Instance;
  37. }
  38. void Awake()
  39. {
  40. ht = new Hashtable();
  41. }
  42. /// <summary>
  43. /// 调用资源(带对象缓冲技术)
  44. /// </summary>
  45. /// <typeparam name="T"></typeparam>
  46. /// <param name="path"></param>
  47. /// <param name="isCatch"></param>
  48. /// <returns></returns>
  49. public T LoadResource<T>(string path, bool isCatch) where T : UnityEngine.Object
  50. {
  51. if (ht.Contains(path))
  52. {
  53. return ht[path] as T;
  54. }
  55. T TResource = Resources.Load<T>(path);
  56. if (TResource == null)
  57. {
  58. Debug.LogError(GetType() + "/GetInstance()/TResource 提取的资源找不到,请检查。 path=" + path);
  59. }
  60. else if (isCatch)
  61. {
  62. ht.Add(path, TResource);
  63. }
  64. return TResource;
  65. }
  66. /// <summary>
  67. /// 调用资源(带对象缓冲技术)
  68. /// </summary>
  69. /// <param name="path"></param>
  70. /// <param name="isCatch"></param>
  71. /// <returns></returns>
  72. public GameObject LoadAsset(string path, bool isCatch)
  73. {
  74. GameObject goObj = LoadResource<GameObject>(path, isCatch);
  75. GameObject goObjClone = GameObject.Instantiate<GameObject>(goObj);
  76. if (goObjClone == null)
  77. {
  78. Debug.LogError(GetType() + "/LoadAsset()/克隆资源不成功,请检查。 path=" + path);
  79. }
  80. //goObj = null;//??????????
  81. return goObjClone;
  82. }
  83. }//Class_end
  84. }