Beispiel #1
0
        private void ParseResData(ABDataList dataList)
        {
            mABDict    = new Dictionary <string, ResName>();
            mAssetDict = new Dictionary <string, List <ResName> >();

            foreach (ABData abInfo in dataList.abInfos)
            {
                ResName abName = new ResName(GroupName, abInfo.name, null, null);
                mABDict.Add(abName.ABName, abName);

                foreach (string asset in abInfo.assets)
                {
                    ResName        assetName = new ResName(GroupName, abInfo.name, Path.GetFileNameWithoutExtension(asset), Path.GetExtension(asset));
                    List <ResName> resList;
                    if (!mAssetDict.TryGetValue(assetName.AssetName, out resList))
                    {
                        resList = new List <ResName>();
                        mAssetDict.Add(assetName.AssetName, resList);
                    }
                    else if (resList.Exists(r => r.Equals(assetName)))
                    {
                        throw new Exception(string.Format(">>>[ResKit]: assetbundle \"{0}\" 中存在同名且同后缀文件: {1}", assetName.ABName,
                                                          assetName.AssetNameWithExtension));
                    }
                    resList.Add(assetName);
                }
            }
        }
 public void Init(ResName resName)
 {
     Name     = resName;
     State    = eResState.Waiting;
     RefCount = 0;
     OnInit();
 }
Beispiel #3
0
        private AbstractRes AddToLoad(ResName resName)
        {
            AbstractRes res = mResList.Find(r => r.Name.Equals(resName));

            if (res != null)
            {
                return(res);
            }

            res = ResMgr.Instance.GetRes(resName, true);

            //无论该资源是否加载完成,都需要添加对该资源依赖的引用
            ResName[] depends = res.GetDependencies();
            if (depends != null)
            {
                for (int i = 0; i < depends.Length; i++)
                {
                    AddToLoad(depends[i]);
                }
            }

            //add to list
            res.AddRef();
            mResList.Add(res);

            if (res.State != eResState.Ready)
            {
                mWaitLoadList.AddLast(res);
            }
            return(res);
        }
Beispiel #4
0
        public override bool Equals(object obj)
        {
            ResName target = obj as ResName;

            if (target == null)
            {
                return(false);
            }
            return(target.FullName.Equals(this.FullName));
        }
        public ResName GetResName(string assetName, string abName)
        {
            if (string.IsNullOrEmpty(assetName) && string.IsNullOrEmpty(abName))
            {
                return(null);
            }

            //file name should keep letter case
            if (!string.IsNullOrEmpty(assetName) && ResName.NotABAsset(assetName))
            {
                int index = assetName.LastIndexOf('.');
                if (index < 0)
                {
                    return(new ResName(null, assetName, null));
                }
                return(new ResName(null, assetName.Substring(0, index), assetName.Substring(index)));
            }

            if (!string.IsNullOrEmpty(assetName))
            {
                assetName = assetName.ToLower();
            }
            if (!string.IsNullOrEmpty(abName))
            {
                abName = abName.ToLower();
            }

            ResName resName = null;
            SearchResNameFailInfo failInfo = null;

            for (int i = 0; i < mResGroups.Count; i++)
            {
                resName = mResGroups[i].GetResName(assetName, abName, out failInfo);
                if (resName != null)
                {
                    return(resName);
                }

                if (failInfo.FailType == SearchResNameFailInfo.NAME_AMBIGUOUS)
                {
                    throw new Exception(string.Format(failInfo.ToString(), assetName));
                }
            }
            if (failInfo.FailType == SearchResNameFailInfo.NOTFOUND_AB)
            {
                throw new Exception(string.Format(failInfo.ToString(), abName));
            }
            if (failInfo.FailType == SearchResNameFailInfo.NOTFOUND_ASSET)
            {
                throw new Exception(string.Format(failInfo.ToString(), assetName));
            }
            return(null);
        }
        private ResLoader GetResLoader(ResName resName, bool allocate = true)
        {
            ResLoader loader;

            if (!mResLoaderDict.TryGetValue(resName.FullName, out loader))
            {
                if (allocate)
                {
                    loader = ResLoader.Allocate();
                    mResLoaderDict.Add(resName.FullName, loader);
                }
            }
            return(loader);
        }
        /// <summary>
        /// release resource, also release all its dependencies
        /// </summary>
        /// <param name="name">res name with extension, e.g. "a.prefab"</param>
        /// <param name="abName">optional assetbundle name, in case of same naming.</param>
        public static void Release(string name, string abName = null)
        {
            ResName   resName = ResDataMgr.Instance.GetResName(name, abName);
            ResLoader loader  = Instance.GetResLoader(resName, false);

            if (loader != null)
            {
                loader.Recycle2Cache();
                Instance.RemoveResLoader(resName);
                return;
            }

            Debug.LogFormat("<color=yellow> >>>[ResKit]- GlobalResLoader.Release: Can't find res {0} loaded.</color>", resName.FullName);
        }
