本文目錄一覽:
- 1、unity3d js 和web js的區別
- 2、學習web前端能做遊戲開發嗎?
- 3、如何在Unity3d中實現和網頁數據的交互
- 4、如何調用unity3d webgl 的js
- 5、如何在Unity3d中實現和網頁數據的交互?
unity3d js 和web js的區別
unity中的js與其說是js,不如說是Unity Script。unity中的js是會經過編譯的,其性能和本地速度差不多。在官方教材《unity 4.x從入門到精通》中unity稱C#,Boo,JS的性能是差不多的。
unity中的js腳本是可以和C#腳本等值替換的,所以自然也有一大堆數據類型,對象繼承等傳統語言及OOP的概念。不過var speed = 5這麼寫也是可以的,因為編譯器會自動理解成var speed:int = 5。但是其他數據類型比如GameObject、Transform就不行了,必須在聲明變量時指定數據類型。
自然,很多標準js中的特性在unity中也不支持,比如高階函數,閉包等。
Unity3D中的Math對象叫做Mathf。
Unity中的js可以直接調用Mono,C#封裝的dll等。
Unity中的調試語句用Debug.Log。
每行後面必須有分號。
總之,Unity中的js是會在運行前被編譯成本地代碼的。和標準js僅是寫法比較相似,內在是完全不同的。比如js是非阻塞的,而unity中的js是阻塞的;js是動態語言,而unity中的js則是不折不扣的靜態語言。所以前端攻城獅們想要熟練掌握untiy的js的話最好的方法就是多參考官方的js腳本,相信上手還是很快的,畢竟語法很相似。
學習web前端能做遊戲開發嗎?
當然可以啦,學習前端會製作很多炫酷的動畫,配合設計師的切圖和產品的邏輯,就能完成一個大大小小的遊戲了呀。之前的網頁遊戲是以flash 為主,目前逐漸被前端取代了。!!所以答案是 Yes
如何在Unity3d中實現和網頁數據的交互
1、unity向網頁發送數據的函數:Application.ExternalCall(“SayHello”,gameObject.name),這個函數將調用網頁中的SayHello函數,gameObject.name為傳遞的參數。
2、網頁向unity發送數據的函數:網頁中用GetUnity().SendMessage(message, “AcceptName”, buildingname)函數來調用unity中的函數,此函數的參數message為unity中的物體,AcceptName為物體上的函數,buildingname為傳遞的參數。
網頁中的函數如下:
1 function SayHello(message){//此函數來接收unity中發送出來的message值,並將處理後的數據再發送回unity中
2 jQuery.post(‘../Unity/javascript/DBhelper.ashx’, {id:message}, function(data)
3 {
4 var msg=JSON.parse(data);//將json數據解析
5 var buildingname = msg[0].Building_name;
6 var buildingcategory=msg[0].Building_category;
7 var buildingpic = msg[0].Building_pic;
8 GetUnity().SendMessage(message, “AcceptName”, buildingname);//向unity中的message物體上的MyFunction函數發送buildingname值
9 GetUnity().SendMessage(message, “AcceptCategory”, buildingcategory);
10
11 GetUnity().SendMessage(message, “AcceptImg”, buildingpic);
12 });
13 }
此函數將unity中發送的數據message傳到DBhelper.ashx中,在DBhelper.ashx中將傳遞過來的數據進行查詢等操作,然後再用GetUnity().SendMessage(message, “AcceptName”, buildingname)將處理好的數據buildingname傳給unity中的AcceptName函數。
以下是unity中的腳本,可以實現中文,關於中文的實現由於文章有限,在此不再說明,只說明怎樣接收網頁中的數據。
1 var chineseSkin : GUISkin;//在此可以選擇字體,並設置為中文。建議編輯器設為uft-8。
2
3 var buildingname:String;//用來接收從網頁中傳遞過來的buildingname值
4 var buildingcategory:String;//用來接收從網頁中傳遞過來的buildingcategory值
5
6 var buildingpic:Texture2D;//用來接收從網頁中傳遞過來的buildingpic值
7 var windowRect0 = Rect (20, 20, 250, 200);
8 var enable:boolean;
9 function Awake(){
10 enable = false ;
11 }
12 function OnMouseDown () {
13 Application.ExternalCall(“SayHello”,gameObject.name);// 向網頁中的SayHello函數發送gameObject.name數據
14 enable = true;
15 }
16 function AcceptName(bdname){//用於接收網頁中發送回來的數據
17 buildingname=bdname;
18 }
19 function AcceptCategory(buildingType){//用於接收網頁中發送回來的數據
20 buildingcategory=buildingType;
21 }
22
23 function AcceptImg(img){
24 var www :WWW = new WWW(“”+img+””);
25 yield www;
26 buildingpic=;
27 }
28 function OnGUI(){
29 GUI.skin=chineseSkin;
30 if(enable)
31 {
32 windowRect0 = GUI.Window (0, windowRect0, DoMyWindow, “屬性”);
33 }
34 }
35 function DoMyWindow (windowID : int) {
36 GUI.Label(Rect(10,50,80,30),”建築物名字”);
37 GUI.TextField(Rect(100,50,100,30),buildingname);
38 GUI.Label(Rect(10,100,80,30),”建築物類型”);
39 GUI.TextField(Rect(100,100,100,30),buildingcategory);
40
41 GUI.DrawTexture(Rect(10,150,200,50),buildingpic,ScaleMode.ScaleToFit,true,0);
42 if(GUI.Button(Rect(190,20,50,30),”退出”)){
43 enable = false;
44 }
45 GUI.DragWindow (Rect (0,0,10000,10000));
46 }
47 function OnMouseOver(){
48 transform.Rotate(0,Time.deltaTime*100,0,Space.World);
49 }
50 function OnMouseEnter(){
51 renderer.material.color = Color.blue;
52 }
53 function OnMouseExit(){
54 renderer.material.color = Color.yellow;
55 }
這是unity中的腳本,此腳本實現點擊物體,彈出物體的屬性。
如何調用unity3d webgl 的js
開發網頁js代碼交互:
方向一:你可以調用Application.ExternalCall() 和Application.ExternalEval()在你嵌入的網頁中執行 JavaScript代碼.
方向二:在網頁的js代碼中執行Unity中GameObjects的方法:例如
SendMessage (‘MyGameObject’, ‘MyFunction’, ‘foobar’)
《二》Application.ExternalCall調用JS函數
public static function ExternalCall(functionName: string, params args: object[]): void;
如何在Unity3d中實現和網頁數據的交互?
Unity3D和網頁數據交互的基本原理簡介:
1、Unity3D的遊戲引擎是和編輯器集成在一起的,所有它也是一個製作/開發平台。
2、Unity3D是使用JavaScript、C#作為核心腳本語言來驅動事個遊戲引擎。
3、平台可以
數據交互:
1、在Unity3D中調用網頁js函數
如果我們在html中有腳本函數;則在u3d中我們可用使用Application.ExternalCall調用js函數,該方法只適合在Web3D環境下使用。該方法支持基本類型的傳遞和數組傳遞,任何類型都會轉換成字符串類型使用。
例子代碼:
Application.ExternalCall(“SayHello”,”The game says hello!);//調用SayHello,傳遞一個字符串
2、在Unity3D中直接執行一段腳本代碼如:
Application.ExternalEval(“if(document.location.host!=’unity3d.com’){document.location=”;}”);
3、在js中調用Unity3D函數(傳遞消息等)
如果有Unity3D中有一段用JS寫的功能函數:
function MyFunction(param:String)
{
Debug.Log(param);
}
需要在JS中呼叫這個函數則可以這樣寫:
這裡要注意的是MyObject代表Unity3D中的一個場景名稱為MyObject,MyFunction是調用的函數,最後一個字符為傳遞的參數。
與php,jsp等的表單數據交互
與php,jsp等的表單數據交互很可能會是今後用到的主要方式,原理是利用form表彰傳遞數據,下面以php為例來進行說明。
Unity3D可以實現向某個指定頁面發送表單數據然後在php中使用_POST獲取傳遞迴來的表彰數據。
比如:$action = $_POST[“myform_action”];//定義一個變量$action用來獲取頁面傳遞過來的表單數據
if($action!=””){
echo $action;//如果接收到了數據則打印出數據內容
}
?
在Unity3D中我們發送數據的代碼如下:
var form = new WWWForm();//定義一個網頁表單
form.AddField(“myform_action”,”Action1″);//添加一個表彰字段名稱為myform_action內容是action1
var download = new WWW(“”,form);//發送表單數據到指定網址頁面
假如index.php執行的是數據庫/統計操作,我們就可以對傳遞的數據進行保存讀取或者其他操作了。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/247254.html