TextAssetManager.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine.UI;
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class TextAssetManager : MonoSingleton<TextAssetManager>
  10. {
  11. /// <summary>
  12. /// 资源
  13. /// </summary>
  14. public Dictionary<string, TextAsset> mIconRes = new Dictionary<string, TextAsset>();
  15. /// <summary>
  16. /// 初始化
  17. /// </summary>
  18. private void Start()
  19. {
  20. mIconRes.Clear();
  21. //LoadRes("GateDialog", () =>
  22. //{
  23. //});
  24. }
  25. /// <summary>
  26. /// 加载资源
  27. /// </summary>
  28. /// <param name="bundleName"> 资源包的名称 </param>
  29. /// <param name="ac"> 加载完后执行的回调 </param>
  30. private void LoadRes(string bundleName, Action ac)
  31. {
  32. ResourceHelper.Instance.LoadAssetBundle(bundleName, (AssetBundle bundle) =>
  33. {
  34. if (null != bundle)
  35. {
  36. GameObject go = (GameObject)bundle.LoadAsset(bundleName);
  37. UnityEngine.Object[] sprObj = bundle.LoadAllAssets(typeof(TextAsset));
  38. foreach (UnityEngine.Object obj in sprObj)
  39. {
  40. mIconRes[obj.name] = (TextAsset)obj;
  41. }
  42. }
  43. ac();
  44. });
  45. }
  46. /// <summary>
  47. /// 获取资源
  48. /// </summary>
  49. /// <param name="iconContainer">容器bundle名称</param>
  50. /// <param name="resName"> 查找的名称 </param>
  51. /// <param name="callBack">赋值成功后回调</param>
  52. public void GetTextAsset(string iconContainer, string resName, Action<TextAsset> callBack)
  53. {
  54. string bundleName = resName;
  55. if (mIconRes.ContainsKey(bundleName) && mIconRes[bundleName] != null)
  56. {
  57. if (callBack != null)
  58. {
  59. callBack(mIconRes[bundleName]);
  60. }
  61. }
  62. else
  63. {
  64. LoadRes(IconPrefixConst.getIconContainerName(iconContainer), () =>
  65. {
  66. if (mIconRes.ContainsKey(bundleName))
  67. {
  68. if (callBack != null)
  69. {
  70. callBack(mIconRes[bundleName]);
  71. }
  72. }
  73. else
  74. {
  75. callBack(null);
  76. }
  77. });
  78. }
  79. }
  80. /// <summary>
  81. /// 对话
  82. /// </summary>
  83. public const string GateDialogBundleName = "GateDialog";
  84. }