/// <summary>
        /// 通用资源AssetBundle卸载方法[Unload(true)];
        /// </summary>
        /// <param name="type">资源类型</param>
        /// <param name="assetName">资源名字</param>
        public void UnloadAsset(AssetType type, string assetName)
        {
            if (type == AssetType.Non || type == AssetType.Shader || type == AssetType.Lua || type == AssetType.Scripts || string.IsNullOrEmpty(assetName))
            {
                return;
            }

            string assetBundleName = FilePathHelper.GetAssetBundleFileName(type, assetName);

            string[] DependentAssetBundle = Manifest.GetAllDependencies(assetBundleName);
            foreach (string tempAssetBundle in DependentAssetBundle)
            {
                if (tempAssetBundle == FilePathHelper.GetAssetBundleFileName(AssetType.Shader, "Shaders"))
                {
                    continue;
                }
                string tempPtah = FilePathHelper.AssetBundlePath + tempAssetBundle;
                UnloadAsset(tempPtah, true);
            }
            string assetBundlePath = FilePathHelper.GetAssetBundlePath(type, assetName);

            if (assetBundlePath != null)
            {
                UnloadAsset(assetBundlePath, true);
            }
        }
Exemple #2
0
        /// <summary>
        /// AssetBundle同步加载;
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public AssetBundle LoadFromFile(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }
            string assetBundlePath = FilePathHelper.GetAssetBundlePath(path);

            if (assetBundlePath == null)
            {
                return(null);
            }
            string assetBundleName = FilePathHelper.GetAssetBundleFileName(path);

            AssetBundle assetBundle = LoadSync(assetBundlePath);

            if (assetBundle == null)
            {
                return(null);
            }
            //返回AssetBundleName;
            string[] DependentAssetBundle = Manifest.GetAllDependencies(assetBundleName);
            foreach (string tempAssetBundle in DependentAssetBundle)
            {
                if (tempAssetBundle == FilePathHelper.GetAssetBundleFileName(FilePathHelper.shaderAssetBundleName) ||
                    tempAssetBundle == FilePathHelper.GetAssetBundleFileName(FilePathHelper.luaAssetBundleName))
                {
                    continue;
                }
                string tempPtah = FilePathHelper.AssetBundlePath + tempAssetBundle;
                LoadSync(tempPtah);
            }
            return(assetBundle);
        }
        /// <summary>
        /// AssetBundle同步加载;
        /// </summary>
        /// <param name="type">资源类型</param>
        /// <param name="assetName">资源名字</param>
        /// <returns>AssetBundle</returns>
        public AssetBundle LoadAssetBundleSync(AssetType type, string assetName)
        {
            if (type == AssetType.Non || string.IsNullOrEmpty(assetName))
            {
                return(null);
            }

            string assetBundlePath = FilePathHelper.GetAssetBundlePath(type, assetName);

            if (assetBundlePath == null)
            {
                return(null);
            }
            string assetBundleName = FilePathHelper.GetAssetBundleFileName(type, assetName);

            AssetBundle assetBundle = LoadAssetBundleSync(assetBundlePath);

            if (assetBundle == null)
            {
                return(null);
            }

            //返回AssetBundleName;
            string[] DependentAssetBundle = Manifest.GetAllDependencies(assetBundleName);
            foreach (string tempAssetBundle in DependentAssetBundle)
            {
                if (tempAssetBundle == FilePathHelper.GetAssetBundleFileName(AssetType.Shader, "Shaders"))
                {
                    continue;
                }
                string tempPtah = FilePathHelper.AssetBundlePath + tempAssetBundle;
                LoadAssetBundleSync(tempPtah);
            }
            return(assetBundle);
        }
