频道栏目
首页 > 程序开发 > 综合编程 > 其他综合 > 正文
[Unity3d]动态搜索加载模型并且能够实现模型拖动缩放的功能
2013-12-27 11:20:37         来源:学无止境的专栏  
收藏   我要投稿

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

Code:

搜索功能框:

\

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

\

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

\

创建在这个Model对象下:

\

 

case 开始搜索:
				
//执行查询
searchText = GameObject.Find(Input).GetComponentInChildren().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(obj);
                            //panel
                            UIDraggablePanel panel = NGUITools.AddChild(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().clipping = UIDrawCall.Clipping.SoftClip;
                            //panel.GetComponent().clipRange = new Vector4(0, 0, 280, 800);
                            panel.GetComponent().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(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().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().draggablePanel = panel;
								gobject.GetComponentInChildren().text = jd[i][Name].ToString();
                                gobject.GetComponentInChildren().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().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 = https://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().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();  //动态添加拖动组件
				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 && ga>0)
			{
				transform.Translate(Vector3.forward*-ga*zSpeed*Time.deltaTime,Space.World);	
			}
		}
	}
}


 

 

 

点击复制链接 与好友分享!回本站首页
相关TAG标签 模型 功能 动态
上一篇:[Unity3d]控制物体的旋转以及缩放
下一篇:[Unity3D][NGUI]选中某个图标后高亮
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站