unity中的web player与webGL

unity开发网页游戏,发布的时候可以选择web player跟webGL。unity5.4版本以后发布只能选择webGL,web player能用的最高版本是5.3.8。两者比较如下:

 

Web Player

 

 

 

 

优点

  • 成熟:在开发过程中遇到问题,在论坛或其它地方可以找到答案
  • 稳定:bug更少,经过Unity不同版本的迭代更新,web player技术稳定
  • 轻量级:打开网页即可进行游戏,不需要安装客户端

缺点

  • 插件支持:浏览器需要安装插件来支持unity3d游戏
  • 停止支持:chrome从2015年9月后的45版本停止支持NPAPI plugin 包括web player,后面的chrome版本运行不了web player
  • 文件限制:出于安全原因,没有本地databases或者其它Streaming Assets

 

unity网页游戏网站:http://www.pacogames.com/en

在chrome继续使用web player,参考:http://www.gameload.top/      

http://www.gameflare.com/news/article/how-to-run-unity-games/

chrome历史版本下载:http://www.chromedownloads.net/chrome64win/

查看chrome版本:chrome://version/ 或者点击 帮助->关于google chrome 

chrome44版本及以下版本可以运行web player,设置:浏览器里输入 chrome://flags/#enable-npapi ,启用NPAPI,重启浏览器,安装web player插件,启用web player插件

 

 

WebGL

优点

  • 无插件:firefox及chrome无需插件即可支持
  • 新事物:技术在更新迭代,在未来也许webgl会替代web player
  • 美好的未来:在移动市场,google及mozilla大力支持webgl

 

 

缺点

  • Audio/浏览器:音频方面目前仅仅支持mp3,IE不支持
  • 不稳定:目前bug和坑比较多,因技术新使用人群较少,这个平台遇到的bug,论坛上比较难找到解答
  • 性能:和本地代码相比,webgl在某些方面性能较低下,比如支持多线程的3D物理
  • 文件大:打包文件的size比web player更大
  • 打包慢:build时等待的时间长,使用新技术il2cpp

 

[官方]在WebGL 平台上的Unity 性能基准 http://forum.china.unity3d.com/thread-681-1-1.html

[官方] Unity 5.3 中WebGL的更新 http://forum.china.unity3d.com/thread-12832-1-1.html

 

如果选择webGl,那么网络socket要选择webSocket,unity中带System.Net相关socket都不能用,只能用webSocket相关的库(需下载),同时服务器中需要相应的解析webSocket,先握手后才能用socket进行通信。web player则没有这个限制,socket正常使用。下面就web player的开发与发布部署,说下相关内容。

 

1.在调用connect之前,先调用 Security.PrefetchSocketPolicy(ip, 843, 3000)。这是Unity WebPlayer Security SandBox机制,Unity3d为Web Player平台搞了一个security SandBox机制,Only在Web Player的安全机制中,在使用Socket时需要服务器配置一个服务安全策略。如果没有这方面处理,Security SandBox会阻止程序的Socket连接,导致不能通信。

Unity提供了一个“sockpol.exe”这么一个工具,在“...\Unity\Editor\Data\Tools\SocketPolicyServer“路径下有sockpol.exe和它的源码。如果你的服务器端是Windows平台的话,直接Copy一个sockpol.exe到服务器端,在CMD中执行

 cd D:\softinstalled\unity5.3.8\Unity\Editor\Data\Tools\SocketPolicyServer

sockpol --all

即可为服务器端配置好Security SandBox安全策略。

sockpol.exe干的活就是监听Web Player平台获取Security SandBox安全策略时需要连接服务器端的843端口,监听到843端口有请求时,发送给请求的客户端一个crossdomain.xml配置,内容为标准的crossdomain.xml文件格式:

<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*" to-ports="1-65536"/>
</cross-domain-policy>

这样客户端就能获取到Security SandBox安全策略并进行网络活动了。其中,执行sockpol.exe的参数--all的意义就是设置服务器的Security SandBox安全策略为允许任何IP访问服务器的任何端口。

如果是linux服务器,写一个脚本:

#!/bin/sh
while true; do echo '<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*" to-ports="1-65536"/>
</cross-domain-policy>' | nc -l 843; done

 

保存为serverPolicy.sh。然后让脚本在后台运行:

nohup shserverPolicy.sh

nohup命令会忽略SIGHUP信号,从而终端退出时不会影响到后台作业。然后启动其他游戏服务器进程

 

2.搭建IIS,外部通过IP直接访问。web服搭建在windows上,游戏服在linux上。发布的web player游戏的html改为index.xml,增加MIME扩展名为.unity3d,类型为 application/octet-stream

 

3.网页上查看游戏运行日志。On Windows 7 the debug log is located at C:\Users*Your User Name Here*\AppData\Local\Temp\UnityWebPlayer\log

在浏览器中调试Unity web player的方法:按住 ALT键,然后点击右键 - Release Channel - Dev

 

 

 

4.如果需求要是有POST请求返回数据,则要在IIS下web.config中添加

5.html中查看浏览器版本的方法:

 

<script>

document.write("用户代理: " + navigator.userAgent);

</script>

 

判断chrome版本的方法:

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-browser/0.0.8/jquery.browser.min.js"></script>
<script>
$(document).ready(function(){

	var ver = navigator.userAgent;//获取用户端的Web浏览器版本号  
	var a= ver.indexOf("Chrome");//检测特殊字符串"MSIE"的位置
	var versionNum = parseFloat(ver.substring(a+7,a+9));

	var isChrome = navigator.userAgent.toLowerCase().match(/chrome/) != null;
	if (isChrome && versionNum >= 45) { //detect Chrome 45+
				
	var myJsonString = JSON.stringify({ title: 'Three', file: 'http://192.168.6.107:8086/webplayer0710.unity3d', type: "unity", width: 800, height: 600 });
	try{ //IE8 does not support window.btoa   
		var insert_data = window.btoa(myJsonString); 
	}catch(e){ 
	functionToHandleError(e);
	}
				
					 
	document.write('<center><a href="gameload://' + insert_data + '/"><img alt="Play game with Gameload!" src="http://data.gameload.top/download/playgameload.png" /></a><br /><br />Install Gameload to play Unity games<br /><a href="http://data.gameload.top/download/gameload.exe" title="Install Gameload now!"><img alt="Install Gameload now!" src="http://data.gameload.top/download/getgameload.png" /></a></center>');
			}
		});
</script>


6.web player游戏下载美术资源失败,需要IIS中的 MIME类型添加相应后缀

 

 

 

unity web playerWeb Player License Agreement PLEASE READ CAREFULLY: BY INSTALLING THE SOFTWARE (AS DEFINED BELOW), YOU (EITHER ON BEHALF OF YOURSELF AS AN INDIVIDUAL OR ON BEHALF OF AN ENTITY AS ITS AUTHORIZED REPRESENTATIVE) AGREE TO ALL OF THE TERMS OF THIS END USER LICENSE AGREEMENT REGARDING THE USE OF THE SOFTWARE. 1) GRANT OF LICENSE: You may install this Software on your computer to experience Unity web content. 2) TITLE: You acknowledge that no title to the intellectual property in the Software is transferred to you. Title, ownership, rights, and intellectual property rights in and to the Software shall remain that of Unity Technologies. The Software is protected by copyright laws of the United States and international treaties. 3) ANONYMOUS USAGE STATISTICS: You accept that the first time the Unity Web Player is used, anonymous information about the computer it's loaded on is submitted to Unity Technologies ApS. This only happens once time, and contains no personally identifiable information. The information submitted is: (a) Operating system and version (b) The make of the CPU, and number of CPUs present (c) The graphics card type and vendor name (d) Graphics card driver name and version (example: "nv4disp.dll 6.10.93.71") (e) Which graphics API is in use (example: "OpenGL 2.1" or "Direct3D 9.0c") (f) Amount of system and video RAM present (g) Current desktop resolution (h) Version of the Unity Web Player (i) A number describing whether running on Mac or Windows (j) A checksum of all the data that gets sent to verify that it did transmit correctly 3) DISTRIBUTION: You acknowledge that only Unity Technologies ApS and its designated distribution partners may distribute the Unity Web Player, without a special permission.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值