Unity UI拖拽模型选择功能
指定一块区域,玩家鼠标or手指拖拽这个区域,模型会进行偏移,并用于进行人物、道具的选择
给模型定义一些属性
usingSystem.Collections; usingSystem.Collections.Generic; usingUnityEngine; publicclassUIModelUtil:MonoBehaviour { publicAnimatoranimator; publicintid; publicintindex; }
模型控制
usingSystem.Collections; usingSystem.Collections.Generic; usingUnityEngine; publicclassUIModelControl:MonoBehaviour { publicTransformmodelsParent; publicTransformcenterPos; publicfloatinterval; publicboolloop; Listmodels; boolisPressing; publicUIDragdragComp; Vector3mousePos; privatevoidAwake() { if(models==null) { inti=0; models=newList (); foreach(UIModelUtilutilinmodelsParent.GetComponentsInChildren ()) { models.Add(util); //util.index=i; Vector3pos=Vector3.zero; pos.x=i*interval; util.transform.localPosition=pos; i++; } } } privatevoidStart() { JumpToSelect(); } privatevoidUpdate() { //接受拖拽事件 if(isPressing) { floatx=GetInputDeltaX(); intdir=0; if(x>0)dir=1; elseif(x<0)dir=-1; //分辨率修正 if(dir==0)return; x=Mathf.Abs(x)/(Screen.width)*800f; if(x>800f)x=800f; //偏移 floatcurrectX=Mathf.Lerp(0,interval,x/800f)*dir; Vector3pos=modelsParent.position; pos.x+=currectX; Transformright=GetRight().transform; Transformleft=GetLeft().transform; //不循环时候设置边框 if(models.Count>2||!loop||models.Count==1) { if(right.localPosition.x+interval/10<-pos.x)pos.x=-(right.localPosition.x+interval/10); elseif(left.localPosition.x-interval/10>-pos.x)pos.x=-(left.localPosition.x-interval/10); //modelsParent.position=pos; } //只有两个循环的时候 elseif(models.Count==2&&loop) { Transformselected=GetSelect().transform; //当前是右边那个且向右拖拽 if(selected==right&&dir<0) { Vector3leftPos=left.localPosition; leftPos.x=right.localPosition.x+interval; left.localPosition=leftPos; } //当前是左边那个且向左拖拽 elseif(selected==left&&dir>0) { Vector3rightPos=right.localPosition; rightPos.x=left.localPosition.x-interval; right.localPosition=rightPos; } } modelsParent.position=pos; AfterSelect(); } } voidAfterSelect() { foreach(UIModelUtilutilinmodels) { floatdis=GetXDis(util); //设置显示 if(dis>interval) util.gameObject.SetActive(false); else { //越靠近中间越前 util.gameObject.SetActive(true); floatt=Mathf.Abs(dis)/interval; floaty=Mathf.Lerp(centerPos.position.z,modelsParent.position.z,t); Vector3pos=util.transform.position; pos.z=y; util.transform.position=pos; } } //循环时候位置修正 if(loop&&models.Count>2) { Transformright=GetRight().transform; Transformleft=GetLeft().transform; Transformselected=GetSelect().transform; if(selected==right) { Vector3pos=right.position; pos.x+=interval; left.position=pos; } elseif(selected==left) { Vector3pos=left.position; pos.x-=interval; right.position=pos; } } //设置UI选中状况 dragComp.OnSelected(GetSelect().id,GetSelect().index); } //通过id选中 UIModelUtilGetById(intid) { if(models==null)returnnull; UIModelUtiltarget=null; foreach(UIModelUtilutilinmodels) { if(util.id==id)returnutil; } returntarget; } //获取当前选中 UIModelUtilGetSelect() { if(models==null)returnnull; floatmin=9999; UIModelUtiltarget=null; foreach(UIModelUtilutilinmodels) { floatdis=Mathf.Abs(GetXDis(util)); if(dis max) { target=util; max=dis; } } returntarget; } //所有模型最左边的那个 UIModelUtilGetLeft() { if(models==null)returnnull; floatmin=9999; UIModelUtiltarget=null; foreach(UIModelUtilutilinmodels) { floatdis=util.transform.localPosition.x; if(dis UI接受点击事件:
usingSystem.Collections; usingSystem.Collections.Generic; usingUnityEngine; usingUnityEngine.EventSystems; publicclassUIDrag:MonoBehaviour,IPointerDownHandler,IPointerUpHandler { publicUIModelControlcontrol; virtualpublicvoidOnPointerDown(PointerEventDatadata) { control.OnPress(); } virtualpublicvoidOnPointerUp(PointerEventDatadata) { control.OnRelease(); } virtualpublicvoidOnSelected(intid,intindex) { } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。