回调永远不会在Unity Web Player中执行

时间:2014-01-04 03:59:44

标签: c# callback unity3d

因此,我试图发布到我的网络API并在帖子结束时使用回调。这一切都在Unity编辑器和桌面应用程序中完美运行,但在Web Player中不起作用。我把它缩小到只是没有被调用的回调。我怎样才能使用回调?这是我的代码:

void ui_login() { 
if (uiBase == null) 
     return; 

Debug.LogError(uiBase); clicks++; status = "(" + clicks + ")" + "working";

var username = uiBase.UIElements.FirstOrDefault(e => e.Name == "txt_username");
var password = uiBase.UIElements.FirstOrDefault(e => e.Name == "txt_password");
try
{
    var request = new LoginRequest()
    {
        Email = username.Text,
        Password = password.Text
    };
    StartCoroutine(WaitForRequest<LoginResponse>(request, loginCallback));
}
catch (Exception e)
{
    status = "(" + clicks + ")" + e;
}
}

void loginCallback(LoginResponse response, WWW www)
{
if (www.error != null)
    status = www.error;
if (response != null)
    status = response.ErrorMessage;
}

IEnumerator WaitForRequest<TResponse>(
LoginRequest request, Action<TResponse, WWW> callback)
{
var json = JsonMapper.ToJson(request);
var www = new WWW("http://someurl.com", json.ToBytes());
yield return www;
TResponse response;
if (www.error == null && www.isDone)
{
    var str = Encoding.UTF8.GetString(www.bytes);
    print(str);
    status = str;
    response = JsonConvert.DeserializeObject<TResponse>(str);
}

else
    response = default(TResponse);

print("somthif");
callback(response, www);
}

1 个答案:

答案 0 :(得分:0)

出于安全原因,Unity Web Player可在Sandbox中运行,并将阻止您的HTTP请求。

要解决此问题,请按照此链接中描述的过程进行操作:http://docs.unity3d.com/Documentation/Manual/SecuritySandbox.html