【发布时间】:2018-09-30 17:00:47
【问题描述】:
我现在遇到了一个奇怪的问题。我正在尝试从 StreamingAssets 加载本地化 json 文件。我正在使用 Path.Combine 将 StreamingAssetsPath 和 Path 组合在一起以形成完整路径。 Path 的值为 en-us。但由于某种原因,Path.Combine 在连接字符串时会抛出一个 \ ,所以我的路径无效。如果我将 Path 的值更改为 /en-US,那么它会完全切断 Application.StreamingAssetsPath 部分。
Debug.Log 结果 Path = en-US:
C:/Users/bluem/Documents/Fishtale/Assets/StreamingAssets\zh-CN
Debug.Log 结果的 Path = /en-US
/zh-CN
我只是无法对这种怪异的事情做出头脑或故事,哈哈。
public void LoadLocalizedText()
{
localizedText = new Dictionary<string, string>();
string filePath = Path.Combine(Application.streamingAssetsPath, path);
Debug.Log(filePath);
if (File.Exists(filePath))
{
string dataAsJson = File.ReadAllText(filePath);
LocalizationData loadedData = JsonUtility.FromJson<LocalizationData>(dataAsJson);
for (int i = 0; i < loadedData.items.Length; i++)
{
localizedText.Add(loadedData.items[i].key, loadedData.items[i].value);
}
Debug.Log("Localization Manager: Data loaded, dictionary contains: " + localizedText.Count + " entries.");
}
else
{
Debug.LogError("Localization Manager: Cannot find data file name: " + filePath);
return;
}
isReady = true;
}
【问题讨论】:
-
这是那些讨厌的 C# 怪癖之一 :),你可以在这里阅读:stackoverflow.com/a/1141114/3073551,问题是第二个参数是根的,还有新的
Path.Join方法没有这种行为,你可以在这里阅读:stackoverflow.com/a/52486005/3073551 -
第一个结果除了正反斜杠混合有什么问题吗? Windows 路径可以使用其中之一,也可以使用它们的组合。将其放入 \ 因为这是默认的目录分隔符(/,就像您的 streamingAssetsPath 使用的一样,是 AltDirectorySeperator,坦率地说,作为一个 Windows 人,我看起来很奇怪)