我试图将我的Windows Phone游戏移植到Unity,但我所做的异步服务器调用出现了一些问题:
public static async Task<T> Get<T>(String path)
{
HttpClient client = CreateHttpClient();
var clientResponse = await client.GetAsync(Config.SERVER_URL + path);
return JsonConvert.DeserializeObject<T>(await clientResponse.Content.ReadAsStringAsync());
}
我在Unity脚本中有以下内容,但这有点疯狂:
public static IEnumerable<T> GetAnonymous<T>(string path)
{
WWW www = new WWW(SERVER_URL + path);
yield return (T)JsonConvert.DeserializeObject(www.text);
}
它指出,当我试图访问对象的属性时,Object ref没有设置为对象的实例
您需要等待WWW类来完成请求。
public static IEnumerable<T> GetAnonymous<T>(string path)
{
WWW www = new WWW(SERVER_URL + path);
yield return www;
return (T)JsonConvert.DeserializeObject(www.text)
}
你可以在这里阅读更多关于WWW类的内容。