原因
游戏工程使用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;
}
}