behaviac icon indicating copy to clipboard operation
behaviac copied to clipboard

unity下 behaviac 如果xml要做热更新该怎么做

Open yangqingming opened this issue 7 years ago • 2 comments

unity下 behaviac 得xml配置 可以不放在Resources目录下吗 我看了下源码发现没有自定义加载啊 behaviac底层FileManager直接采用的Resources.Load加载的

yangqingming avatar Mar 28 '18 07:03 yangqingming

我放在了预设下。实现通过预设获取xml,实现了xml的热更。

MrTigerZhang avatar Apr 11 '18 06:04 MrTigerZhang

我们是一般打包时 仍旧放在Resources下, 思路就是:重载 behaviac.FileManager、先去找热更目录有没有这个xml, 有就加载外部文件,没有就直接按原来方式去Resources目录加载。

根据 http://www.behaviac.com/tutorial_10_extent_filemanager/ 重写了一个MyBehaviacFileManager

public class MyBehaviacFileManager : behaviac.FileManager
{
    public override byte[] FileOpen(string filePath, string ext)
    {
        bool in_out = false;    //这个有点鸡肋

        //思路:检测热更目录下是否有对应文件,存在则优先读取, 否则仍旧读取Resources
        int k0 = filePath.IndexOf("Resources");
        if (k0 != -1)
        {
            //根据传过来的路径,计算出我们在热更目录下的相对路径。
            string filePathInResources = filePath.Substring(k0);
            string fileOutPath = filePathInResources + ext;
            fileOutPath = fileOutPath.ToLower();
            if (FileHelper.CheckResInPersistentData(fileOutPath))   //封装的 检查安卓、ios热更外部目录的函数
            {
                try
                {
                    string file_name = FileHelper.GetResInPersistentDataPath(fileOutPath);   //封装的检查安卓、ios热更外部目录的文件全路径函数
                    byte[] buffer = CUtility.GetBinaryFileBuffer(file_name);                //文件读取成字节数组(安卓有些不一样,所以统一封装了一个接口)
                    in_out = true;
                    return buffer;
                }
                catch (System.Exception e)
                {
                    Log.Error("LoadJsonFile ({0}) failed, error({1})", fileOutPath, e.ToString());
                }

            }
        }
        if (!in_out)
        {
            return base.FileOpen(filePath, ext);    //外部xml等文件不存在,就走默认基类的加载
        }
        else
        {
            return null;
        }
    }
}

zhenmu avatar Apr 28 '18 10:04 zhenmu