Пример #1
0
 void LoadFromResources()
 {
     if (info != null)
     {
         string resPath = AssetPaths.AddAssetPrev(info.fullName);
         if (type == null)
         {
             m_Request      = new SyncLoaderRequest();
             m_Request.data = AssetDatabase.LoadMainAssetAtPath(resPath);
         }
         else
         {
             m_Request      = new SyncLoaderRequest();
             m_Request.data = AssetDatabase.LoadAssetAtPath(resPath, type);
         }
         if (m_Request.data != null)
         {
             Complete();
         }
         else
         {
             Error();
         }
     }
     else
     {
         Error();
     }
 }
Пример #2
0
        void LoadFromAssetBundle()
        {
            if (info != null)
            {
#if SUPPORT_ASSET_ALIAS
                string assetName = info.aliasName;
#else
                string assetName = AssetPaths.AddAssetPrev(info.fullName);
#endif
                if (type == null)
                {
                    asset = assetBundleReference.assetBundle.LoadAsset(assetName);
                }
                else
                {
                    asset = assetBundleReference.assetBundle.LoadAsset(assetName, type);
                }
                Complete();
            }
            else
            {
                Error();
                Debug.LogError("[AssetManage]Load Asset with no info");
            }
        }
Пример #3
0
        public virtual void Init()
        {
#if ASSETMANAGE_BUNDLE_CACHE_ON
            m_CacheManager = new CacheManager();
            m_CacheManager.cacheInfoFile = AssetPaths.ToBundlePath(cacheManager.cacheInfoFile);
            m_CacheManager.LoadCacheInfo();
#endif
        }
Пример #4
0
        void LoadBundle()
        {
            string assetPath = AssetPaths.GetFullPath(info.fullName);

#if ASSETMANAGER_LOG_ON
            Debug.Log("[AssetManage]LoadBundle " + assetPath + "," + Time.frameCount);
#endif
            LoadFromFileSync(assetPath);
        }
Пример #5
0
        public AssetReference LoadAssetSync(string path, int tag, Type type)
        {
            if (!string.IsNullOrEmpty(path))
            {
                path = AssetPaths.AddAssetPrev(path);
            }

            AssetReference ar = null;

            AssetLoader loader = null;

            if (m_Assets.ContainsKey(path))
            {
#if ASSETMANAGER_LOG_ON
                Debug.Log("[AssetManage]LoadAssetSync asset is loaded " + path + "," + Time.frameCount);
#endif
                ar = m_Assets[path];

                //refresh
                ar.AddTag(tag);

                //cache asset
                ar.Cache();
            }
            else
            {
                if (m_LoadingAssetLoaders.ContainsKey(path))
                {
#if ASSETMANAGER_LOG_ON
                    Debug.Log("[AssetManage]LoadAssetSync async load staring " + path + "," + Time.frameCount);
#endif
                    //TODO Stop async loader
                    return(null);
                }
                else
                {
#if ASSETMANAGER_LOG_ON
                    Debug.Log("[AssetManage]LoadAssetSync create new loader " + path + "," + Time.frameCount);
#endif
                    loader = m_LoaderManager.CreateAssetSyncLoader(path);
                }

                loader.AddParamTag(tag);

                if (type != null)
                {
                    loader.type = type;
                }
                loader.state = Loader.State.Inited;
                loader.Start();
                ar = loader.result;
                OnAssetBeforeLoaded(loader);
                OnAssetAfterLoaded(loader);
            }

            return(ar);
        }
Пример #6
0
 void LoadFromResources()
 {
     if (info != null)
     {
         string resourcePath = Path.Combine(Path.GetDirectoryName(info.fullName), Path.GetFileNameWithoutExtension(info.fullName));
         resourcePath          = AssetPaths.RemoveAssetPrev(resourcePath);
         m_Request             = RequestManager.CreateResouceLoaderRequest(resourcePath, type);
         m_Request.onComplete += OnRequestComplete;
         assetManager.requestManager.ActiveRequest(m_Request);
     }
     else
     {
         Error();
         Debug.LogError("[AssetManage]Load Asset with no info");
     }
 }
