| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 | using System;using System.Collections;using System.Collections.Generic;using Object = UnityEngine.Object;namespace VEngine{    public class Asset : Loadable, IEnumerator    {        public static readonly Dictionary<string, Asset> Cache = new Dictionary<string, Asset>();        public static readonly Dictionary<string, int> CacheCount = new Dictionary<string, int>();        public static readonly List<Asset> Unused = new List<Asset>();        public Action<Asset> completed;        public Object asset { get; protected set; }        protected Type type { get; set; }        public bool MoveNext()        {            return !isDone;        }        public void Reset()        {        }        protected void OnLoaded(Object target)        {            asset = target;            Finish(asset == null ? "asset == null" : null);        }        public object Current => null;        public T Get<T>() where T : Object        {            return asset as T;        }        protected override void OnComplete()        {            if (completed == null) return;            var saved = completed;            if (completed != null) completed(this);            completed -= saved;        }        protected override void OnUnused()        {            completed = null;            Unused.Add(this);        }        public static Asset LoadAsync(string path, Type type, Action<Asset> completed = null)        {            return LoadInternal(path, type, false, completed);        }        public static Asset Load(string path, Type type)        {            return LoadInternal(path, type, true);        }        internal static Asset LoadInternal(string path, Type type, bool mustCompleteOnNextFrame,            Action<Asset> completed = null)        {            if (!Versions.Contains(path))            {                Logger.E("FileNotFoundException {0}", path);                return null;            }            if (!Cache.TryGetValue(path, out var item))            {                item = Versions.CreateAsset(path, type);                Cache.Add(path, item);            }            // if (CacheCount.ContainsKey(path))            // {            //     CacheCount[path] = CacheCount[path] + 1;            // }            // else            // {            //     CacheCount.Add(path, 1);            // }            if (completed != null) item.completed += completed;            item.mustCompleteOnNextFrame = mustCompleteOnNextFrame;            item.Load();            if (mustCompleteOnNextFrame) item.LoadImmediate();            return item;        }        public static void UpdateAssets()        {            for (var index = 0; index < Unused.Count; index++)            {                var item = Unused[index];                if (!item.isDone) continue;                Unused.RemoveAt(index);                index--;                if (!item.reference.unused) continue;                item.Unload();                Cache.Remove(item.pathOrURL);            }        }    }}
 |