123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class AssetObject : MonoBehaviour {
- //有名字代表应该被回收
- public string name;
- public string bundleName { get; private set; }
- public int bundleVersion { get; private set; }
- public delegate void DelayDelegate(GameObject obj, float time);
- public bool trailRendererCheck = false;
- public static Dictionary<string, int> assetRef = new Dictionary<string, int>();
- List<TrailRenderer> cachedTrailRenderer = null;
- public delegate void RemoveDelegate(string name, GameObject go);
- public static RemoveDelegate removeDelegate = null;
- List<TrailRenderer> getTrailRenderers()
- {
- if (cachedTrailRenderer == null)
- {
- TrailRenderer[] trailRenderers = gameObject.GetComponentsInChildren<TrailRenderer>(true);
- cachedTrailRenderer = new List<TrailRenderer>(trailRenderers);
- }
- return cachedTrailRenderer;
- }
- //void Awake()
- //{
- //}
-
- //// Use this for initialization
- //void Start ()
- //{
- //}
- public void AddRef(string name, int version)
- {
- int i;
- if(assetRef.TryGetValue(name, out i))
- {
- assetRef[name] = i + 1;
- }
- else
- {
- assetRef[name] = 1;
- }
- bundleName = name;
- bundleVersion = version;
- }
- private void RemoveRef()
- {
- int i;
- CommonUnity3D.XMUnity.LoadUtil.XMUnityAssetBundleManager mgr = CommonUnity3D.XMUnity.LoadUtil.XMUnityAssetBundleManager.GetInstance();
- if(mgr == null)
- {
- return;
- }
- var mfab = mgr.GetXMUnityAssetBundle(bundleName);
- if (mfab == null || mfab.Version != this.bundleVersion)
- {
- Debug.LogWarning("[策划测试请无视]RemoveRef version not match: " + name);
- return;
- }
- if (assetRef.TryGetValue(bundleName, out i))
- {
- int new_count = i - 1;
- assetRef[bundleName] = new_count;
- if(new_count == 0)
- {
- mgr.UnloadAssetBundle(bundleName, true);
- }
- }
- else
- {
- Debug.LogWarning("[策划测试请无视]RemoveRef error: " + name);
- }
- }
- void OnEnable()
- {
- if (trailRendererCheck)
- {
- List<TrailRenderer> trs = getTrailRenderers();
- for (int i = 0; i < trs.Count; i++)
- {
- trs[i].Clear();
- }
- }
- }
- IEnumerator StartTimer(DelayDelegate handle, float time)
- {
- yield return new WaitForSeconds(time);
- if (handle != null)
- {
- handle(this.gameObject, time);
- }
- }
- public void SetDelay(float time, DelayDelegate handle)
- {
- StartCoroutine(StartTimer(handle, time));
- }
- public void Destroy()
- {
- Destroy(gameObject);
- }
- public void Unload()
- {
- if (string.IsNullOrEmpty(name))
- {
- UnityEngine.Debug.Log("[策划测试请无视]Unload AssetObject Error: name null");
- }
- else
- {
- if(removeDelegate != null)
- {
- removeDelegate(name, gameObject);
- }
- else
- {
- UnityEngine.Debug.Log("[策划测试请无视]Unload AssetObject Error: removeDelegate null");
- }
- }
- }
- void OnDestroy()
- {
- if (string.IsNullOrEmpty(bundleName))
- {
- UnityEngine.Debug.LogError("[策划测试请无视]RemoveRef AssetObject Error: " + name);
- }
- else
- {
- RemoveRef();
- }
- }
- public static void ClearRefs()
- {
- assetRef.Clear();
- }
- //public static void ClearRef()
- //{
- // assetRef.Clear();
- //}
- //// Update is called once per frame
- //void Update () {
- //}
- }
|