Beispiel #8
0
        public static AbstractRes Create(ResName name)
        {
            AbstractRes res = null;

            switch (name.ResType)
            {
            case eResType.Assetbundle:
                res = SafeObjectPool <ABRes> .Instance.Allocate();

                break;

            case eResType.Asset:
                res = SafeObjectPool <AssetRes> .Instance.Allocate();

                break;

            case eResType.Scene:
                res = SafeObjectPool <SceneRes> .Instance.Allocate();

                break;

            case eResType.Internal:
                res = SafeObjectPool <InternalRes> .Instance.Allocate();

                break;

            case eResType.SingleFile:
                res = SafeObjectPool <SingleFileRes> .Instance.Allocate();

                break;
            }

            if (res == null)
            {
                throw new Exception(">>>[ResKit]: Create Res Error: " + name.FullName);
            }

            res.Init(name);
            return(res);
        }
Beispiel #9
0
        public AbstractRes GetRes(ResName resName, bool createNew = false)
        {
            AbstractRes res;

            if (mResDict.TryGetValue(resName.FullName, out res))
            {
                return(res);
            }

            if (!createNew)
            {
                return(null);
            }

            res = ResFactory.Create(resName);
            if (res != null)
            {
                mResDict.Add(resName.FullName, res);
                GetAllLoadedAssetBundles();
            }
            return(res);
        }
Beispiel #10
0
        public ResName[] GetABDepencecies(string abName, bool recursive = false)
        {
            if (!mABDict.ContainsKey(abName))
            {
                return(null);
            }

            string[] dependencies;

#if UNITY_EDITOR
            if (ResMgr.SimulationMode)
            {
                dependencies = UnityEditor.AssetDatabase.GetAssetBundleDependencies(abName, recursive);

                foreach (var group in ResKitConfig.GetResGroups())
                {
                    if (group.SimulateLoad && group.GroupName == GroupName && group.SimulateLoad)
                    {
                        dependencies = recursive ? mABManifest.GetAllDependencies(abName) : mABManifest.GetDirectDependencies(abName);
                    }
                }
            }
            else
            #endif
            {
                dependencies = recursive ? mABManifest.GetAllDependencies(abName) : mABManifest.GetDirectDependencies(abName);
            }

            ResName[] resNames = new ResName[dependencies.Length];
            for (int i = 0; i < dependencies.Length; i++)
            {
                SearchResNameFailInfo failInfo;
                resNames[i] = GetResName(null, dependencies[i], out failInfo);
            }
            return(resNames);
        }
Beispiel #11
0
        protected override void OnInit()
        {
            ResName abName = ResDataMgr.Instance.GetResName(null, Name.ABName);

            mABs = new[] { abName };
        }
Beispiel #12
0
 public T GetRes <T>(ResName resName, bool createNew = false) where T : AbstractRes
 {
     return(GetRes(resName, createNew) as T);
 }