Exemple #4
0
        /// <summary>
        /// AssetBundle异步加载;
        /// </summary>
        /// <param name="path"></param>
        /// <param name="action"></param>
        /// <param name="progress"></param>
        /// <returns></returns>
        public IEnumerator <float> LoadFromFileAsync(string path, Action <AssetBundle> action, Action <float> progress)
        {
            if (string.IsNullOrEmpty(path))
            {
                yield break;
            }
            string assetBundlePath = FilePathHelper.GetAssetBundlePath(path);

            if (assetBundlePath == null)
            {
                yield break;
            }
            string assetBundleName = FilePathHelper.GetAssetBundleFileName(path);

            //先加载依赖的AssetBundle;
            string[] DependentAssetBundle = Manifest.GetAllDependencies(assetBundleName);
            float    count   = DependentAssetBundle.Length;
            var      precent = ResourceMgr.Instance.LOAD_BUNDLE_PRECENT;
            float    unit    = precent / (count + 1);
            int      index   = 0;

            foreach (string tempAssetBundle in DependentAssetBundle)
            {
                float dp = 0f;
                if (tempAssetBundle == FilePathHelper.GetAssetBundleFileName(FilePathHelper.shaderAssetBundleName) ||
                    tempAssetBundle == FilePathHelper.GetAssetBundleFileName(FilePathHelper.luaAssetBundleName))
                {
                    continue;
                }
                string tempPtah          = FilePathHelper.AssetBundlePath + tempAssetBundle;
                IEnumerator <float> itor = LoadAsync(tempPtah, null, (value) => { dp = value; });
                while (itor.MoveNext())
                {
                    if (progress != null)
                    {
                        progress(unit * (index + dp));
                    }
                    yield return(Timing.WaitForOneFrame);
                }
                index++;
            }
            //加载目标AssetBundle;
            float p = 0f;
            IEnumerator <float> itorTarget = LoadAsync(assetBundlePath, action, (value) => { p = value; });

            while (itorTarget.MoveNext())
            {
                if (progress != null)
                {
                    progress(unit * (count + p));
                }
                yield return(Timing.WaitForOneFrame);
            }
        }
        /// <summary>
        /// AssetBundle异步加载;
        /// </summary>
        /// <param name="type">资源类型</param>
        /// <param name="assetName">资源名字</param>
        /// <param name="action">AssetBundle回调</param>
        /// <param name="progress">progress回调</param>
        /// <returns></returns>
        public IEnumerator <float> LoadAssetBundleAsync(AssetType type, string assetName, Action <AssetBundle> action, Action <float> progress)
        {
            if (type == AssetType.Non || string.IsNullOrEmpty(assetName))
            {
                yield break;
            }
            string assetBundlePath = FilePathHelper.GetAssetBundlePath(type, assetName);

            if (assetBundlePath == null)
            {
                yield break;
            }
            string assetBundleName = FilePathHelper.GetAssetBundleFileName(type, assetName);

            //先加载依赖的AssetBundle;
            string[] DependentAssetBundle = Manifest.GetAllDependencies(assetBundleName);
            float    count = DependentAssetBundle.Length;
            float    unit  = 0.9f / (count + 1);
            int      index = 0;

            foreach (string tempAssetBundle in DependentAssetBundle)
            {
                float dp = 0f;
                if (tempAssetBundle == FilePathHelper.GetAssetBundleFileName(AssetType.Shader, "Shaders"))
                {
                    continue;
                }
                string tempPtah          = FilePathHelper.AssetBundlePath + tempAssetBundle;
                IEnumerator <float> itor = LoadAsync(tempPtah, null, (value) => { dp = value; });
                while (itor.MoveNext())
                {
                    if (progress != null)
                    {
                        progress(unit * (index + dp));
                    }
                    yield return(Timing.WaitForOneFrame);
                }
                index++;
            }
            //加载目标AssetBundle;
            float p = 0f;
            IEnumerator <float> itorTarget = LoadAsync(assetBundlePath, action, (value) => { p = value; });

            while (itorTarget.MoveNext())
            {
                if (progress != null)
                {
                    progress(unit * (count + p));
                }
                yield return(Timing.WaitForOneFrame);
            }
        }
