Unity 识别文件名大小写不同

原因

游戏工程使用GIT进行版本控制,GIT的一个特征是不对文件名大小写敏感,需要手动去对单个文件进行重名操作,若是有人不是通过重名,就无法上传。在Unity Windows 编辑器读取正常,在安卓下就有可能读取不到文件。

解决

若是在Windows 下也能识别到文件的真正名称就没有这个问题。增加个可以识别文件真正名称的接口,然后在读取文件的时候,判断文件名跟传入的是否大小写相同,不相同就报错。

    /// <summary>
    /// 获取文件的真实文件名,大小写真实
    /// </summary>
    /// <param name="pathName"></param>
    /// <returns></returns>
    public static string GetExactPathName(string pathName)
    {
        if (!(File.Exists(pathName) || Directory.Exists(pathName)))
            return pathName;
 
        var di = new DirectoryInfo(pathName);
 
        if (di.Parent != null)
        {
            return Path.Combine(
                GetExactPathName(di.Parent.FullName),
                di.Parent.GetFileSystemInfos(di.Name)[0].Name);
        }
        else {
            return di.Name.ToUpper();
        }
    }

判断示例

    public static byte[] LoadPbData(string pbPath)
    {
        if (!File.Exists(pbPath))
        {
            LogModule.Error(LogScenario.Default, "不存在文件{0}", pbPath);
            return null;
        }
#if UNITY_EDITOR
        var realName = Path.GetFileName(FileUtils.GetExactPathName(pbPath));
        if (realName != Path.GetFileName(pbPath))
        {
            Debug.LogErrorFormat("文件大小写不匹配 实际名 {0} 读取名 {1} ", realName, Path.GetFileName(pbPath));
        }
#endif
        using (FileStream fs = new FileStream(pbPath, FileMode.Open, FileAccess.Read))
        using (BinaryReader sr = new BinaryReader(fs))
        {
            int len = (int) fs.Length;
            var v = sr.ReadBytes(len);
            sr.Close();
            fs.Close();
            return v;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值