Пример #7
0
        public void Init(string allManifestFile = null, Action <bool> callback = null)
        {
            if (m_Inited)
            {
                if (m_InfoManager.inited)
                {
                    if (callback != null)
                    {
                        callback(true);
                    }
                }
                else
                {
                    m_InfoManager.onInitComplete += callback;
                }
                return;
            }
            m_Inited = true;

            //asset search path
            AssetPaths.SetupDefaultSearchPaths();

            //system events
            SetupSystemEvents();

            //crate loader manager
            m_LoaderManager = new LoaderManager(this);

            //create rquest manager
            m_RequestManager = new RequestManager(this);
            m_RequestManager.Init();

            //create info manager
            m_InfoManager = new InfoManager(this);
            m_InfoManager.Init();
            if (callback != null)
            {
                m_InfoManager.onInitComplete += callback;
            }

            if (string.IsNullOrEmpty(allManifestFile))
            {
                allManifestFile = AssetPaths.bundleManifestFile;
            }
            m_InfoManager.Load(AssetPaths.GetFullPath(allManifestFile));
        }
Пример #8
0
        IEnumerator LoadPackageFile(string fileUrl)
        {
            using (UnityWebRequest webRequest = UnityWebRequest.Get(fileUrl))
            {
                webRequest.timeout = AMSetting.DownloadTimeout;
                yield return webRequest.SendWebRequest();

                if (webRequest.isNetworkError && m_RetryTimes-- > 0)
                {
                        webRequest.Dispose();
                        LoadFromPackage(fileUrl);
                }
                else
                {
                    bool success = true;
                    if (string.IsNullOrEmpty(webRequest.error))
                    {
                        //save to local file
                        string bundleDir = AssetPaths.GetBundlePath();
                        if (!Directory.Exists(bundleDir))
                        {
                            Directory.CreateDirectory(bundleDir);
                        }
                        string localInfoFilePath = AssetPaths.Combine(bundleDir, AssetPaths.bundleManifestFile);
#if ASSETMANAGER_LOG_ON
                        Debug.LogFormat("[AssetManage]Save all.manifest to {0} ", localInfoFilePath);
#endif
                        File.WriteAllBytes(localInfoFilePath,webRequest.downloadHandler.data);

                        using (MemoryStream stream = new MemoryStream(webRequest.downloadHandler.data))
                        {
                            LoadFromStream(stream);
                        }
                    }
                    else
                    {
                        Debug.LogErrorFormat("[AssetManage]LoadPackageFile:{0} error: {1} ", fileUrl, webRequest.error);
                        success = false;
                    }

                    InitComplete(success);
                }
            }
        }
Пример #9
0
        public virtual Request CreateAssetBundleRequest(AssetBundleInfo assetBundleInfo)
        {
            if (assetBundleInfo == null)
            {
                return(null);
            }

            if (m_CacheManager != null)
            {
                //use cache
                if (m_CacheManager.IsCached(assetBundleInfo.fullName, assetBundleInfo.hash))
                {
                    //load from cache
                    string assetPath = AssetPaths.GetFullPath(assetBundleInfo.fullName);
                    return(CreateBundleCreateRequest(assetPath));
                }
                else
                {
                    //download and save to cache
                    string url      = AssetPaths.GetUrl(assetBundleInfo.fullName);
                    string savePath = AssetPaths.ToBundlePath(assetBundleInfo.fullName);
                    BundleWebSaveRequest webSaveRequest = CreateBundleWebSaveRequest(url, savePath, assetBundleInfo.hash, assetBundleInfo.fullName);
                    webSaveRequest.onSaveComplete += OnBundleWebRequestSaveComplete;
                    return(webSaveRequest);
                }
            }
            else
            {
                //no cache
                string assetPath = AssetPaths.GetFullPath(assetBundleInfo.fullName);
#if ASSETMANAGER_LOG_ON
                Debug.LogFormat("[AssetManage]LoadBundle {0}---{1}", assetPath, Time.frameCount);
#endif
                if (assetPath.Contains("://"))
                {
                    return(CreateBundleWebRequest(assetPath));
                }
                else
                {
                    return(CreateBundleCreateRequest(assetPath));
                }
            }
        }
Пример #10
0
        public AssetInfo FindAssetInfo(string key)
        {
            if (m_AssetInfos != null && !string.IsNullOrEmpty(key))
            {
                if (m_AssetInfos.ContainsKey(key))
                {
                    return m_AssetInfos[key];
                }

                string fixKey = AssetPaths.AddAssetPrev(key);
                if (!fixKey.Equals(key))
                {
                    if (m_AssetInfos.ContainsKey(fixKey))
                    {
                        return m_AssetInfos[fixKey];
                    }
                }
            }
            return null;
        }
Пример #11
0
 void LoadFromResources()
 {
     if (info != null)
     {
         string resourcePath = Path.Combine(Path.GetDirectoryName(info.fullName), Path.GetFileNameWithoutExtension(info.fullName));
         resourcePath = AssetPaths.RemoveAssetPrev(resourcePath);
         if (type == null)
         {
             asset = Resources.Load(resourcePath);
         }
         else
         {
             asset = Resources.Load(resourcePath, type);
         }
         Complete();
     }
     else
     {
         Debug.LogError("[AssetManage]Load Asset with no info");
         Error();
     }
 }
Пример #12
0
        void LoadFromAssetBundle()
        {
            if (info != null)
            {
#if SUPPORT_ASSET_ALIAS
                string assetName = info.aliasName;
#else
                string assetName = AssetPaths.AddAssetPrev(info.fullName);
#endif
#if ASSETMANAGER_LOG_ON
                Debug.LogFormat("[AssetManage]Load asset {0}", assetName);
#endif

                m_Request             = RequestManager.CreateAssetLoaderRequest(assetBundleReference.assetBundle, assetName, type);
                m_Request.onComplete += OnRequestComplete;
                assetManager.requestManager.ActiveRequest(m_Request);
            }
            else
            {
                Debug.LogError("[AssetManage]Load Asset with no info");
                Error();
            }
        }
Пример #13
0
        /// <summary>
        /// 资源加载
        /// 资源加载完成,返回一个关于资源的refrence,记录资源的使用情况。
        /// 资源使用的三种方式:
        ///     1.Retain(),使用完成时需要执行Release()。
        ///     2.Retain(Object),使用完成时可以不用执行Release(Object),等待UnloadUnuseds清理。
        ///     3.Monitor(GameObject),当GameObject被删除时,会自动执行Release(Object)。
        /// 对于手动删除资源最好执行RemoveAsset。
        /// 同一个资源只有一个正在加载的loader。由Manager负责管理Loader。
        /// </summary>
        /// <param name="path"></param>
        /// <param name="tag"></param>
        /// <param name="type"></param>
        /// <param name="completeHandle"></param>
        /// <returns></returns>
        public AssetLoader LoadAsset(string path, int tag, Type type, Action <AssetReference> completeHandle = null, bool autoReleaseBundle = true, bool autoStart = true)
        {
            AssetLoader loader = null;

            if (!string.IsNullOrEmpty(path))
            {
                path = AssetPaths.AddAssetPrev(path);
            }
            else
            {
                if (completeHandle != null)
                {
                    completeHandle(null);
                }
                return(loader);
            }


            if (m_Assets.ContainsKey(path))
            {
#if ASSETMANAGER_LOG_ON
                Debug.Log("[AssetManage]LoadAsset asset is loaded " + path + "," + Time.frameCount);
#endif
                AssetReference ar = m_Assets[path];

                //refresh
                ar.AddTag(tag);

                //cache asset
                ar.Cache();

                loader           = m_LoaderManager.CreateAssetAsyncLoader(path);
                loader.forceDone = true;
                loader.result    = ar;

                if (completeHandle != null)
                {
                    loader.onComplete += completeHandle;
                }

                loader.onAfterComplete += OnAssetAfterLoaded;
                loader.state            = Loader.State.Completed;
                if (autoStart)
                {
                    m_LoaderManager.ActiveLoader(loader);
                }
            }
            else
            {
                if (m_LoadingAssetLoaders.ContainsKey(path))
                {
#if ASSETMANAGER_LOG_ON
                    Debug.Log("[AssetManage]LoadAsset using loading loader " + path + "," + Time.frameCount);
#endif
                    loader = m_LoadingAssetLoaders[path];
                }
                else
                {
#if ASSETMANAGER_LOG_ON
                    Debug.Log("[AssetManage]LoadAsset create new loader " + path + "," + Time.frameCount);
#endif
                    loader = m_LoaderManager.CreateAssetAsyncLoader(path);
                    m_LoadingAssetLoaders[path] = loader;
                }

                loader.AddParamTag(tag);

                if (type != null)
                {
                    loader.type = type;
                }

                if (!autoReleaseBundle)
                {
                    loader.autoReleaseBundle = autoReleaseBundle;
                }

                if (completeHandle != null)
                {
                    loader.onComplete += completeHandle;
                }

                loader.IncreaseLoadingRequest();

                //only once init
                if (loader.state == Loader.State.Idle)
                {
                    loader.onBeforeComplete += OnAssetBeforeLoaded;
                    loader.onAfterComplete  += OnAssetAfterLoaded;
                    loader.state             = Loader.State.Inited;
                    if (autoStart)
                    {
                        m_LoaderManager.ActiveLoader(loader);
                    }
                }
            }

            return(loader);
        }