Exemple #6
0
        /// <summary>
        /// AssetBundle 镜像卸载方法[Unload(false)];
        /// 使用资源为一般初始化就全局保存不在销毁的资源,如:Shader;
        /// </summary>
        /// <param name="path"></param>
        public void UnloadMirroring(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            string assetBundlePath = FilePathHelper.GetAssetBundlePath(path);

            if (assetBundlePath != null)
            {
                UnloadAsset(assetBundlePath, false);
            }
        }
        /// <summary>
        /// AssetBundle 镜像卸载方法[Unload(false)],使用资源为一般初始化就全局保存不在销毁的资源,如:Shader;
        /// </summary>
        /// <param name="type">资源类型</param>
        /// <param name="assetName">资源名字</param>
        public void UnloadMirroring(AssetType type, string assetName)
        {
            if (type == AssetType.Non || type == AssetType.Scripts || string.IsNullOrEmpty(assetName))
            {
                return;
            }
            string assetBundlePath = FilePathHelper.GetAssetBundlePath(type, assetName);

            if (assetBundlePath != null)
            {
                UnloadAsset(assetBundlePath, false);
            }
        }
        /// <summary>
        /// AssetBundle异步加载;
        /// </summary>
        /// <param name="path"></param>
        /// <param name="action"></param>
        /// <param name="progress"></param>
        /// <returns></returns>
        public IEnumerator <float> LoadFromFileAsync(string path, Action <AssetBundle> action, Action <float> progress)
        {
            if (string.IsNullOrEmpty(path))
            {
                yield break;
            }
            var assetBundlePath = FilePathHelper.GetAssetBundlePath(path);

            if (assetBundlePath == null)
            {
                yield break;
            }
            var assetBundleName = FilePathHelper.GetAssetBundleFileName(path);
            //先加载依赖的AssetBundle;
            var dependentAssetBundle = Manifest.GetAllDependencies(assetBundleName);
            var count   = dependentAssetBundle.Length;
            var precent = ResourceMgr.singleton.LOAD_BUNDLE_PRECENT;
            var unit    = precent / (count + 1);
            int index   = 0;

            foreach (var tempAssetBundle in dependentAssetBundle)
            {
                var dp = 0f;
                if (tempAssetBundle == FilePathHelper.GetAssetBundleFileName(FilePathHelper.shaderAssetBundleName) ||
                    tempAssetBundle == FilePathHelper.GetAssetBundleFileName(FilePathHelper.luaAssetBundleName))
                {
                    continue;
                }
                var tempPtah = $"{FilePathHelper.AssetBundlePath}/{tempAssetBundle}";
                var itor     = LoadAsync(tempPtah, null, (value) => { dp = value; });
                while (itor.MoveNext())
                {
                    progress?.Invoke(unit * (index + dp));
                    yield return(Timing.WaitForOneFrame);
                }
                index++;
            }
            //加载目标AssetBundle;
            var p          = 0f;
            var itorTarget = LoadAsync(assetBundlePath, action, (value) => { p = value; });

            while (itorTarget.MoveNext())
            {
                progress?.Invoke(unit * (count + p));
                yield return(Timing.WaitForOneFrame);
            }
        }
