如何在Unity3d中实现和网页数据的交互?

如何在Unity3d中实现和网页数据的交互?,第1张

Unity3D和网页数据交互的基本原理简介:

1、Unity3D的游戏引擎是和编辑器集成在一起的,所有它也是一个制作/开发平台。

2、Unity3D是使用JavaScript、C#作为核心脚本语言来驱动事个游戏引擎。

3、平台可以

数据交互:

1、在Unity3D中调用网页js函数

如果我们在html中有脚本函数则在u3d中我们可用使用Application.ExternalCall调用js函数,该方法只适合在Web3D环境下使用。该方法支持基本类型的传递和数组传递,任何类型都会转换成字符串类型使用。

例子代码:

Application.ExternalCall("SayHello","Thegamesayshello!)//调用SayHello,传递一个字符串

2、在Unity3D中直接执行一段脚本代码如:

Application.ExternalEval("if(document.location.host!='unity3d.com'){document.location='http://unity3d.com'}")

3、在js中调用Unity3D函数(传递消息等)

如果有Unity3D中有一段用JS写的功能函数:

functionMyFunction(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中我们发送数据的代码如下:

varform=newWWWForm()//定义一个网页表单

form.AddField("myform_action","Action1")//添加一个表彰字段名称为myform_action内容是action1

vardownload=newWWW("www.xxx.com/index.php",form)//发送表单数据到指定网址页面

假如index.php执行的是数据库/统计 *** 作,我们就可以对传递的数据进行保存读取或者其他 *** 作了。

Unity3d连接SQL

Server数据库:

首先需要把Unity安装路径下的System.Data.dll和Mono.Data.SqliteClient.dll拷贝到当前unity工程的Assets文件夹下,因为之后在脚本中引用的.dll并不是framework提供的。脚本的内容:

using

UnityEngine

using

Sys...

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("http://localhost:1166/Unity/images/"+img+"")

25 yield www

26 buildingpic=www.texture

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中的脚本,此脚本实现点击物体,d出物体的属性。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6660249.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-26
下一篇 2023-03-26

发表评论

登录后才能评论

评论列表(0条)

保存