[Unity3d]unity+asp.net实现动态搜索加载模型并且能够实现模型拖动缩放的功能

实现的功能要求:用户在文本框中输入要查询的模型型号(包括模糊搜索),服务器返回数据,然后客户端解析数据并且动态的生成数据列表,点击列表从服务器端下载打包的assetbundle模型,加载到场景,并且点击其他的,确保场景中每次只显示一个模型,而且该模型能够缩放旋转。

Code:

搜索功能框:

[Unity3d]unity+asp.net实现动态搜索加载模型并且能够实现模型拖动缩放的功能

在搜索框中输入,然后向服务器端发送请求,服务器端返回数据,客户端解析数据,做出列表

[Unity3d]unity+asp.net实现动态搜索加载模型并且能够实现模型拖动缩放的功能

点击列表之后,会动态加载model

[Unity3d]unity+asp.net实现动态搜索加载模型并且能够实现模型拖动缩放的功能

创建在这个Model对象下:

[Unity3d]unity+asp.net实现动态搜索加载模型并且能够实现模型拖动缩放的功能

case "开始搜索": 				 //执行查询 searchText = GameObject.Find("Input").GetComponentInChildren<UILabel>().text; print(searchText); if(!string.IsNullOrEmpty(searchText)) { 	WWWForm formSearch = new WWWForm(); 	formSearch.AddField("type",searchText); 	WWW www = new WWW(searchUrl,formSearch); 	yield return www;       	if (www.error != null)         {               Debug.LogError(www.error);          }          else          { 		print(www.text); 		if(www.text!="NULL") 		{                     if (www.isDone)                     {                             int posY = 0;                             //父物体:panel、sprite                             //GameObject gobj = CreateEmptyObject(name, showpanel);                             gobj.transform.localPosition = new Vector3(0, 0, 50);                             gobj.transform.localScale = new Vector3(1, 1, 1); 					    	 					    	 					    	//生成生成三级导航                             ////UIScrollBar scrollbar = NGUITools.AddChild<UIScrollBar>(obj);                             //panel                             UIDraggablePanel panel = NGUITools.AddChild<UIDraggablePanel>(gobj);                             panel.transform.localPosition = new Vector3(500 + 140 - Screen.width, 0, 0);                             panel.transform.localScale = new Vector3(1, 1, 1);                             panel.scale = new Vector3(0, 1, 0);                             panel.GetComponent<UIPanel>().clipping = UIDrawCall.Clipping.SoftClip;                             //panel.GetComponent<UIPanel>().clipRange = new Vector4(0, 0, 280, 800);                             panel.GetComponent<UIPanel>().clipSoftness = new Vector2(1, 1); 					    	 					    	                             //sprite                             UISprite texture = NGUITools.AddSprite(gobj, atlas, "Sprite (MID_navigation_70%)");             //名字 						    //texture.pivot = UIWidget.Pivot.Top;                             texture.transform.localPosition = new Vector3(500 + 140 - Screen.width, 0, 0);                             //texture.transform.localScale = new Vector3(280, 800, 1);                             texture.alpha = 0.5f;                             //panel-grid                             UIGrid grid = NGUITools.AddChild<UIGrid>(panel.gameObject); 							                             grid.transform.localPosition = new Vector3(0, texture.transform.localScale.y / 2, -20);                             grid.transform.localScale = new Vector3(1, 1, 1);                             grid.arrangement = UIGrid.Arrangement.Vertical;                             grid.cellHeight = 40;                             grid.cellWidth = 505; 					                                 JsonData jd = JsonMapper.ToObject(www.text);                             for (int i = 0; i < jd.Count; i++)                             {                                 //根据解析的Name添加二级导航                                                             //print("Name:" + jd[i]["Name"]); 								//print("url:"+jd[i]["Url"]);                                 //grid-item                                 GameObject gobject = (GameObject)Instantiate(prefabItem1, new Vector3(0, 0, 0), Quaternion.identity); 								gobject.GetComponentInChildren<PrefbLoadmodel>().modelname = jd[i]["Type"].ToString(); //传入模型的名字                                 gobject.transform.parent = grid.transform;                                 gobject.transform.localPosition = new Vector3(0, 0, 0);                                 gobject.transform.localScale = new Vector3(1, 1, 1); 								//gobject.GetComponent<UIDragPanelContents>().draggablePanel = panel; 								gobject.GetComponentInChildren<UILabel>().text = jd[i]["Name"].ToString();                                 gobject.GetComponentInChildren<UISprite>().alpha = 0.5f;                                 grid.repositionNow = true;                                 //技术参数                                 //print("Telimgurl:" + jd[i]["Telimgurl"]);                                 //规格                                 //print("Imgurl:" + jd[i]["Imgurl"]); 								                             }                             posY = 40 * jd.Count;                             if (posY > 400)                             {                                 posY = 400;                             }                             texture.transform.localScale = new Vector3(505, posY + 10, 1);                             panel.GetComponent<UIPanel>().clipRange = new Vector4(0, 0, 505, posY + 10);  //设置左上角坐标和宽高                         } 	}     } } break;

PrefbLoadmodel.cs: 这个类放在预设的按钮下,作用是点击加载模型

using UnityEngine; using System.Collections; using LitJson;  public class PrefbLoadmodel : MonoBehaviour { 	 	static string ipStr = "http://ueige123.oicp.net/handler/"; 	string searchText = null; 	//产品中心导航链接    	private string productUrl = ipStr + "Search.ashx"; 	 	Transform model; 	 	public string modelname=null; 	// Use this for initialization 	void Start () { 		model = GameObject.Find("Model").transform; 		 	} 	 	// Update is called once per frame 	void Update () { 		 	} 	 	void OnClick() 	{	 		print("modelName:---"+modelname); 		 		isLoad(modelname+"(Clone)");  //将其他的隐藏 		 	} 	 	void isLoad(string name) 	{ 		if(model.FindChild(name)) 		{ 			for (int i = 0; i < model.childCount; i++)         	{             //当鼠标点击对象的名字与Modell中子对象名字不同相同时,设置激活状态:false             	if (model.GetChild(i).name != name)            		{ 					model.GetChild(i).gameObject.SetActive(false); 		    	} 				else 				{ 					model.GetChild(i).gameObject.SetActive(true);	 				} 			} 			print("存在"); 		}	 		else 		{ 			print("不存在"); 			for (int i = 0; i < model.childCount; i++)             {                                  model.GetChild(i).gameObject.SetActive(false);             } 			searchText = this.gameObject.GetComponentInChildren<UILabel>().text; 			StartCoroutine(SendName(searchText)); 		} 	} 	 	IEnumerator SendName(string name)     { 		WWWForm form = new WWWForm(); 		print("name:"+name); 		form.AddField("type",name); 		WWW resumewww = new WWW(productUrl,form); 		yield return resumewww; 		//记录加载时出现的错误信息           if (resumewww.error != null)          {              print("错误:" + resumewww.error);          }          else          {              print(resumewww.text); //服务器端返回的数据   			 JsonData jd = JsonMapper.ToObject(resumewww.text);              if (resumewww.isDone)              {                 string url1= jd[0]["Url"].ToString(); 				string type = jd[0]["Type"].ToString(); 				modelname = type; 				 WWW download = WWW.LoadFromCacheOrDownload(url1,1); 				yield return download; 				 				GameObject obj = Instantiate(download.assetBundle.mainAsset)as GameObject; 				obj.transform.parent = model; 				obj.transform.localPosition = new Vector3(4,0,2); 				obj.transform.localScale = new Vector3(1000, 1000,1000); 				 				obj.AddComponent<DragModel>();  //动态添加拖动组件 				download.assetBundle.Unload(false);              }          } 	} } 

DragModel.cs:这个类是模型动态的添加的组件,作用是能够鼠标旋转和缩放模型

using UnityEngine; using System.Collections;  public class DragModel : MonoBehaviour { 	 	 	private float x = 0.0f; 	private float y = 0.0f; 	 	private float xSpeed = 200.0f; 	private float ySpeed = 200.0f; 	 	private float zSpeed = 40f; 	 	private float minDistence = 0; 	private float maxDistence = 10; 	  	void Update ()  	{ 		 		if(Input.GetMouseButton(1)) 		{ 			x=Input.GetAxis("Mouse X") *xSpeed;  			y=Input.GetAxis("Mouse Y") *ySpeed; 			 			transform.Rotate(Vector3.up * -x *Time.deltaTime,Space.World); 			transform.Rotate(Vector3.right * y *Time.deltaTime,Space.World); 			 		} 		 		 		else if(Input.GetAxis("Mouse ScrollWheel")!=0) 		{ 			float ga = Input.GetAxis("Mouse ScrollWheel"); 			if(transform.position.z > minDistence && transform.position.z<maxDistence||transform.position.z<=minDistence && ga<0||transform.position.z>=maxDistence && ga>0) 			{ 				transform.Translate(Vector3.forward*-ga*zSpeed*Time.deltaTime,Space.World);	 			} 		} 	} } 


==================== 迂者 丁小未 CSDN博客专栏=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:858550         cocos2dx QQ群:280818155

====================== 相互学习,共同进步 ===================

转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/17587497

欢迎关注我的微博:http://weibo.com/u/2590571922
















上一篇:开启2个月,已有691+队伍报名繁星计划*第二届支付宝小程序创新大赛


下一篇:手把手教你把小程序登上支付宝首页