Ejemplo n.º 1
0
        private IEnumerator _loadAssetBundle(Resource_Assetbundle _assetbundle_data)
        {
            AssetBundle loaded_asset_bundle = null;
            //if( Application.platform == RuntimePlatform.Android && _assetbundle_data.ResouceInfo.Location == AssetLocation.StreamingPath )
            //{
            //	using( WWW www = new WWW( _assetbundle_data.ResouceInfo.UnityApiLoadingPath ) )
            //	{
            //		yield return www;

            //		if( www.error == null )
            //			loaded_asset_bundle = www.assetBundle;
            //		else
            //			Log.Error( eLogType.Resources, "Load Fail: {0}! Msg: {1}", _assetbundle_data.ResouceInfo.UnityApiLoadingPath, www.error );
            //	}
            //}
            //else
            //{
            AssetBundleCreateRequest bundle_load_request = AssetBundle.LoadFromFileAsync(_assetbundle_data.ResouceInfo.SystemApiLoadingPath);

            yield return(bundle_load_request);

            loaded_asset_bundle = bundle_load_request.assetBundle;
            if (loaded_asset_bundle == null)
            {
                Log.Error(eLogType.Resources, "Load Fail: {0}! Msg: Assetbundle 为空", _assetbundle_data.ResouceInfo.SystemApiLoadingPath);
            }
            //}
            _assetbundle_data.OnLoadFinished(loaded_asset_bundle, loaded_asset_bundle != null);
            m_current_loading = null;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 同步加载bundle中的一个资源
 /// </summary>
 /// <param name="_resouce"></param>
 /// <param name="_asset_name"></param>
 public UnityEngine.Object LoadAssetSync(Resource_Assetbundle _resouce, String _asset_name, Type _type)
 {
     UnityEngine.Object asset = _resouce.Assetbundle.LoadAsset(_asset_name, _type);
     //_type asset = _resouce.Assetbundle.LoadAsset<_type>( _asset_name );
     _resouce.OnAssetLoaded(asset);
     return(asset);
 }
Ejemplo n.º 3
0
 public UnityAssetLoadTask(Resource_Assetbundle _res_assetbundle, String _asset_name, Type _type, Predicate <Boolean, Resource_Assetbundle, UnityEngine.Object> _callback)
 {
     m_res_assetbundle = _res_assetbundle;
     m_asset_name      = _asset_name;
     m_callback        = _callback;
     m_asset_type      = _type;
     m_res_assetbundle.Retain();
 }
Ejemplo n.º 4
0
 public void AddDependencies(Resource_Assetbundle _res_assetbundle)
 {
     if (m_dependencies.Contains(_res_assetbundle))
     {
         Utility.Log.Error("[Resource_Assetbundle] Dependencies: {0} 已经被添加!", _res_assetbundle.Name);
         return;
     }
     m_dependencies.Add(_res_assetbundle);
 }
Ejemplo n.º 5
0
 public void Update()
 {
     if (m_current_loading != null || m_pending_asset.Count == 0)
     {
         return;
     }
     m_current_loading = m_pending_asset.Dequeue();
     StartCoroutine(_loadAssetBundle(m_current_loading));
 }
Ejemplo n.º 6
0
 public void LoadAssetbundle(Resource_Assetbundle _assetbundle)
 {
     // 等待中或者正在加载的被跳过
     if (m_pending_asset.Contains(_assetbundle) || m_current_loading == _assetbundle)
     {
         return;
     }
     m_pending_asset.Enqueue(_assetbundle);
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 处理release, 这里把根的对象传进来,避免循环依赖导致的死循环
 /// </summary>
 /// <param name="_root"></param>
 private void _release(Resource_Assetbundle _root)
 {
     for (int i = 0; i < m_dependencies.Count; i++)
     {
         if (m_dependencies[i] == _root)
         {
             continue;
         }
         m_dependencies[i]._release(_root);
     }
 }
Ejemplo n.º 8
0
 public UnitySceneLoadTask(Resource_Assetbundle _asset_data, String _scene_name, Action <Boolean> _callback)
 {
     m_resource   = _asset_data;
     m_scene_name = _scene_name;
     m_callback   = _callback;
     if (_asset_data.State == ResourceState.Error)
     {
         throw new Utility.ExceptionEx("");
     }
     m_resource.Retain();
 }
Ejemplo n.º 9
0
        /// <summary>
        /// 同步加载Assetbundle
        /// </summary>
        /// <param name="_name"></param>
        /// <returns></returns>
        public Resource_Assetbundle LoadAssetbundleSync(String _assetbundle_name)
        {
            Resource_Assetbundle res_assetbundle = GetResource(_assetbundle_name) as Resource_Assetbundle;

            if (res_assetbundle == null)
            {
                Log.Error("[ResouceManager]: res [{0}] 不存在!或类型不为Assetbundle", _assetbundle_name);
                return(null);
            }
            res_assetbundle.LoadResourceSync();
            return(res_assetbundle);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 处理retain, 这里把根的对象传进来,避免循环依赖导致的死循环
        /// </summary>
        /// <param name="_root"></param>
        private void _retain(Resource_Assetbundle _root)
        {
            for (int i = 0; i < m_dependencies.Count; i++)
            {
                // 断掉循环依赖
                if (m_dependencies[i] == _root)
                {
                    continue;
                }
                m_dependencies[i]._retain(_root);
            }

            // 如果未加载,就加载
            if (State != ResourceState.Loaded && State != ResourceState.Loading)
            {
                LoadResource();
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 加载完成检测,传入root是为了处理循环依赖
        /// </summary>
        /// <param name="_root"></param>
        /// <returns></returns>
        private bool _checkLoaded(Resource_Assetbundle _root)
        {
            Boolean all_loaded = State == ResourceState.Loaded;

            for (int i = 0; i < m_dependencies.Count; i++)
            {
                if (m_dependencies[i].State == ResourceState.Loaded)
                {
                    continue;
                }

                all_loaded = false;
                if (m_dependencies[i].State != ResourceState.Loading)
                {
                    throw new ExceptionAssetBundleDependenciesStateError(m_dependencies[i].State);
                }
                break;
            }
            return(all_loaded);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 异步加载bundle中的一个资源
        /// </summary>
        /// <param name="_resouce"></param>
        /// <param name="_asset_name"></param>
        /// <param name="_type"></param>
        public void LoadAsset(Resource_Assetbundle _resouce, String _asset_name, Type _type)
        {
            // 等待中或者正在加载的被跳过
            if (m_current_loading != null && m_current_loading.Resource == _resouce && m_current_loading.AssetName == _asset_name)
            {
                return;
            }

            foreach (LoadRequireData load_require_data in m_pending_asset)
            {
                if (load_require_data.Resource == _resouce && _asset_name == load_require_data.AssetName)
                {
                    return;
                }
            }

            m_pending_asset.Enqueue(new LoadRequireData()
            {
                Resource = _resouce, AssetName = _asset_name, Type = _type
            });
        }
Ejemplo n.º 13
0
        public void LoadAssetbundleAsync(String _assetbundle_name, Predicate <Boolean, List <Resource_Assetbundle> > _callback)
        {
            Resource_Assetbundle res_assetbundle = GetResource(_assetbundle_name) as Resource_Assetbundle;

            if (res_assetbundle == null)
            {
                Log.Error("[ResouceManager]: res [{0}] 不存在!或类型不为Assetbundle", _assetbundle_name);
                _callback(false, null);
                return;
            }

#if UNITY_EDITOR
            // 其实没有意义的验证, 除非Resource_assetbundle和内部存储的类型不同
            if (res_assetbundle.ResouceInfo.ResType != ResourceType.AssetBundle)
            {
                _callback(false, null);
                Log.Error("[ResouceManager]: 请求加载的Assetbundle: {0} 非Assetbundle资源! 类型为: {1}", _assetbundle_name, res_assetbundle.ResouceInfo.ResType);
                return;
            }
#endif
            var task = new UnityAssetBundleLoadTask(res_assetbundle, _callback);
            m_running_task.AddLast(task);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 初始化资源管理器
        /// </summary>
        /// <param name="_updateByAppConfig">是否使用app内config更新列表,在更新完成之后重新初始化时不需要更新</param>
        /// <returns></returns>
        public Boolean Initialize(Boolean _updateByAppConfig)
        {
            String appResConfigPath        = String.Concat(Application.streamingAssetsPath, "/res/res_config.grt");
            var    assetbundleAppResConfig = AssetBundle.LoadFromFile(appResConfigPath);

            if (assetbundleAppResConfig == null)
            {
                Log.Critical("[ResourceManager.Initialize]:读取inapp res config 失败! assetbundle 加载失败!");
                return(false);
            }

            var appResConfigBytes = assetbundleAppResConfig.LoadAsset <TextAsset>("res_config");

            if (appResConfigBytes == null)
            {
                Log.Critical("[ResourceManager.Initialize]:读取inapp res config 失败! TextAsset加载失败");
                assetbundleAppResConfig.Unload(true);
                return(false);
            }
            var appResConfig = ResConfigFile.Create(appResConfigBytes.bytes);

            if (appResConfig == null)
            {
                Log.Critical(eLogType.Resources, "[ResourceManager.Initialize]: 读取inapp res config 失败! 解析失败!");
                assetbundleAppResConfig.Unload(true);
                return(false);
            }
            assetbundleAppResConfig.Unload(true);


            mLoadResConfig = ResConfigFile.Create(ResUtility.LocalResConfigPath, true);
            if (mLoadResConfig == null)
            {
                Log.Error(eLogType.Resources, "[ResouceManager]: Local res config file 创建失败!");
                return(false);
            }

            // 首次初始化时执行
            if (_updateByAppConfig)
            {
                mLoadResConfig.UpdateByAppResConfig(appResConfig);
            }


            mLoadResConfig.TraverseAssets(_info => {
                m_res_dictionary.Add(_info.Name, _createResourceData(_info));
            });

            // 读取依赖
            var assetbundleManifest      = AssetBundle.LoadFromFile(GetResource("res.grt").ResouceInfo.SystemApiLoadingPath);
            AssetBundleManifest manifest = assetbundleManifest.LoadAsset <AssetBundleManifest>("AssetBundleManifest");

            foreach (Resource_Base res in m_res_dictionary.Values)
            {
                if (res.ResouceInfo.ResType != ResourceType.AssetBundle)
                {
                    continue;
                }
                Resource_Assetbundle resAssetbundle = res as Resource_Assetbundle;
                var dependenciesName = manifest.GetDirectDependencies(res.Name);
                for (int i = 0; i < dependenciesName.Length; i++)
                {
                    Resource_Assetbundle dependency = GetResource <Resource_Assetbundle>(dependenciesName[i]);
                    if (dependency == null)
                    {
                        Log.Critical(eLogType.Resources, "[ResourceManager.Initialize]: {0}的依赖资源: {1}没有在ResourceConfig中找到! 请尝试重新打包!", res.Name, dependenciesName[i]);
                        return(false);
                    }
                    resAssetbundle.AddDependencies(dependency);
                }
            }
            assetbundleManifest.Unload(true);

            Log.Debug(eLogType.Resources, "ResourceManager Initialized Done!");
            return(true);
        }
Ejemplo n.º 15
0
 /// <summary>
 /// 单个加载文件任务
 /// </summary>
 /// <param name="_asset"></param>
 /// <param name="_on_complete_action">全部加载完成的回调, arg1: 是否成功加载全部, arg2: 所加载的AssetRuntimeData列表, 无论加载成功或者失败, 返回值: 是否保持Assetbundle引用</param>
 public UnityAssetBundleLoadTask(Resource_Assetbundle _asset, Predicate <Boolean, List <Resource_Assetbundle> > _on_complete_action)
 {
     m_asset_list.Add(_asset);
     m_on_completed_action = _on_complete_action;
     _requireLoad();
 }