#region Copyright RenGuiYou. All rights reserved. //===================================================== // NeatlyFrameWork // Author: RenGuiyou // Feedback: mailto:750539605@qq.com //===================================================== #endregion using System; using System.Collections.Generic; using UnityEngine; using Object = UnityEngine.Object; namespace Neatly.Load { public abstract class Loader : ILoadObject { //加载结果 public Object ResultObject { get; private set; } private Dictionary m_AssetPool; private Dictionary AssetPool { get { return m_AssetPool ?? (m_AssetPool = new Dictionary()); } } //是否完成(异步使用) public bool IsCompleted { get; private set; } //是否错误(文件修复) public bool IsError { get; private set; } public void Unload(bool unload) { var bundle = ResultObject as AssetBundle; if (bundle != null) { bundle.Unload(unload); } } public bool ContainAsset(string assetName) { return AssetPool.ContainsKey(assetName); } //引用数 public int RefCount { get; set; } //依赖数 public int DependCount { get; set; } //路径,也是key public string Path { get; private set; } //是否常驻内存 public bool Persistent { get; private set; } //存在时间 public float LiveTime { get; set; } #region Atlas //是否图集bundle public bool IsAtlas { get; set; } public string AtlasName { get; set; } //图集名 public NAtlas AtlasInfo { get; set; } #endregion //是否同步 protected bool m_Sync; public bool IsSync { get { return m_Sync; } } //回调 public Action m_callBack; //初始化 protected virtual void Init(string path, Action callback, bool sync = true) { Persistent = LoaderConfig.CheckPersistent(path); RefCount = 0; DependCount = 0; LiveTime = Time.realtimeSinceStartup; IsCompleted = false; IsError = false; m_callBack = callback; Path = path; m_Sync = sync; } public virtual void Load() { } public virtual void Update() { } protected void OnLoadCompleted(Object data) { ResultObject = data; IsCompleted = true; if (m_callBack != null) { m_callBack(this); } } //添加引用计数 public void AddRefenceCount() { RefCount++; } //移除引用计数 public void LoseRefenceCount() { RefCount--; if (RefCount <= 0 && DependCount <= 0) { LiveTime = Time.realtimeSinceStartup; } } //添加依赖计数 public void AddDependCount() { DependCount++; } //移除依赖计数 public void LoseDependCount() { DependCount--; if (RefCount <= 0 && DependCount <= 0) { LiveTime = Time.realtimeSinceStartup; } } //设置是否常驻 public void SetPersistent(bool isPersistent) { if (isPersistent) { Persistent = true; } } public static Loader AutoLoad(string path, Action callback = null, bool sync = true) where T : Loader, new() { T t = new T(); t.Init(path, callback, sync); t.Load(); return t; } public Object LoadAsset(string name, Type type) { Object asset = null; //添加引用计数 AddRefenceCount(); if (AssetPool.TryGetValue(name, out asset)) { return asset; } var bundle = ResultObject as AssetBundle; Developer.StartLoadAsset(name); asset = bundle.LoadAsset(name, type); Developer.EndLoadAsset(name); AssetPool.Add(name, asset); return asset; } } }