Exemple #9
0
        /// <summary>
        /// 通用资源AssetBundle卸载方法[Unload(true)];
        /// </summary>
        /// <param name="path"></param>
        /// <param name="asset"></param>
        public void UnloadAsset(string path, Object asset)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (asset != null)
            {
                List <WeakReference> list;
                if (_assetBundleRefDict.TryGetValue(path, out list))
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        if ((Object)(list[i].Target) == asset)
                        {
                            list.RemoveAt(i);
                            //需要UnloadAsset?
                            //Resources.UnloadAsset(asset);
                            break;
                        }
                    }
                }
            }

            string assetBundleName = FilePathHelper.GetAssetBundleFileName(path);

            string[] DependentAssetBundle = Manifest.GetAllDependencies(assetBundleName);
            foreach (string tempAssetBundle in DependentAssetBundle)
            {
                if (tempAssetBundle == FilePathHelper.GetAssetBundleFileName(FilePathHelper.shaderAssetBundleName))
                {
                    continue;
                }
                string tempPtah = FilePathHelper.AssetBundlePath + tempAssetBundle;
                UnloadAsset(tempPtah, true);
            }
            string assetBundlePath = FilePathHelper.GetAssetBundlePath(path);

            if (assetBundlePath != null)
            {
                UnloadAsset(assetBundlePath, true);
            }
        }
        public AssetBundleLoadNode GetAssetBundleLoadNode(AssetType assetType, string assetName)
        {
            if (assetType == AssetType.Non || string.IsNullOrEmpty(assetName))
            {
                return(null);
            }
            string assetBundlePath = FilePathHelper.GetAssetBundlePath(assetType, assetName);

            if (assetBundlePath == null)
            {
                return(null);
            }
            string assetBundleName = FilePathHelper.GetAssetBundleFileName(assetType, assetName);

            Queue <AssetBundleLoadNode> nodeQueue = null;

            //返回AssetBundleName;
            string[] DependentAssetBundle = Manifest.GetAllDependencies(assetBundleName);
            if (DependentAssetBundle.Length > 0)
            {
                nodeQueue = new Queue <AssetBundleLoadNode>();
            }
            foreach (string tempAssetBundle in DependentAssetBundle)
            {
                if (tempAssetBundle == FilePathHelper.GetAssetBundleFileName(AssetType.Shader, "Shaders"))
                {
                    continue;
                }
                string tempPtah = FilePathHelper.AssetBundlePath + tempAssetBundle;
                AssetBundleLoadNode tempNode = PoolMgr.Instance.GetCsharpObject <AssetBundleLoadNode>();
                tempNode.Init(tempPtah);
                nodeQueue.Enqueue(tempNode);
            }
            AssetBundleLoadNode node = PoolMgr.Instance.GetCsharpObject <AssetBundleLoadNode>();

            node.Init(assetBundlePath, nodeQueue);
            return(node);
        }
        public Object GetUnityAsset(AssetType assetType, string assetName)
        {
            string path = FilePathHelper.GetAssetBundlePath(assetType, assetName);
            UnityAssetCacheInfo info = null;

            if (!string.IsNullOrEmpty(path) && _unityAssetCacheDict.TryGetValue(path, out info))
            {
                int count = 0;
                if (!_unityAssetRefCountDict.TryGetValue(path, out count))
                {
                    count = 0;
                }
                count++;
                _unityAssetRefCountDict[path] = count;
            }
            if (null == info)
            {
                return(null);
            }
            else
            {
                return(info.target);
            }
        }
        public AssetBundle LoadLuaAssetBundle()
        {
            string path = FilePathHelper.GetAssetBundlePath(AssetType.Lua, "lua");

            return(LoadAssetBundleSync(path));
        }
        /// <summary>
        /// 加载Shader AssetBundle;
        /// </summary>
        /// <returns>AssetBundle</returns>
        public AssetBundle LoadShaderAssetBundle()
        {
            string path = FilePathHelper.GetAssetBundlePath(AssetType.Shader, "Shaders");

            return(LoadAssetBundleSync(path));
        }
        public void ReleaseUnityAsset(AssetType assetType, string assetName, Object obj, bool isUsePool)
        {
            string path = FilePathHelper.GetAssetBundlePath(assetType, assetName);

            if (null == obj)
            {
                LogHelper.PrintError(string.Format("[UnityAssetCachePool]Release unity asset:{0} error" +
                                                   ",Object is null!", path));
                return;
            }
            if (string.IsNullOrEmpty(path))
            {
                ResourceMgr.Instance.UnloadUnityAssetMemory(assetType, obj);
                return;
            }
            UnityAssetCacheInfo info = null;

            if (_unityAssetCacheDict.TryGetValue(path, out info))
            {
                if (info.target != obj)
                {
                    LogHelper.PrintError(string.Format("[UnityAssetCachePool]Release unity asset:{0} error" +
                                                       ",the path ref multiple Object!", path));
                    ResourceMgr.Instance.UnloadUnityAssetMemory(assetType, obj);
                    return;
                }
                int count = 0;
                if (!_unityAssetRefCountDict.TryGetValue(path, out count))
                {
                    LogHelper.PrintError(string.Format("[UnityAssetCachePool]Release unity asset:{0} error" +
                                                       ",ref count not exist!", path));
                    _unityAssetRefCountDict[path] = 0;
                }
                else
                {
                    _unityAssetRefCountDict[path]--;
                    if (!isUsePool)
                    {
                        //引用为0,又不是初次放入,直接回收;
                        if (_unityAssetRefCountDict[path] == 0)
                        {
                            _unityAssetCacheDict.Remove(path);
                            _unityAssetRefCountDict.Remove(path);
                            ResourceMgr.Instance.UnloadUnityAssetMemory(assetType, info.target);
                            PoolMgr.Instance.ReleaseCsharpObject <UnityAssetCacheInfo>(info);
                            AssetBundleMgr.Instance.UnloadAsset(assetType, assetName);
                        }
                    }
                }
            }
            else
            {
                info = PoolMgr.Instance.GetCsharpObject <UnityAssetCacheInfo>();
                info.Init(assetType, assetName, obj);
                _unityAssetCacheDict[path] = info;
                int count = 0;
                if (_unityAssetRefCountDict.TryGetValue(path, out count))
                {
                    LogHelper.PrintError(string.Format("[UnityAssetCachePool]Release unity asset:{0} error" +
                                                       ",ref count != 0!", path));
                }
                _unityAssetRefCountDict[path] = 0;
            }
        }