AssetObject.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class AssetObject : MonoBehaviour {
  5. //有名字代表应该被回收
  6. public string name;
  7. public string bundleName { get; private set; }
  8. public int bundleVersion { get; private set; }
  9. public delegate void DelayDelegate(GameObject obj, float time);
  10. public bool trailRendererCheck = false;
  11. public static Dictionary<string, int> assetRef = new Dictionary<string, int>();
  12. List<TrailRenderer> cachedTrailRenderer = null;
  13. public delegate void RemoveDelegate(string name, GameObject go);
  14. public static RemoveDelegate removeDelegate = null;
  15. List<TrailRenderer> getTrailRenderers()
  16. {
  17. if (cachedTrailRenderer == null)
  18. {
  19. TrailRenderer[] trailRenderers = gameObject.GetComponentsInChildren<TrailRenderer>(true);
  20. cachedTrailRenderer = new List<TrailRenderer>(trailRenderers);
  21. }
  22. return cachedTrailRenderer;
  23. }
  24. //void Awake()
  25. //{
  26. //}
  27. //// Use this for initialization
  28. //void Start ()
  29. //{
  30. //}
  31. public void AddRef(string name, int version)
  32. {
  33. int i;
  34. if(assetRef.TryGetValue(name, out i))
  35. {
  36. assetRef[name] = i + 1;
  37. }
  38. else
  39. {
  40. assetRef[name] = 1;
  41. }
  42. bundleName = name;
  43. bundleVersion = version;
  44. }
  45. private void RemoveRef()
  46. {
  47. int i;
  48. CommonUnity3D.XMUnity.LoadUtil.XMUnityAssetBundleManager mgr = CommonUnity3D.XMUnity.LoadUtil.XMUnityAssetBundleManager.GetInstance();
  49. if(mgr == null)
  50. {
  51. return;
  52. }
  53. var mfab = mgr.GetXMUnityAssetBundle(bundleName);
  54. if (mfab == null || mfab.Version != this.bundleVersion)
  55. {
  56. Debug.LogWarning("[策划测试请无视]RemoveRef version not match: " + name);
  57. return;
  58. }
  59. if (assetRef.TryGetValue(bundleName, out i))
  60. {
  61. int new_count = i - 1;
  62. assetRef[bundleName] = new_count;
  63. if(new_count == 0)
  64. {
  65. mgr.UnloadAssetBundle(bundleName, true);
  66. }
  67. }
  68. else
  69. {
  70. Debug.LogWarning("[策划测试请无视]RemoveRef error: " + name);
  71. }
  72. }
  73. void OnEnable()
  74. {
  75. if (trailRendererCheck)
  76. {
  77. List<TrailRenderer> trs = getTrailRenderers();
  78. for (int i = 0; i < trs.Count; i++)
  79. {
  80. trs[i].Clear();
  81. }
  82. }
  83. }
  84. IEnumerator StartTimer(DelayDelegate handle, float time)
  85. {
  86. yield return new WaitForSeconds(time);
  87. if (handle != null)
  88. {
  89. handle(this.gameObject, time);
  90. }
  91. }
  92. public void SetDelay(float time, DelayDelegate handle)
  93. {
  94. StartCoroutine(StartTimer(handle, time));
  95. }
  96. public void Destroy()
  97. {
  98. Destroy(gameObject);
  99. }
  100. public void Unload()
  101. {
  102. if (string.IsNullOrEmpty(name))
  103. {
  104. UnityEngine.Debug.Log("[策划测试请无视]Unload AssetObject Error: name null");
  105. }
  106. else
  107. {
  108. if(removeDelegate != null)
  109. {
  110. removeDelegate(name, gameObject);
  111. }
  112. else
  113. {
  114. UnityEngine.Debug.Log("[策划测试请无视]Unload AssetObject Error: removeDelegate null");
  115. }
  116. }
  117. }
  118. void OnDestroy()
  119. {
  120. if (string.IsNullOrEmpty(bundleName))
  121. {
  122. UnityEngine.Debug.LogError("[策划测试请无视]RemoveRef AssetObject Error: " + name);
  123. }
  124. else
  125. {
  126. RemoveRef();
  127. }
  128. }
  129. public static void ClearRefs()
  130. {
  131. assetRef.Clear();
  132. }
  133. //public static void ClearRef()
  134. //{
  135. // assetRef.Clear();
  136. //}
  137. //// Update is called once per frame
  138. //void Update () {
  139. //}
  140. }