本帖最后由 zzlple 于 2016-5-20 21:59 编辑
上个教程我们讲到了unity网络访问的android的实现,本教程中我们着重讲述在IOS中网络访问的实现,先上代码大家一探究竟:
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)response;
{
if (response && self.manuallyHandleRedirect)
{
// notify TransportiPhone of the redirect and signal to process the next response.
if([response isKindOfClass:[NSHTTPURLResponse class]])
{
NSHTTPURLResponse *httpresponse = (NSHTTPURLResponse*)response;
NSMutableDictionary *headers = [httpresponse.allHeaderFields mutableCopy;
// grab the correct URL from the request that would have
// automatically been called through NSURLConnection.
// The reason we do this is that WebRequestProto's state needs to
// get updated internally, so we intercept redirects, cancel the current
// NSURLConnection, notify WebRequestProto and let it construct a new
// request from the updated URL
[headers setObject:[request.URL absoluteString forKey:@"Location";
httpresponse = [[NSHTTPURLResponse alloc initWithURL:response.URL statusCode:httpresponse.statusCode HTTPVersion:nil headerFields:headers;
[self handleResponse:httpresponse;
}
else
{
[self handleResponse:response;
}
SignalConnection(self);
[connection cancel;
return nil;
}
return request;
}
我们可以发现unity基于ios平台网络访问基于ios原生的实现,基于NSHTTPURLResponse的一种实现,具体源码大家可以在unity安装目录下的PlaybackEngines目录寻找,这样大家就有思路是否可以扩展自己的unity引擎了呢?经过这几次的探索,相信大家对unity引擎有一个基础认识,正如那句话所言,你所见并非你所见,一切竟在发现的道路上,今天关于unity协程那点事儿的教程我们到这里结束,下一篇我们着重讲述如何扩展你的unity引擎,让大家真正灵活扩展自己的引擎,谢谢大家![](static/image/smiley/default/biggrin.gif)
|
|
|
|
|
|