[Unity3d]unity+asp.net实现动态搜索加载模型并且能够实现模型拖动缩放的功能
来源:互联网 发布:修改远程桌面端口 编辑:程序博客网 时间:2024/06/03 04:06
实现的功能要求:用户在文本框中输入要查询的模型型号(包括模糊搜索),服务器返回数据,然后客户端解析数据并且动态的生成数据列表,点击列表从服务器端下载打包的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 initializationvoid Start () {model = GameObject.Find("Model").transform;}// Update is called once per framevoid 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
10 1
- [Unity3d]unity+asp.net实现动态搜索加载模型并且能够实现模型拖动缩放的功能
- [Unity3d]鼠标旋转缩放拖动模型
- Unity3D 加载fbx模型,实现简单的方向控制
- unity 5.3 工业模型的细节展开功能实现
- Unity3d物体模型(实现旋转缩放平移自动旋转)
- Unity3d物体模型(实现旋转缩放平移自动旋转)
- OpenInventor实现三维模型的拖动
- 在Unity中通过触摸屏幕实现模型的旋转和缩放
- Unity之触屏控制_实现模型旋转和缩放—Touch类的使用
- 在Unity中通过触摸屏幕实现模型的旋转和缩放
- 在Unity中通过触摸屏幕实现模型的旋转和缩放
- android开发-图片缩放,拖动功能实现。
- surfaceView实现图片缩放拖动功能
- 自定义ImageView实现拖动,缩放,旋转功能
- 自定义ImageView实现拖动、旋转、缩放功能
- 如何使Unity+vuforia做出来的模型导入到Android手机中能够实现阴影效果
- [unity基础教程]Unity3D实现动态加载游戏资源(转)
- [unity基础教程]Unity3D实现动态加载游戏资源(转)
- Win7 禁止Ctrl+Alt+Del、Win+L等任意系统热键(利用IDA,windbg等工具分析)
- alsa分析:alsa的那些配置文件2
- Spring支持的bean作用域
- linux内核--中断机制
- CF211 Div 1 题解
- [Unity3d]unity+asp.net实现动态搜索加载模型并且能够实现模型拖动缩放的功能
- 编程规范
- 8天学习MongoDB——第八天 驱动实践
- tomcat创建一个windows服务
- ios 数据库
- 51wan《南帝北丐2》坐骑降临如虎添翼
- java中26个提示性能的地方
- Android模拟按键——源码环境下开发应用程序
- Android---屏幕适配