Beispiel #13
0
        public ResName GetResName(string assetName, string abName, out SearchResNameFailInfo failInfo)
        {
            failInfo = null;

            if (string.IsNullOrEmpty(assetName))
            {
                ResName resName;
                if (mABDict.TryGetValue(abName, out resName))
                {
                    return(resName);
                }

                failInfo = new SearchResNameFailInfo(SearchResNameFailInfo.NOTFOUND_AB);
                return(null);
            }

            string nameWithoutExt = assetName;
            string ext            = null;
            int    index          = assetName.LastIndexOf('.');

            if (index > 0)
            {
                nameWithoutExt = assetName.Substring(0, index);
                ext            = assetName.Substring(index);
            }

            List <ResName> resList;

            if (!mAssetDict.TryGetValue(nameWithoutExt, out resList))
            {
                failInfo = new SearchResNameFailInfo(SearchResNameFailInfo.NOTFOUND_ASSET);
                return(null);
            }

            ResName result = null;

            foreach (ResName resName in resList)
            {
                if (!string.IsNullOrEmpty(abName) && !abName.Equals(resName.ABName, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(ext) && !ext.Equals(resName.Extension, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                if (result != null)
                {
                    failInfo = new SearchResNameFailInfo(SearchResNameFailInfo.NAME_AMBIGUOUS);
                    return(null);
                }

                result = resName;
            }
            if (result == null)
            {
                failInfo = new SearchResNameFailInfo(SearchResNameFailInfo.NOTFOUND_ASSET);
            }

            return(result);
        }
        /// <summary>
        /// Load sprite synchronously
        /// </summary>
        /// <param name="spriteName">sprite name with extension, e.g. "a.png"</param>
        /// <param name="abName">optional assetbundle name, in case of same naming.</param>
        public static Sprite LoadSprite(string spriteName, string abName = null)
        {
            ResName resName = ResDataMgr.Instance.GetResName(spriteName, abName);

            return(Instance.GetResLoader(resName).LoadSprite(spriteName, abName));
        }
        /// <summary>
        /// Load resource asynchronously
        /// </summary>
        /// <param name="name">res name with extension, e.g. "a.prefab"</param>
        /// <param name="abName">optional assetbundle name, in case of same naming.</param>
        /// <param name="onFinish">on finish callback, with param: success boolean, and loaded object.</param>
        public static void LoadAsync(string name, string abName = null, UnityAction <bool, object> onFinish = null)
        {
            ResName resName = ResDataMgr.Instance.GetResName(name, abName);

            Instance.GetResLoader(resName).LoadAsync(name, abName, onFinish);
        }
        /// <summary>
        /// Load resource synchronously
        /// </summary>
        /// <param name="name">res name with extension, e.g. "a.prefab"</param>
        /// <param name="abName">optional assetbundle name, in case of same naming.</param>
        public static object LoadSync(string name, string abName = null)
        {
            ResName resName = ResDataMgr.Instance.GetResName(name, abName);

            return(Instance.GetResLoader(resName).LoadSync(name, abName));
        }
 private void RemoveResLoader(ResName resName)
 {
     mResLoaderDict.Remove(resName.FullName);
 }
        /// <summary>
        /// Load from Resources folder synchronously
        /// </summary>
        public static object LoadFromResourcesSync(string name)
        {
            ResName resName = ResDataMgr.Instance.GetResName(ResName.RESOURCES_PREFIX + name, null);

            return(Instance.GetResLoader(resName).LoadFromResourcesSync(name));
        }
        /// <summary>
        /// Load file asynchronously, including: text, texture, bytes files
        /// </summary>
        public static void LoadFileAsync(string name, UnityAction <bool, FileContent> onFinish = null)
        {
            ResName resName = ResDataMgr.Instance.GetResName(ResName.FILE_PREFIX + name, null);

            Instance.GetResLoader(resName).LoadFileAsync(name, onFinish);
        }
        /// <summary>
        /// Load file synchronously, including: text, texture, bytes files
        /// </summary>
        public static FileContent LoadFileSync(string name)
        {
            ResName resName = ResDataMgr.Instance.GetResName(ResName.FILE_PREFIX + name, null);

            return(Instance.GetResLoader(resName).LoadFileSync(name));
        }
        /// <summary>
        /// Load from Resources folder asynchronously
        /// </summary>
        public static void LoadFromResourcesAsync <T>(string name, UnityAction <bool, T> onFinish = null) where  T : class
        {
            ResName resName = ResDataMgr.Instance.GetResName(ResName.RESOURCES_PREFIX + name, null);

            Instance.GetResLoader(resName).LoadFromResourcesAsync(name, onFinish);
        }
        /// <summary>
        /// Load from Resources folder synchronously
        /// </summary>
        public static T LoadFromResourcesSync <T>(string name) where T : class
        {
            ResName resName = ResDataMgr.Instance.GetResName(ResName.RESOURCES_PREFIX + name, null);

            return(Instance.GetResLoader(resName).LoadFromResourcesSync <T>(name));
        }