




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第Unity實(shí)現(xiàn)簡(jiǎn)單搖桿的制作利用UGUI制作一個(gè)簡(jiǎn)單搖桿,效果圖
1、首先建立兩個(gè)Image,然后將其中一個(gè)為父物體,另一個(gè)為子物體,并且調(diào)整好大小:
ps:將子物體的錨點(diǎn)設(shè)置為居中
2、在父物體上寫(xiě)個(gè)JoyStick.cs腳本:
usingUnityEngine;
usingUnityEngine.EventSystems;
usingSystem.Collections;
publicclassJoyStick:MonoBehaviour,IDragHandler,IEndDragHandler,IBeginDragHandler
publicstaticfloath,v;//傳出hv
publicfloatmaxDis;//最大距離
privateRectTransformchildRectTrans;
privateCoroutinecoroutine=null;
voidStart()
childRectTrans=transform.GetChild(0)asRectTransform;
publicvoidOnBeginDrag(PointerEventDataeventData)
if(coroutine!=null)
StopCoroutine(coroutine);
coroutine=null;
publicvoidOnDrag(PointerEventDataeventData)
Vector3outPos;
if(RectTransformUtility.ScreenPointToWorldPointInRectangle(this.transformasRectTransform,eventData.position,eventData.pressEventCamera,outoutPos))
childRectTrans.position=outPos;
//限制拖拽距離
childRectTrans.anchoredPosition=Vector2.ClampMagnitude(childRectTrans.anchoredPosition,maxDis);
//或者利用子物體和父物體的距離判斷是否超過(guò)最大距離,當(dāng)距離大于等于最大的距離時(shí)候,
//計(jì)算父物體和子物體的向量,然后利用向量*最大距離來(lái)限制拖拽距離
//if(Vector2.Distance(childRectTrans.position,this.transform.position)maxDis)
//Vector2dir=(childRectTrans.position-this.transform.position).normalized;
//childRectTrans.anchoredPosition=dir*maxDis;
GetHV();
publicvoidOnEndDrag(PointerEventDataeventData)
//當(dāng)結(jié)束拖動(dòng),要將物體歸0,為了加一點(diǎn)緩沖效果
//(1)可以使用dotween等補(bǔ)間動(dòng)畫(huà)插件,會(huì)減少很多
//rectTransform.DoAnchoredPos(Vector2.zero,0.5f);
//(2)或者使用攜程這里使用攜程
if(coroutine==null)
coroutine=StartCoroutine(IEToZeroPos(childRectTrans,0.1f));
privatevoidGetHV()
h=childRectTrans.anchoredPosition.x/maxDis;
v=childRectTrans.anchoredPosition.y/maxDis;
privateIEnumeratorIEToZeroPos(RectTransformrectTransform,floatduartion)
if(duartion==0f)
yieldreturnnull;
rectTransform.anchoredPosition=Vector2.zero;
GetHV();
coroutine=null;
yieldbreak;
Vector2currentpos=rectTransform.anchoredPosition;
floatoffx=currentpos.x/duartion;
floatoffy=currentpos.y/duartion;
while(rectTransform.anchoredPosition!=Vector2.zero)
yieldreturnnull;
rectTransform.anchoredPosition=newVector2(rectTransform.anchoredPosition.x-offx*Time.deltaTime,rectTransform.anchoredPosition.y-offy*Time.deltaTime);
GetHV();
if(rectTransform.anchoredPosition.sqrMagnitude8f)
rectTransform.anchoredPosition=Vector2.zero;
GetHV();
coroutine=null;
break;
}
另外附上Cube上面的腳本
privatevoidUpdate()
Vector3dir=newVector3(JoyStick.h,0,JoyStick.v);
if(dir.sqrMagnitude0)
transform.Translate(dir*3f*Time.deltaTime,Space.World);
QuaterniontargatRotate=Quaternion.LookRotation(dir,Vector3.up);
transform.rotation=Quaternion.Slerp(transform.rotation,targatRotate,3*Time.deltaTime);
}
加個(gè)使用doTween的吧
usingUnityEngine;
usingUnityEngine.EventSystems;
usingSystem.Collections;usingDG.Tweening;
publicclassJoyStick:MonoBehaviour,IDragHandler,IEndDragHandler,IBeginDragHandler
publicstaticfloath,v;//傳出hv
publicfloatmaxDis;//最大距離
privateRectTransformchildRectTrans;
privateCoroutinecoroutine=null;
voidStart()
childRectTrans=transform.GetChild(0)asRectTransform;
publicvoidOnBeginDrag(PointerEventDataeventData)
if(coroutine!=null)
StopCoroutine(coroutine);
coroutine=null;
publicvoidOnDrag(PointerEventDataeventData)
Vector3outPos;
if(RectTransformUtility.ScreenPointToWorldPointInRectangle(this.transformasRectTransform,eventData.position,eventData.pressEventCamera,outoutPos))
childRectTrans.position=outPos;
//限制拖拽距離
childRectTrans.anchoredPosition=Vector2.ClampMagnitude(childRectTrans.anchoredPosition,maxDis);
//或者利用子物體和父物體的距離判斷是否超過(guò)最大距離,當(dāng)距離大于等于最大的距離時(shí)候,
//計(jì)算父物體和子物體的向量,然后利用向量*最大距離來(lái)限制拖拽距離
//if(Vector2.Distance(childRectTrans.position,this.transform.position)maxDis)
//Vector2dir=(childRectTrans.position-this.transform.position).normalized;
//childRectTrans.anchoredPosition=dir*maxDis;
GetHV();
publicvoidOnEndDrag(PointerEventDataeventData)
//當(dāng)結(jié)束拖動(dòng),要將物體歸0,為了加一點(diǎn)緩沖效果
//(1)可以使用dotween等補(bǔ)間動(dòng)畫(huà)插件,會(huì)減少很多
rectTran
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025屆陜西省重點(diǎn)中學(xué)物理高二下期末考試試題含解析
- 湖北省襄陽(yáng)市重點(diǎn)中學(xué)2025年物理高二第二學(xué)期期末監(jiān)測(cè)試題含解析
- 公司管理層薪酬管理辦法
- 集體就診空間管理辦法
- 新西蘭旅游統(tǒng)計(jì)管理辦法
- 昆明中小學(xué)收費(fèi)管理辦法
- 上海市水閘管理辦法電話
- 熱電廠節(jié)約用水管理辦法
- 江西省危房鑒定管理辦法
- 長(zhǎng)春煙花燃放管理辦法
- DB34-T 4800-2024 退化天然林生態(tài)修復(fù)技術(shù)規(guī)程
- 一種紅外線圖像識(shí)別的變壓器綜合溫度監(jiān)測(cè)裝置
- GB/T 44260-2024虛擬電廠資源配置與評(píng)估技術(shù)規(guī)范
- 上海市培佳雙語(yǔ)學(xué)校2025屆高一數(shù)學(xué)第二學(xué)期期末達(dá)標(biāo)檢測(cè)試題含解析
- 醫(yī)院課件:《食源性疾病知識(shí)培訓(xùn)》
- 河北省石家莊市辛集市2023-2024學(xué)年八下物理期末教學(xué)質(zhì)量檢測(cè)試題及答案解析
- 廣西壯族傳統(tǒng)聚落及民居研究
- 2024年華陽(yáng)新材料科技集團(tuán)有限公司招聘筆試參考題庫(kù)附帶答案詳解
- 檔案整理及數(shù)字化服務(wù)項(xiàng)目整體服務(wù)方案
- 小學(xué)生心理健康綜合測(cè)試表
- 《中國(guó)八大菜系》課件
評(píng)論
0/150
提交評(píng)論