Unity 使用WWW读取本地文件出现 Cloudn't read a file

之前一直在使用 Unity4 的版本,之后因为编写优化需要 写出了导致Unity Crash的代码。

struct IMStruct{
}

public class Crash : MonoBehaviour{
    Dictionary<string, IMStruct> map; //如 字典中 Key or Value 类型为struct 即可导致 Unity Crash [Version 4.6.4]
}

所以升级到Unity5[Version 5.6.4]
发现新版本上 MonoBehaviour 取消了很多直接读取 Component 的属性,顺带升级项目时 新版本会自动转换为GetComponent<xxx>.如

// in unity 4
var m = renderer.material;
// in unity 5
var m = GetComponent<Renderer>().material;

在使用过程中 也发现之前在 Unity4上使用 WWW 读取本地文件一直都成功的情况 随着升级到Unity5版本后会报错:Cloudn't read a file.
后来在查阅资料后发现 新版本对URL的要求更严格了,例如在Unity4上执行如下语句可以成功读取文件

var www = new WWW(@"file://c:hello/world/im.txt");

在 Unity5中必须写成

var www = new WWW(@"file://c:helloworldim.txt");

否则会出现如标题的错误。

原文地址:https://www.cnblogs.com/godzza/p/10992397.html