- Home /
WWW Request runs in Editor but not in Webplayer
As the title says, I'm sending an WWW request to my webserver that has a crossdomain policy and everything working correctly. As also mentioned, everything works perfectly fine without and errors or anything in the Editor.
But when trying to run the same exact game in a Webplayer build, it simply just freezes, not even my Try-Catch fires.
This is the part, where I try to fire the WWW Request code:
if(GUILayout.Button("Create Channel", GUI.skin.button)) {
try {
int channelID = int.Parse(DataHandler.self.CreateChannelID(ChannelName));
if(channelID != 0) {
Message = "Channel Created!";
} else {
Message = "Channel Name already exist!";
}
} catch(UnityException err) {
Message = err.Message;
}
}
And this is the part, where the 'behind the scenes happen, A.K.A the WWW Request. Note that I have edited the urlRequest for this question, for security purposes of mine, It's tested in browser and works there aswell.
public string CreateChannelID(string ChannelName) {
string urlRequest = "http://mywebsite.com/game/AddChannel.aspx?ChannelName=" + ChannelName;
WWW request = new WWW(urlRequest);
StartCoroutine(YieldRequest(request));
while(!request.isDone) {}
return stripString(request.text);
}
private IEnumerator YieldRequest(WWW request) {
yield return request;
}
Answer by gfoot · Apr 22, 2014 at 07:41 PM
Line 7 does a busy-wait for the request to complete. Depending on the WWW implementation (which varies between platforms), the request may or may not be able to complete in the background - on some platforms it requires your code to return control to Unity. So you need to avoid busy loops like that - they are really bad practice anyway.
The easiest way to resolve this is probably to move all of the response to the button getting pressed into a coroutine. Then that coroutine can yield waiting for the request to complete, without blocking OnGUI. Something like this:
if(GUILayout.Button("Create Channel", GUI.skin.button)) {
StartCoroutine(DoCreateChannel());
}
...
private IEnumerator DoCreateChannel() {
string urlRequest = "http://mywebsite.com/game/AddChannel.aspx?ChannelName=" + ChannelName;
WWW request = new WWW(urlRequest);
yield return request;
string result = stripString(request.text);
int channelID = int.Parse(result);
if(channelID != 0) {
Message = "Channel Created!";
} else {
Message = "Channel Name already exist!";
}
}
If you need some of the processing to be encapsulated elsewhere then you can do that but it's best if the WWW request itself is handled here. Otherwise you end up needing to chain coroutines together, and you quickly reach a point where pretty much every function involved has to be a coroutine, and it's all badly coupled.
Tried your solution that makes sense.. It still works no problem in the Editor. After more debugging in the Webplayer, I found that it still doesn't work there, but just simply stops when it reaches the yield return. It never goes beyond that point, which is my current problem. This guy has a very similiar problem to what I'm having right now: http://answers.unity3d.com/questions/154299/yield-return-request-never-returns.html
I don't know why that would happen. You could check the webplayer logs in case there are error messages in there - some things throw exceptions and unless you look in the logs you'll never know about it.
Your answer
Follow this Question
Related Questions
Yield return request never returns 0 Answers
How to optimize the build game for unity webplayer? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers