Unity網頁游戲


Unity網頁游戲是跑在瀏覽器的UnityWebPlayer插件中的,運行的模式是webplayer.unity3d+html

在嵌入UnityWebPlayer的網頁中會調用UnityObject2的initPlugin函數,將webplayer.unity3d作為參數傳入來初始化Unity網頁游戲的運行環境

注:游戲dll及其他托管dll在構建Web版本時都會寫入到webplayer.unity3d文件中

 

UnityObject2.js (其他鏈接1  其他鏈接2

擁有檢測 Unity Web Player插件、初始化其安裝和嵌入 Unity 內容的函數。可以將 UnityObject2.js 文件與 HTML 文件一起部署到網絡服務器,

但最好是從 Unity 服務器中直接加載,地址為:http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject2.js 或 https://ssl-webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject2.js(以獲得 https 支持)。

這樣,您將始終使用到UnityObject2 最新版本。而且托管在 Unity 服務器上的 UnityObject2.js 文件被壓縮了,使其變小,節約流量。

在Windows 系統Unity編輯器的Data\Resources 文件夾和 Mac OS X 中的Contents/Resources文件夾中可找到UnityObject2.js 源文件。

 

瀏覽器支持情況

由於各大瀏覽器廠商開始逐漸不支持瀏覽器插件,Unity5.4及之后的版本,UntiyWebPlayer被移除,只提供WebGL導出方式來支持網頁游戲。UnityWebPlayer Road Map

支持UnityWebPlayer的瀏覽器有:

windows平台瀏覽器 -- IE系列(注:最新為IE11。win10開始使用Edge作為缺省瀏覽器,Edge不支持插件的。另外為了訪問需要插件的網站,win10也提供了IE瀏覽器)

mac os x平台瀏覽器 -- Safari

跨平台瀏覽器 -- Chrome 45前的版本、Firefox 52.0前的版本、Opera 37前的版本

國產瀏覽器 -- 如:QQ瀏覽器、360瀏覽器等    一般有2個模式:兼容模式(IE)和極速模式(Chrome內核)

                    盡管集成的Chrome內核版本高於45,這些國產瀏覽器一般都會對集成的Chrome內核做修改,讓其能支持NPAPI插件的運行

注1:除了IE是使用ActiveX插件外,其他瀏覽器都是NPAPI插件

注2:Chrome 42版本開始缺省禁止NPAPI插件,但允許用戶手動開啟;到45版本則強制禁止了;但flashplayer仍然可以運行:chrome://settings/content/flash

注3:Firefox 52.0版本強制禁止除flashplayer外的所有NPAPI插件

注4:PPAPI和NPAPI說明

PPAPI是專為chrome內核而開發的,能夠在各種瀏覽器、操作系統和移動設備上使用,軟件短小精悍,功能強大,兼容性高。
NPAPI是當今最流行的插件架構,由Netscape開發后Mozilla維護,幾乎支持所有的瀏覽器,不過它存在很大的安全隱患,插件可以竊取系統底層權限,發起惡意攻擊。

2010年,Google在原有NPAPI(Netscape Plugin API)基礎上開發了新的PPAPI(Pepper Plugin API),將外掛插件全部放到沙盒里運行,
2012年Windows、Mac版本的Chrome瀏覽器先后升級了PPAPI Flash Player。
PPAPI的flash相較於NPAPI來講,內存占用更大,因為全在沙盒里面運行,而且flash每次更新基本上都是修補安全漏洞。

PPAPI:Opera 15、Chrome
NPAPI:適用於FireFox、Safari、Opera 12.17版以下

 

UnityWebPlayer插件

下載地址:https://unity3d.com/cn/webplayer/

              https://unity3d.com/cn/webplayer/setup

              http://webplayer.unity3d.com/download_webplayer-3.x/UnityWebPlayer.exe
              http://webplayer.unity3d.com/download_webplayer-3.x/UnityWebPlayerFull.exe

              如果需要特定版本的UntiyWebPlayer,可以安裝對應的Unity編輯器,在編輯器目錄下有對應的UnityWebPlayer.exe安裝文件

              形如:D:\Program Files (x86)\Unity\Editor\UnityWebPlayer.exe

Directory鍵值記錄是UnityWebPlayer的安裝目錄:%homepath%\AppData\LocalLow\Unity\WebPlayer

un.Directory鍵值記錄是UnityWebPlayer卸載程序Uninstall.exe所在的目錄

 

在IE中查詢UnityWebPlayer插件的信息與運行狀態:

win7 64bit  Internet Explorer9:主菜單 -- 管理加載項

 

從控制面板中卸載UnityWebPlayer插件:

 

安裝后的UnityWebPlayer目錄結構   -- 安裝在%homepath%\AppData\LocalLow\Unity\WebPlayer

player為Unity運行時框架,提供游戲運行的基本環境

mono虛擬機負責解釋執行游戲腳本邏輯

loader為瀏覽器插件:IE下為ActiveX控件(ocx),其他瀏覽器為NPAPI的dll,Mac為.plugin文件

player\Beta-4.63.x.x\Data\lib及mono\Beta-4.63.x.x\Data\lib中的dll均為托管dll

player\Beta-4.63.x.x\Data\unity default resources及mono\Beta-4.63.x.x\Data\unity_web_old為webplayer資源文件(ui貼圖,shader等等)

 

以下為UnityWebPlayer初始化過程中所訪問的url鏈接(使用finddler捕捉到的)

http://10.123.102.142/resources/Client/junhongwang/NZWebTGame.unity3d

http://autoupdate-revision.unity3d.com/revisions.plist?content=http%3a%2f%2f10.123.102.142%2fresources%2fClient%2fjunhongwang%2fNZWebTGame.unity3d&page=http%3a%2f%2f10.123.102.142%2fresources%2fClient%2fjunhongwang%2fNZWebTGame_InnerNew.html&runtime=3.x.x&

http://webplayer.unity3d.com//Channels/a7fe7b7f3d1f/UnityPlayer3.x.x-win32.zip // 會下載到%homepath%\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\COFOLVJA\UnityPlayer3.x.x-win32.zip

http://webplayer.unity3d.com//Channels/a7fe7b7f3d1f/Mono3.x.x-win32.zip // 會下載到%homepath%\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\C3AYLT70\Mono3.x.x-win32.zip

 

注:UnityWebPlayer.exe僅僅包含loader目錄、UnityBugReporter.exe和UnityWebPlayerUpdate.exe文件

     UnityWebPlayerFull.exe除了UnityWebPlayer.exe包含的內容外,還包含mono目錄和player目錄

     UnityWebPlayer插件在初始化,會根據當前游戲用的runtime版本號,去安裝目錄中找是否已經存在對應版本的mono和player,若不存在則下載它們

 

運行時臨時目錄  -- %Temp%\UnityWebPlayer

+---UnityWebPlayer
       +---log
             |           log_bb370ff473d10349836025afd6e6808f.txt   //日志文件
             |           manager_77b9ab931e2f344884b164d4660a0d19.txt
             |
             \---temp
                    \---bb370ff473d10349836025afd6e6808f    // 為了防止多個瀏覽器實例占用同一份dll,以下3個dll在運行每個實例前都會從UnityWebPlayer安裝目錄下拷貝
                                       mono-1-vc.dll
                                       webplayer_win.dll
                                       wrap_oal.dll

 

IE臨時目錄

reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Cache

下圖為:win7 64bit  Internet Explorer9的緩存目錄

 

由於UnityWebPlayer沙盒安全限制,Unity網頁游戲除了能讀取瀏覽器緩存目錄下的文件外,沒有任何權限訪問用戶其他目錄及文件

 

清除IE緩存命令:

rundll32.exe InetCpl.cpl,ClearMyTracksByProcess 8

 

開發調試技巧

在腳本中打印log到瀏覽器的console窗口中:

Application.ExternalCall( "BrowserLog", "my log message" );

然后在頁面的Js腳本中增加函數:

<script type="text/javascript" language="javascript">
function BrowserLog( msg)
{
    // log the message
    try
    {
        if (console != null)
        {
            //控制台沒有打開過時,該代碼執行時會異常(注:控制台打開后又關閉則不會有問題),所以需要放在try-catch塊中
            //否則會導致后面alert語句不會執行
            console.log(msg);
        }
    }
    catch (ex)
    {
        
    }

     alert(msg);
}
</script>

 

 

或者直接使用以下語句:

Application.ExternalCall( "console.log", "my log message" );

 

使用Development模式運行游戲:

1. 需要在html頁面中不要禁止右鍵菜單

2. 按住Alt鍵,在UnityWebPlayer插件上右鍵彈出如下快捷菜單(會多出一個Release Channel的菜單項)

3. 會下載%homepath%\AppData\LocalLow\Unity\WebPlayer\player\Beta-4.63.x.x-Development版本的UnityWebPlayer來運行游戲

   輸出的日志會包含堆棧信息

[2017-06-06 11:04:44 416]PublishLog:Device info: 
            DeviceId:d8c0a2227ec766787d43edbbba9fbd7a8d687a74
            deviceType:Desktop
            deviceModel:Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz (16313 MB)
            deviceName:NICOCHEN-PC0
            operatingSystem:Windows 7 Service Pack 1 (6.1.7601) 64bit
            systemMemorySize:16313
            graphicsDeviceName:NVIDIA GeForce GTX 660  
            graphicsDeviceVersion:Direct3D 9.0c [nvd3dum.dll 9.18.13.3788]
            graphicsMemorySize:1989
            graphicsShaderLevel:30
            Screen Size:1440x900
            DPI:96
            ProcessorType: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
            ProcessorCoreCount: 8

UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
WNEngine.Logger:GeneralLog(ELogType, String, Object[])
WNEngine.Logger:PublishLog(String, Object[])
WNEngine.Logger:PrintIdentifyLog()
WNEngine.Logger:Init()
WNGameBase.GameFramework:Awake()
 
(Filename: C:/buildslave/unity/build/artifacts/WebPlayerGenerated/UnityEngineDebug.cpp Line: 56)

 

Attach調試游戲

1. 需要在html頁面中打開調試開關

 

2. 打開游戲項目工程,“調試” -- “Attach Unity Debugger”

 

UnityWebPlayer Security Sandbox(安全沙盒)

受UnityWebPlayer沙盒限制,在網頁游戲邏輯里面訪問web player級別不支持的方法或外部Native模塊中方法,會拋出以下異常:

MethodAccessException: Attempt to access a private/protected method failed.

如果想在UntiyWebPlayer中使用Natvie模塊中的功能,可通過間接繞行的方式來實現:

(1)寫一個瀏覽器Plugin,在Plugin中調用Native模塊

(2)在UntiyWebPlayer中通過ExternalCall調用js函數,在js函數中再調用Plugin中對應的函數,最終實現對Native模塊的調用

 

參考

U3D-頁游-檢測機制-webplayer-調試方法

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM