ResourceManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.SceneManagement;
  7. namespace I2.Loc
  8. {
  9. public class ResourceManager : MonoBehaviour
  10. {
  11. public static ResourceManager pInstance
  12. {
  13. get
  14. {
  15. bool flag = ResourceManager.mInstance == null;
  16. if (ResourceManager.mInstance == null)
  17. {
  18. ResourceManager.mInstance = (ResourceManager)UnityEngine.Object.FindObjectOfType(typeof(ResourceManager));
  19. }
  20. if (ResourceManager.mInstance == null)
  21. {
  22. GameObject gameObject = new GameObject("I2ResourceManager", new Type[]
  23. {
  24. typeof(ResourceManager)
  25. });
  26. gameObject.hideFlags |= HideFlags.HideAndDontSave;
  27. ResourceManager.mInstance = gameObject.GetComponent<ResourceManager>();
  28. if (ResourceManager.<>f__mg$cache0 == null)
  29. {
  30. ResourceManager.<>f__mg$cache0 = new UnityAction<Scene, LoadSceneMode>(ResourceManager.MyOnLevelWasLoaded);
  31. }
  32. SceneManager.sceneLoaded += ResourceManager.<>f__mg$cache0;
  33. }
  34. if (flag && Application.isPlaying)
  35. {
  36. UnityEngine.Object.DontDestroyOnLoad(ResourceManager.mInstance.gameObject);
  37. }
  38. return ResourceManager.mInstance;
  39. }
  40. }
  41. public static void MyOnLevelWasLoaded(Scene scene, LoadSceneMode mode)
  42. {
  43. ResourceManager.pInstance.CleanResourceCache();
  44. LocalizationManager.UpdateSources();
  45. }
  46. public T GetAsset<T>(string Name) where T : UnityEngine.Object
  47. {
  48. T t = this.FindAsset(Name) as T;
  49. if (t != null)
  50. {
  51. return t;
  52. }
  53. return this.LoadFromResources<T>(Name);
  54. }
  55. private UnityEngine.Object FindAsset(string Name)
  56. {
  57. if (this.Assets != null)
  58. {
  59. int i = 0;
  60. int num = this.Assets.Length;
  61. while (i < num)
  62. {
  63. if (this.Assets[i] != null && this.Assets[i].name == Name)
  64. {
  65. return this.Assets[i];
  66. }
  67. i++;
  68. }
  69. }
  70. return null;
  71. }
  72. public bool HasAsset(UnityEngine.Object Obj)
  73. {
  74. return this.Assets != null && Array.IndexOf<UnityEngine.Object>(this.Assets, Obj) >= 0;
  75. }
  76. public T LoadFromResources<T>(string Path) where T : UnityEngine.Object
  77. {
  78. T result;
  79. try
  80. {
  81. UnityEngine.Object @object;
  82. if (string.IsNullOrEmpty(Path))
  83. {
  84. result = (T)((object)null);
  85. }
  86. else if (this.mResourcesCache.TryGetValue(Path, out @object) && @object != null)
  87. {
  88. result = (@object as T);
  89. }
  90. else
  91. {
  92. T t = (T)((object)null);
  93. if (Path.EndsWith("]", StringComparison.OrdinalIgnoreCase))
  94. {
  95. int num = Path.LastIndexOf("[", StringComparison.OrdinalIgnoreCase);
  96. int length = Path.Length - num - 2;
  97. string value = Path.Substring(num + 1, length);
  98. Path = Path.Substring(0, num);
  99. T[] array = Resources.LoadAll<T>(Path);
  100. int i = 0;
  101. int num2 = array.Length;
  102. while (i < num2)
  103. {
  104. if (array[i].name.Equals(value))
  105. {
  106. t = array[i];
  107. break;
  108. }
  109. i++;
  110. }
  111. }
  112. else
  113. {
  114. t = (Resources.Load(Path, typeof(T)) as T);
  115. }
  116. if (t == null)
  117. {
  118. t = this.LoadFromBundle<T>(Path);
  119. }
  120. if (t != null)
  121. {
  122. this.mResourcesCache[Path] = t;
  123. }
  124. result = t;
  125. }
  126. }
  127. catch (Exception ex)
  128. {
  129. Debug.LogErrorFormat("Unable to load {0} '{1}'\nERROR: {2}", new object[]
  130. {
  131. typeof(T),
  132. Path,
  133. ex.ToString()
  134. });
  135. result = (T)((object)null);
  136. }
  137. return result;
  138. }
  139. public T LoadFromBundle<T>(string path) where T : UnityEngine.Object
  140. {
  141. int i = 0;
  142. int count = this.mBundleManagers.Count;
  143. while (i < count)
  144. {
  145. if (this.mBundleManagers[i] != null)
  146. {
  147. T t = this.mBundleManagers[i].LoadFromBundle(path, typeof(T)) as T;
  148. if (t != null)
  149. {
  150. return t;
  151. }
  152. }
  153. i++;
  154. }
  155. return (T)((object)null);
  156. }
  157. public void CleanResourceCache()
  158. {
  159. this.mResourcesCache.Clear();
  160. Resources.UnloadUnusedAssets();
  161. base.CancelInvoke();
  162. }
  163. private static ResourceManager mInstance;
  164. public List<IResourceManager_Bundles> mBundleManagers = new List<IResourceManager_Bundles>();
  165. public UnityEngine.Object[] Assets;
  166. private readonly Dictionary<string, UnityEngine.Object> mResourcesCache = new Dictionary<string, UnityEngine.Object>(StringComparer.Ordinal);
  167. [CompilerGenerated]
  168. private static UnityAction<Scene, LoadSceneMode> <>f__mg$cache0;
  169. }
  170. }