




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第Unity實現(xiàn)簡單搖桿的制作利用UGUI制作一個簡單搖桿,效果圖
1、首先建立兩個Image,然后將其中一個為父物體,另一個為子物體,并且調(diào)整好大小:
ps:將子物體的錨點設(shè)置為居中
2、在父物體上寫個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);
//或者利用子物體和父物體的距離判斷是否超過最大距離,當(dāng)距離大于等于最大的距離時候,
//計算父物體和子物體的向量,然后利用向量*最大距離來限制拖拽距離
//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é)束拖動,要將物體歸0,為了加一點緩沖效果
//(1)可以使用dotween等補間動畫插件,會減少很多
//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);
}
加個使用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);
//或者利用子物體和父物體的距離判斷是否超過最大距離,當(dāng)距離大于等于最大的距離時候,
//計算父物體和子物體的向量,然后利用向量*最大距離來限制拖拽距離
//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é)束拖動,要將物體歸0,為了加一點緩沖效果
//(1)可以使用dotween等補間動畫插件,會減少很多
rectTran
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 紡織工藝優(yōu)化實踐試題及答案
- 《高效課堂管理》課件
- 采購試題庫及答案
- 酒館轉(zhuǎn)讓合同協(xié)議書
- 企業(yè)衛(wèi)生承包合同協(xié)議書
- 轉(zhuǎn)讓飯店合同協(xié)議書
- 餐飲外賣合同協(xié)議書
- 合同協(xié)議書副本怎么寫
- 民房合同協(xié)議書
- 合同協(xié)議書的間隔
- 中等職業(yè)學(xué)校物理教學(xué)大綱
- 靜脈輸血法并發(fā)癥的預(yù)防和處置
- 八大特種作業(yè)試題及答案
- 龍門吊安裝、拆卸工程安全監(jiān)理實施細(xì)則
- 機關(guān)事業(yè)單位工作人員考勤管理辦法
- 2025年湖北省中考?xì)v史模擬試卷附答案
- 抑郁癥考試試題及答案
- GB/T 25020.1-2025電氣化鐵路接觸網(wǎng)支柱第1部分:鋼支柱
- 新能源汽車維修技術(shù)論文
- 2025年國家統(tǒng)一法律職業(yè)資格考試真題(帶答案)
- 生物安全管理體系文件
評論
0/150
提交評論