Unity 从UI中拖拽对象放置并拖动效果 附demo
程序员文章站
2022-03-11 22:32:42
需求:点击ui,在场景中生成3d对象,对象跟随鼠标移动,放置后可再次拖拽对象,改变其位置。做了一个小demo,如下图所示:实现大致思路: 射线碰撞检测 对象空间坐标变换(世界坐标->...
需求:点击ui,在场景中生成3d对象,对象跟随鼠标移动,放置后可再次拖拽对象,改变其位置。做了一个小demo,如下图所示:
实现大致思路:
- 射线碰撞检测
- 对象空间坐标变换(世界坐标->屏幕坐标、屏幕坐标->世界坐标)
首先为要生成3d对象的ui添加一个鼠标监听事件,脚本如下:
selectimage.cs
using system.collections; using system.collections.generic; using unityengine; using unityengine.eventsystems; public class selectimage : monobehaviour,ipointerdownhandler{ //需要被实例化的预制 public gameobject inistateprefab; //实例化后的对象 private gameobject inistateobj; // use this for initialization void start () { if (inistateprefab==null)return; //实例化预制 inistateobj=instantiate(inistateprefab) as gameobject; inistateobj.setactive(false); } //实现鼠标按下的接口 public void onpointerdown(pointereventdata eventdata) { inistateobj.setactive(true); //将当前需要被实例化的对象传递到管理器中 selectobjmanager.instance.attachnewobject(inistateobj); } }
将脚本挂载到ui对象上。
创建一个对象放置管理器,用于处理拖动的放置的逻辑:
selectobjmanager.cs
using system.collections; using system.collections.generic; using unityengine; public class selectobjmanager : monobehaviour { private static selectobjmanager _instance; public static selectobjmanager instance { get { return _instance; } } //物体z轴距摄像机的长度 public float _zdistance = 50f; //对象的缩放系数 public float _scalefactor=1.2f; //地面层级 public layermask _groundlayermask; int touchid; bool isdragging = false; bool istouchinput = false; //是否是有效的放置(如果放置在地面上返回true,否则为false) bool isplacesuccess = false; //当前要被放置的对象 public gameobject currentplaceobj = null; //坐标在y轴上的偏移量 public float _yoffset=0.5f; void awake () { _instance = this; } void update () { if (currentplaceobj == null) return; if (checkuserinput()){ movecurrentplaceobj(); }else if (isdragging){ checkifplacesuccess(); } } /// <summary> ///检测用户当前输入 /// </summary> /// <returns></returns> bool checkuserinput () { #if !unity_editor&&(unity_android||unity_ios) if (input.touches.length > 0) { if (!istouchinput) { istouchinput = true; touchid = input.touches[0].fingerid; return true; } else if (input.gettouch (touchid).phase == touchphase.ended) { istouchinput = false; return false; } else { return true; } } return false; #else return input.getmousebutton (0); #endif } /// <summary> ///让当前对象跟随鼠标移动 /// </summary> void movecurrentplaceobj () { isdragging = true; vector3 point; vector3 screenposition; #if !unity_editor&&(unity_android||unity_ios) touch touch = input.gettouch (touchid); screenposition = new vector3 (touch.position.x, touch.position.y, 0); #else screenposition = input.mouseposition; #endif ray ray = camera.main.screenpointtoray (screenposition); raycasthit hitinfo; if (physics.raycast (ray, out hitinfo, 1000, _groundlayermask)) { point = hitinfo.point; isplacesuccess = true; } else { point = ray.getpoint (_zdistance); isplacesuccess = false; } currentplaceobj.transform.position = point+new vector3(0,_yoffset,0); currentplaceobj.transform.localeulerangles = new vector3 (0, 60, 0); } /// <summary> ///在指定位置化一个对象 /// </summary> void createplaceobj(){ gameobject obj=instantiate(currentplaceobj) as gameobject; obj.transform.position=currentplaceobj.transform.position; obj.transform.localeulerangles=currentplaceobj.transform.localeulerangles; obj.transform.localscale*=_scalefactor; //改变这个对象的layer为drag,以便后续拖动检测 obj.layer=layermask.nametolayer("drag"); } /// <summary> ///检测是否放置成功 /// </summary> void checkifplacesuccess(){ if (isplacesuccess){ createplaceobj(); } isdragging=false; currentplaceobj.setactive(false); currentplaceobj=null; } /// <summary> /// 将要创建的对象传递给当前对象管理器 /// </summary> /// <param name="newobject"></param> public void attachnewobject(gameobject newobject){ if (currentplaceobj){ currentplaceobj.setactive(false); } currentplaceobj=newobject; } }
脚本中都有详细注释,我就不多解释了。
创建一个脚本,用于处理放置成功后,再次改变位置的逻辑:
dragobject.cs
using system.collections; using system.collections.generic; using unityengine; public class dragobject : monobehaviour { //只针对指定的层级进行拖动 public layermask _draglayermask; //指定当前要拖动的对象 public transform currenttransform; //是否可以拖动当前对象 public bool isdrag = false; //用于存储当前需要拖动的对象在屏幕空间中的坐标 vector3 screenpos = vector3.zero; //当前需要拖动对象的坐标相对于鼠标在世界空间坐标中的偏移量 vector3 offset = vector3.zero; void update () { if (input.getmousebuttondown (0)) { //将鼠标输入点转化为一条射线 ray ray = camera.main.screenpointtoray (input.mouseposition); raycasthit hitinfo; //如果当前对象与指定的层级发生碰撞,表示当前对象可以被拖动 if (physics.raycast (ray, out hitinfo, 1000f, _draglayermask)) { isdrag = true; //将当前需要拖动的对象赋值为射线碰撞到的对象 currenttransform = hitinfo.transform; //将当前对象的世界坐标转化为屏幕坐标 screenpos = camera.main.worldtoscreenpoint (currenttransform.position); //将鼠标的屏幕坐标转换为世界空间坐标,再与当前要拖动的对象计算两者的偏移量 offset = currenttransform.position - camera.main.screentoworldpoint (new vector3 (input.mouseposition.x, input.mouseposition.y, screenpos.z)); } else { isdrag = false; } } if (input.getmousebutton (0)) { if (isdrag == true) { var currentscreenpos = new vector3 (input.mouseposition.x, input.mouseposition.y, screenpos.z); //鼠标的屏幕空间坐标转化为世界坐标,并加上偏移量 var currentpos = camera.main.screentoworldpoint (currentscreenpos) + offset; currenttransform.position = currentpos; } } if (input.getmousebuttonup (0)) { isdrag = false; currenttransform = null; } } }
主要是一些坐标空间的变换和计算。
多余的我就不说了,脚本中都有很详细的注释,demo地址扫码后当前文章末尾获取。
到此这篇关于unity 从ui中拖拽对象放置并拖动的文章就介绍到这了,更多相关unity ui拖拽内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: oppok7x和红米10x哪个好 oppok7x和红米10x参数对比
下一篇: C语言指针一