unity3D与网页的交互---做项目的一点总结

unity3D与网页的交互---做项目的一点总结

(来自博客园,有鄙人的部分注释!张忠喜20xx-12-26)

由于项目需要,要求用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为传递的参数。 网页中的函数如下: 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中的脚本,此脚本实现点击物体,弹出物体的属性。

 

第二篇:Unity3D【飞机大战项目总结】

Unity3D——飞机大战项目总结

学飞机大战好几天了,今天就总结一下飞机大战所学的知识,所遇到的困难,还有常出现在错误。

一:资源包的导入,场景的搭建步骤不在阐述在这包的导入和创建新文件夹在回顾一下Scences存放场景视图,Scripts存放脚本,Resources存放资源包,资源包下有Textures存放图片,Materials存放材质,Audios存放声音,Madels存放模型。

二:脚本的建立

(1)玩家的飞机的前后左右的移动代码如下:

float x = Input.GetAxis ("Horizontal");//左右移动 float z = Input.GetAxis ("Vertical");//前后移动 transform.Translate (-x,0,-z);

(2)子弹的不停发射我们采用了鼠标事件用if判断代码如下:

if (Input.GetMouseButtonDown(0)||Input .GetKey(KeyCode.Space))

//鼠标左键或空格发射子弹

(3)子弹的个数我们用克隆的方法代码如下:

GameObject u=GameObject.Instantiate(yushe,transform.position,Quaternion.identity)as GameObject;//Instantiate有三个参数一个是所克隆的对象,第二个是所克隆的位置,第三个是位置。

(4)在克隆出子弹时,子弹是静止不动的,我们在给它加个力,有于我这个脚本是挂在子弹预设体上的代码如下:

u.rigidbody.AddForce(0,0,-8000);//有三个参数,一个是X轴的力,一个是Y轴的力,一个是Z轴的力。

(5)敌机的飞行就按傻瓜的来所代码如下:

transform.Translate (new Vector3(0,0,-1));//有三个参数,一个是X轴的飞行,一个是Y轴的飞行,一个是Z轴的飞行。

(6)敌机的子弹发射也是傻瓜类型代码如下:

float rocketTime=1;//给敌机所发射的时间

rocketTime -= Time.deltaTime;//时间递减 if(rocketTime<=0){//如果时间小于零为真 rocketTime=1; GameObject

aa=GameObject.Instantiate(EnPeoRo2,transform.position,Quaternion.identity)as GameObject;

aa.rigidbody.AddForce(0,0,8000);//给敌机子弹加力

(7)子弹的销毁,脚本添加给玩家子弹的预设体上代码如下:

Destroy (this.gameObject,2);//俩个参数一个是销毁目标,而是几秒后所消失

(8)敌机子弹的消失,脚本添加给敌机脚本代码如下:

Destroy (this.gameObject,2);

(9)玩家子弹打到敌机时,敌机消失代码如下

void OnTriggerEnter(Collider aa){//添加触发器

if (aa.tag=="PlayRocket") {//如果玩家的子弹碰到标签为PlayRocket则为真 //Debug.Log(collision.gameObject.name);

}else if (transform.transform.position.z>=227){//当敌机飞出电脑屏幕敌机自动 } } Destroy (this.gameObject); 销毁 (10)当敌机子弹打到玩家飞机时则玩家飞机生命减少,若干次销毁代码如下; void OnTriggerEnter(Collider aa){ } if (aa.tag=="EnenmyRocket") {//打到一次 } i--;//生命建少一次 if(i==0){//当减为零时为真 Destroy (this.gameObject);}//销毁目标

具体代码如下:

玩家飞机代码public class PlayerPlan : MonoBehaviour {

public GameObject yushe; int i=2; // Use this for initialization void Start () { } // Update is called once per frame void Update () { float x = Input.GetAxis ("Horizontal"); float z = Input.GetAxis ("Vertical"); transform.Translate (-x,0,-z); if (Input.GetMouseButtonDown(0)||Input .GetKey(KeyCode.Space)){ GameObject

u=GameObject.Instantiate(yushe,transform.position,Quaternion.identity)as GameObject;

} void OnTriggerEnter(Collider aa){ } if (aa.tag=="EnenmyRocket") { } i--; if(i==0){ Destroy (this.gameObject);} } u.rigidbody.AddForce(0,0,-8000);

玩家子弹代码如下:

using UnityEngine;

using System.Collections;

public class PlayRicket : MonoBehaviour {

}

敌机子弹代码:

using UnityEngine;

using System.Collections;

public class EnProRock2 : MonoBehaviour {

public GameObject EnPeoRo2; float rocketTime=1; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } //this.gameObject.rigidbody.AddForce(Vector3.back); Destroy (this.gameObject,2); // Use this for initialization void Start () { } // Update is called once per frame void Update () { rocketTime -= Time.deltaTime; if(rocketTime<=0){ rocketTime=1; GameObject

aa=GameObject.Instantiate(EnPeoRo2,transform.position,Quaternion.identity)as GameObject;

} } aa.rigidbody.AddForce(0,0,8000);

}

敌机代码如下:

using UnityEngine;

using System.Collections;

public class EnenmyPeople2 : MonoBehaviour {

}

if (aa.tag=="PlayRocket") { } } // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnTriggerEnter(Collider aa){ transform.Translate (new Vector3(0,0,-1)); //Debug.Log(collision.gameObject.name); Destroy (this.gameObject); }else if (transform.transform.position.z>=227){ Destroy (this.gameObject);//想了解更多到狗刨学习网

相关推荐