unity3D與網頁的互動

來源:互聯網
上載者:User

由於項目需要,要求用unity來展示三維情境,並在三維中能夠方便的查詢資料庫等。一開始嘗試在unity中直接連接資料庫,當時連的xml,然而每次發布成網頁後都會出現路徑找不到等問題,所以迫不得已採用了unity向網頁傳送資料,網頁中處理資料(查詢資料庫),然後將處理過的資料再反傳送給unity,最終在unity中將其展示(在網頁中展示更為靈活)。

        原理很簡單:

1、unity向網頁發送資料的函數:Application.ExternalCall("SayHello",gameObject.name),這個函數將調用網頁中的SayHello函數,gameObject.name為傳遞的參數。

2、網頁向unity發送資料的函數:網頁中用GetUnity().SendMessage(message, "AcceptName", buildingname)函數來調用unity中的函數,此函數的參數message為unity中的物體,AcceptName為物體上的函數,buildingname為傳遞的參數。

網頁中的函數如下:

function SayHello(message){//此函數來接收unity中發送出來的message值,並將處理後的資料再發送回unity中       jQuery.post('../Unity/javascript/DBhelper.ashx', {id:message}, function(data)         {             var msg=JSON.parse(data);//將json資料解析              var buildingname = msg[0].Building_name;             var buildingcategory=msg[0].Building_category;             var buildingpic = msg[0].Building_pic;           GetUnity().SendMessage(message, "AcceptName", buildingname);//向unity中的message物體上的MyFunction函數發送buildingname值            GetUnity().SendMessage(message, "AcceptCategory", buildingcategory);             GetUnity().SendMessage(message, "AcceptImg", buildingpic);         });     }

此函數將unity中發送的資料message傳到DBhelper.ashx中,在DBhelper.ashx中將傳遞過來的資料進行查詢等操作,然後再用GetUnity().SendMessage(message, "AcceptName", buildingname)將處理好的資料buildingname傳給unity中的AcceptName函數。

       以下是unity中的指令碼,可以實現中文,關於中文的實現由於文章有限,在此不再說明,只說明怎樣接收網頁中的資料。

var chineseSkin : GUISkin;//在此可以選擇字型,並設定為中文。建議編輯器設為uft-8。  var buildingname:String;//用來接收從網頁中傳遞過來的buildingname值 var buildingcategory:String;//用來接收從網頁中傳遞過來的buildingcategory值  var buildingpic:Texture2D;//用來接收從網頁中傳遞過來的buildingpic值 var windowRect0 = Rect (20, 20, 250, 200); var enable:boolean; function Awake(){ enable = false ; } function OnMouseDown () { Application.ExternalCall("SayHello",gameObject.name);// 向網頁中的SayHello函數發送gameObject.name資料 enable = true; } function AcceptName(bdname){//用於接收網頁中發送回來的資料 buildingname=bdname; } function AcceptCategory(buildingType){//用於接收網頁中發送回來的資料 buildingcategory=buildingType; }  function AcceptImg(img){ var www :WWW = new WWW("http://localhost:1166/Unity/images/"+img+""); yield www; buildingpic=www.texture; } function OnGUI(){ GUI.skin=chineseSkin; if(enable) { windowRect0 = GUI.Window (0, windowRect0, DoMyWindow, "屬性"); } } function DoMyWindow (windowID : int) { GUI.Label(Rect(10,50,80,30),"建築物名字"); GUI.TextField(Rect(100,50,100,30),buildingname); GUI.Label(Rect(10,100,80,30),"建築物類型"); GUI.TextField(Rect(100,100,100,30),buildingcategory);  GUI.DrawTexture(Rect(10,150,200,50),buildingpic,ScaleMode.ScaleToFit,true,0); if(GUI.Button(Rect(190,20,50,30),"退出")){ enable = false; } GUI.DragWindow (Rect (0,0,10000,10000)); } function OnMouseOver(){     transform.Rotate(0,Time.deltaTime*100,0,Space.World); } function OnMouseEnter(){     renderer.material.color = Color.blue; } function OnMouseExit(){     renderer.material.color = Color.yellow;     }

 

這是unity中的指令碼,此指令碼實現點擊物體,彈出物體的屬性。

 

引自:http://www.cnblogs.com/Mygirl/archive/2011/04/02/2003116.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.