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

简介:

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

Code:

搜索功能框:


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


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


创建在这个Model对象下:


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
















相关文章
|
16天前
|
存储 文字识别 C#
.NET开源免费、功能强大的 Windows 截图录屏神器
今天大姚给大家分享一款.NET开源免费(基于GPL3.0开源协议)、功能强大、简洁灵活的 Windows 截图、录屏、Gif动图制作神器:ShareX。
|
2天前
|
机器学习/深度学习 存储 计算机视觉
r语言中对LASSO回归,Ridge岭回归和弹性网络Elastic Net模型实现-4
r语言中对LASSO回归,Ridge岭回归和弹性网络Elastic Net模型实现
32 11
|
2天前
|
机器学习/深度学习 算法 数据可视化
r语言中对LASSO回归,Ridge岭回归和弹性网络Elastic Net模型实现-3
r语言中对LASSO回归,Ridge岭回归和弹性网络Elastic Net模型实现
10 0
|
2天前
|
算法 数据可视化
r语言中对LASSO回归,Ridge岭回归和弹性网络Elastic Net模型实现-1
r语言中对LASSO回归,Ridge岭回归和弹性网络Elastic Net模型实现
17 0
|
8天前
|
算法
r语言中对LASSO回归,Ridge岭回归和Elastic Net模型实现
r语言中对LASSO回归,Ridge岭回归和Elastic Net模型实现
15 1
|
8天前
|
算法
r语言中对LASSO,Ridge岭回归和Elastic Net模型实现
r语言中对LASSO,Ridge岭回归和Elastic Net模型实现
12 0
|
15天前
|
机器学习/深度学习 数据挖掘 Go
中科院二区7.4分|NET基因肿瘤分型+生存预后模型+分子对接
在《Journal of Translational Medicine》上发表的最新研究中,科学家们鉴定了一种新的NET相关基因签名,用于预测弥漫性大B细胞淋巴瘤(DLBCL)患者的预后。他们发现8个基因(PARVB,LYZ,PPARGC1A,HIF1A,SPP1,CDH1,S100A9和CXCL2)与DLBCL生存率显著相关,建立了NRG遗传风险模型,该模型可能有助于指导DLBCL的治疗选择。研究包括基因表达数据的分析、预后特征的构建和验证,以及与免疫微环境和潜在治疗靶点的相关性探索。
17 0
|
1月前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
115 0
|
2月前
|
C# Windows
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
|
2月前
|
机器学习/深度学习 编解码 并行计算
SegNetr来啦 | 超越UNeXit/U-Net/U-Net++/SegNet,精度更高模型更小的UNet家族
SegNetr来啦 | 超越UNeXit/U-Net/U-Net++/SegNet,精度更高模型更小的UNet家族
44 0

相关实验场景

更多