EditItemTextureCache.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class EditItemTextureCache : MonoBehaviour
  6. {
  7. private void OnDestroy()
  8. {
  9. foreach (KeyValuePair<int, Texture2D> keyValuePair in this.loadTex)
  10. {
  11. UnityEngine.Object.Destroy(keyValuePair.Value);
  12. }
  13. }
  14. private IEnumerator ProcLoad()
  15. {
  16. float endTime = Time.realtimeSinceStartup + 0.003f;
  17. while (0 < this.preloadItems.Count)
  18. {
  19. int rid = this.preloadItems[0];
  20. if (!this.loadTex.ContainsKey(rid))
  21. {
  22. try
  23. {
  24. this.loadTex.Add(rid, ImportCM.CreateTexture(this.ridToFileNameDic[rid]));
  25. }
  26. catch (Exception)
  27. {
  28. }
  29. this.preloadItems.RemoveAt(0);
  30. if (endTime <= Time.realtimeSinceStartup)
  31. {
  32. yield return null;
  33. endTime = Time.realtimeSinceStartup + 0.003f;
  34. }
  35. }
  36. else
  37. {
  38. this.preloadItems.RemoveAt(0);
  39. }
  40. }
  41. this.loadProcessOperation = false;
  42. yield break;
  43. }
  44. public Texture2D GetTexter(int rid)
  45. {
  46. Texture2D texture2D = null;
  47. if (!this.loadTex.TryGetValue(rid, out texture2D))
  48. {
  49. if (this.IsRegister(rid))
  50. {
  51. texture2D = ImportCM.CreateTexture(this.ridToFileNameDic[rid]);
  52. }
  53. this.loadTex.Add(rid, texture2D);
  54. }
  55. return texture2D;
  56. }
  57. public bool IsRegister(int rid)
  58. {
  59. return this.ridToFileNameDic.ContainsKey(rid);
  60. }
  61. public void PreLoadRegister(int rid, string fileName)
  62. {
  63. if (!this.loadTex.ContainsKey(rid))
  64. {
  65. this.preloadItems.Add(rid);
  66. this.ridToFileNameDic[rid] = fileName;
  67. }
  68. }
  69. public void Update()
  70. {
  71. if (!this.loadProcessOperation && 0 < this.preloadItems.Count)
  72. {
  73. this.loadProcessOperation = true;
  74. base.StartCoroutine(this.ProcLoad());
  75. }
  76. }
  77. private Dictionary<int, Texture2D> loadTex = new Dictionary<int, Texture2D>();
  78. private List<int> preloadItems = new List<int>();
  79. private Dictionary<int, string> ridToFileNameDic = new Dictionary<int, string>();
  80. private bool loadProcessOperation;
  81. }