天天看點

unity3D與網頁的互動

由于項目需要,要求用unity來展示三維場景,并在三維中能夠友善的查詢資料庫等。一開始嘗試在unity中直接連接配接資料庫,當時連的xml,然而每次釋出成網頁後都會出現路徑找不到等問題,是以迫不得已采用了unity向網頁傳送資料,網頁中處理資料(查詢資料庫),然後将處理過的資料再反傳送給unity,最終在unity中将其展示(在網頁中展示更為靈活)。

原理很簡單:

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

網頁中的函數如下:

 1functionSayHello(message){//此函數來接收unity中發送出來的message值,并将處理後的資料再發送回unity中

 2jQuery.post('../Unity/javascript/DBhelper.ashx',{id:message}, function(data)

 3        {

 4varmsg=JSON.parse(data);//将json資料解析

 5varbuildingname = msg[0].Building_name;

 6varbuildingcategory=msg[0].Building_category;

 7varbuildingpic = msg[0].Building_pic;

 8GetUnity().SendMessage(message, "AcceptName",buildingname);//向unity中的message物體上的MyFunction函數發送buildingname值

 9GetUnity().SendMessage(message,"AcceptCategory", buildingcategory);

10

11GetUnity().SendMessage(message,"AcceptImg", buildingpic);

12       });    

13}

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

以下是unity中的腳本,可以實作中文,關于中文的實作由于文章有限,在此不再說明,隻說明怎樣接收網頁中的資料。

 1varchineseSkin : GUISkin;//在此可以選擇字型,并設定為中文。建議編輯器設為uft-8。

 2

 3varbuildingname:String;//用來接收從網頁中傳遞過來的buildingname值

 4varbuildingcategory:String;//用來接收從網頁中傳遞過來的buildingcategory值

 5

 6var buildingpic:Texture2D;//用來接收從網頁中傳遞過來的buildingpic值

 7var windowRect0 = Rect (20, 20, 250, 200);

 8varenable:boolean;

 9function Awake(){

10enable = false ;

11}

//滑鼠按下去時觸發的事件

12functionOnMouseDown () {

13Application.ExternalCall("SayHello",gameObject.name);// 向網頁中的SayHello函數發送gameObject.name資料

14enable = true;

15}

16functionAcceptName(bdname){//用于接收網頁中發送回來的資料

17buildingname=bdname;

18}

19functionAcceptCategory(buildingType){//用于接收網頁中發送回來的資料

20buildingcategory=buildingType;

21}

22

23functionAcceptImg(img){

//讀取檔案夾下的圖檔檔案

24var www :WWW = newWWW("http://localhost:1166/Unity/images/"+img+"");

25yield www;

//為buildingpic設定紋理

26buildingpic=www.texture;

27}

//繪制GUI元素時觸發的事件

28functionOnGUI(){

29GUI.skin=chineseSkin;

30if(enable)

31{

//繪制一個窗體,記住第三個參數是方法名字

32windowRect0 = GUI.Window (0, windowRect0,DoMyWindow, "屬性");

33}

34}

//繪制一個窗體,windID是不可缺少的元素,指向窗體的索引值

35functionDoMyWindow (windowID : int) {

36GUI.Label(Rect(10,50,80,30),"建築物名字");

37GUI.TextField(Rect(100,50,100,30),buildingname);

38GUI.Label(Rect(10,100,80,30),"建築物類型");

39GUI.TextField(Rect(100,100,100,30),buildingcategory);

40

41GUI.DrawTexture(Rect(10,150,200,50),buildingpic,ScaleMode.ScaleToFit,true,0);

42if(GUI.Button(Rect(190,20,50,30),"退出")){

43enable = false;

44}

45GUI.DragWindow (Rect (0,0,10000,10000));

46}

//滑鼠在上面時觸發

47functionOnMouseOver(){

48transform.Rotate(0,Time.deltaTime*100,0,Space.World);

49}

//滑鼠進入時觸發

50functionOnMouseEnter(){

51renderer.material.color = Color.blue;

52}

//滑鼠離開時觸發

53functionOnMouseExit(){

54renderer.material.color =Color.yellow;    

55}

這是unity中的腳本,此腳本實作點選物體,彈出物體的屬性。

本文轉自蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366505,如需轉載請自行聯系原作者