美文网首页
Addresble读取

Addresble读取

作者: SeanLink | 来源:发表于2023-10-23 13:58 被阅读0次
        async void readCatalogFromPathAsync(string path,Action action = null)
        {
            try
            {
                var catalogHandle = Addressables.LoadContentCatalogAsync(path, false);
                await catalogHandle.Task;
                var catalog = catalogHandle.Result;
                var keys = catalog.Keys;
                var locationsHandle = Addressables.LoadResourceLocationsAsync(keys, Addressables.MergeMode.Union);
                await locationsHandle.Task;
                IList<IResourceLocation> locations = locationsHandle.Result;
                refreshFastLocations(locations);
                Addressables.Release(catalogHandle);
                Addressables.Release(locationsHandle);
            }
            catch (Exception ex)
            {
                Log.I(TAG, "readCatalogFromPathAsync path :{0}  , message:{1}",path,ex.Message);
    
            }
    
        }
    
        void refreshFastLocations(IList<IResourceLocation> locations)
        {
            foreach (var item in locations)
            {
                if (item.ResourceType == typeof(GameObject) || item.ResourceType == typeof(SceneInstance) || typeof(DefaultSetting).IsAssignableFrom(item.ResourceType))
                {
                    fastLocations[item.PrimaryKey] = item;
                    Log.I(TAG, "item.PrimaryKey: {0}  item.InternalId:{1}", item.PrimaryKey, item.InternalId);
                }
            }
        }
    
    
        public bool LoadGameObjectAssetSync(string idorname, out AsyncOperationHandle<GameObject> handle)
        {
            if (fastLocations.ContainsKey(idorname))
            {
                IResourceLocation loc = fastLocations[idorname];
                handle = Addressables.LoadAssetAsync<GameObject>(loc);
            }
            else
            {
                handle = Addressables.LoadAssetAsync<GameObject>(idorname);
            }
    
            handle.WaitForCompletion();
    
            if (handle.Status == AsyncOperationStatus.Succeeded)
            {
                return true;
            }
            else
            {
                Addressables.Release(handle);
                return false;
            }
        }
    
        public async Task<AsyncOperationHandle<TObject>> LoadAssetAsyncFromString<TObject>(string key)
        {
            if (fastLocations.ContainsKey(key))
            {
                IResourceLocation loc = fastLocations[key];
                AsyncOperationHandle<TObject> handle =
                    Addressables.LoadAssetAsync<TObject>(loc);
                await handle.Task;
                return handle;
            }
            else
            {
                Log.I(TAG, "LoadAssetAsyncFromString Not Find Key:{0} Error", key);
                AsyncOperationHandle<TObject> handle = Addressables.LoadAssetAsync<TObject>(key);
                await handle.Task;
                return handle;
            }
        }
    
        public  AsyncOperationHandle<TObject> LoadAssetAsync<TObject>(string key)
        {
            if (fastLocations.ContainsKey(key))
            {
                return Addressables.LoadAssetAsync<TObject>(fastLocations[key]);
            }
            else
            {
                Log.I(TAG, "LoadAssetAsync Not Find Key:{0} Error", key);
                return Addressables.LoadAssetAsync<TObject>(key);
            }
        }
    
        public void upDateAddressbleInfo()
        {
            fastLocations.Clear();
            catalogPath = getCatalogPath();
            Log.I(TAG, "catalogs  catalogs::::::" + catalogPath);
    
            Addressables.ClearResourceLocators();
            var catalogHandle = Addressables.LoadContentCatalogAsync(catalogPath, false);
            catalogHandle.WaitForCompletion();
            var catalog = catalogHandle.Result;
            var keys = catalog.Keys;
            var locationsHandle = Addressables.LoadResourceLocationsAsync(keys, Addressables.MergeMode.Union);
            locationsHandle.WaitForCompletion();
            IList<IResourceLocation> locations = locationsHandle.Result;
            refreshFastLocations(locations);
            Addressables.Release(catalogHandle);
            Addressables.Release(locationsHandle);
        }
    
    
        private string getCatalogPath()
        {
    
            string realPath = catalogPath;
    #if UNITY_EDITOR
            string parentPath = System.IO.Directory.GetParent(Application.dataPath).FullName;
            string editorPath = parentPath + config.catalogEditorAliOSPath;
            string localPath = parentPath + config.catalogEditorLocalPath;
            if (File.Exists(editorPath))
            {
                DateTime lastModifiedA = File.GetLastWriteTime(editorPath);
                DateTime lastModifiedB = File.GetLastWriteTime(localPath);
                Log.I(TAG, "lastModifiedA: {0} === lastModifiedB: {1}  ", lastModifiedA, lastModifiedB);
                string jsonPath = lastModifiedA > lastModifiedB ? editorPath : localPath;
                realPath = localPath;
                Log.I(TAG, "realPath: {0} is Have ", realPath);
            }
            else
            {
                realPath = localPath;
                Log.I(TAG, "realPath: {0} is Exist ", realPath);
            }
    #else
            if (File.Exists(config.catalogRemotePath))
            {
                DateTime lastModifiedA = File.GetLastWriteTime(config.catalogRemotePath);
                DateTime lastModifiedB = File.GetLastWriteTime(config.catalogLocalPath);
                Log.I(TAG, "lastModifiedA: {0} === lastModifiedB: {1}  ", lastModifiedA, lastModifiedB);
                string jsonPath = lastModifiedA > lastModifiedB ? config.catalogRemotePath : config.catalogLocalPath;
                realPath = jsonPath;
                Log.I(TAG, "realPath: {0} is Have ", realPath);
            }
            else
            {
                realPath = config.catalogLocalPath;
                Log.I(TAG, "realPath: {0} is Exist ", realPath);
            }
    #endif
            return realPath;
        }
    
    

    相关文章

      网友评论

          本文标题:Addresble读取

          本文链接:https://www.haomeiwen.com/subject/lxfqvdtx.html