U3D Client Framework (Resource Management) Writable Area Resource Manager

1. The role of writable area resource manager

As the name implies, the writable area resource manager is the encapsulation of the read-write area file access API, the encapsulation of the API for checking the writable area resources, the resource version record function, and the record of writable area resource information.

API for obtaining writable area in Unity

The API to get the writable area path in Unity: Application.persistentDataPath

Writable area directory in Android phone

The writable area directory on an Android phone is generally: under the /storage/emulated/0/Android/data/package name/files/ directory

2. Code implementation

The detailed explanation will only select some key codes to talk about. For the rest of the functions, just look at the entire code.

Load writable area resource pack information

When the game starts, all assetbundle information in the writable area will be cached into the dictionary. In order to facilitate hot update, file information can be obtained when loading resources to avoid frequent file IO

        //加载可写区资源包信息
        public Dictionary<string, AssetBundleInfoEntity> GetAssetBundleVersionList(ref string version)
        {
    
    
            //拿到资源版本号
            version = PlayerPrefs.GetString(ConstDefine.ResourceVersion);
            //把json文件反序列化成文件信息map
            string json = IOUtil.GetFileText(LocalVersionFilePath);
            return JsonMapper.ToObject<Dictionary<string, AssetBundleInfoEntity>>(json);
        }

Get file byte array & check file existence

        public bool CheckFileExists(string path)
        {
    
    
            return File.Exists(string.Format("{0}/{1}",Application.persistentDataPath,path));
        }
        
        //获取本地文件字节数组
        public byte[] GetFileBuffer(string path)
        {
    
    
            return IOUtil.GetFileBuffer(string.Format("{0}/{1}", Application.persistentDataPath, path));
        }

LocalAssetsManager.cs complete code

  //可写区资源管理器
    //LocalAssets资源管理器
    public class LocalAssetsManager
    {
    
    
        //本地版本文件路径    
        public string LocalVersionFilePath
        {
    
    
            get 
            {
    
    
                return string.Format("{0}/{1}",Application.persistentDataPath,ConstDefine.VersionFileName);
            }
        }

        //获取可写区版本文件是否存在
        public bool GetVersionFileExists()
        {
    
    
            return File.Exists(LocalVersionFilePath);
        }

        public bool CheckFileExists(string path)
        {
    
    
            return File.Exists(string.Format("{0}/{1}",Application.persistentDataPath,path));
        }
        
        //获取本地文件字节数组
        public byte[] GetFileBuffer(string path)
        {
    
    
            return IOUtil.GetFileBuffer(string.Format("{0}/{1}", Application.persistentDataPath, path));
        }

        //保存资源版本号(资源版本,只是单纯的资源版本,0.0.1)
        //资源版本号记在了本地配置里,版本文件记在了VersionFile.bytes 版本文件里
        public void SetResourceVersion(string version)
        {
    
    
            PlayerPrefs.SetString(ConstDefine.ResourceVersion,version);
        }
        
        //保存资源版本文件(filelist,里面记录了所有ab包的文件信息。把文件信息都写入到本地去 VersionFile.bytes)
        public void SaveVersionFile(Dictionary<string,AssetBundleInfoEntity>dic)
        {
    
    
            //把map结构映射成json格式的string
            string json = JsonMapper.ToJson(dic);
            IOUtil.CreateTextFile(LocalVersionFilePath,json);
        }
        
        //加载可写区资源包信息
        public Dictionary<string, AssetBundleInfoEntity> GetAssetBundleVersionList(ref string version)
        {
    
    
            //拿到资源版本号
            version = PlayerPrefs.GetString(ConstDefine.ResourceVersion);
            //把json文件反序列化成文件信息map
            string json = IOUtil.GetFileText(LocalVersionFilePath);
            return JsonMapper.ToObject<Dictionary<string, AssetBundleInfoEntity>>(json);
        }
    }

Guess you like

Origin blog.csdn.net/qq_33531923/article/details/128489925