溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Unity如何實(shí)現(xiàn)簡單虛擬搖桿

發(fā)布時(shí)間:2020-08-03 14:17:00 來源:億速云 閱讀:167 作者:小豬 欄目:編程語言

小編這次要給大家分享的是Unity如何實(shí)現(xiàn)簡單虛擬搖桿,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

最近一直在倒騰用UGUI做虛擬搖桿,網(wǎng)上普遍的的做法就是使用以下的代碼,但是這個(gè)有些注意事項(xiàng),第一點(diǎn)就是Canvas的Render Mode必須是Screen Space Overlay,第二點(diǎn)就是掛載這個(gè)腳本的錨點(diǎn)的x,y必須是0.5,如圖下:

Unity如何實(shí)現(xiàn)簡單虛擬搖桿

using UnityEngine;
using UnityEngine.EventSystems;
public class JoyStick : MonoBehaviour, IDragHandler, IEndDragHandler
{
 Transform point;
 Vector3 startPos;//開始位置
 Vector3 dir;//方向
 float radius = 0;//需要移動(dòng)的半徑
 void Start()
 {
 point = transform.GetChild(0);
 radius = (transform as RectTransform).sizeDelta.x * 0.5f;
 startPos = point.position;
 }
 public void OnDrag(PointerEventData eventData)
 {
 point.position = eventData.position;
 dir = (point.position - startPos).normalized;
 if (Vector3.SqrMagnitude(point.position - startPos) > radius * radius)
 point.position = startPos + dir * radius;
 }
 public void OnEndDrag(PointerEventData eventData)
 {
 point.localPosition = Vector3.zero;
 }
}

如果Canvas的Render Mode是Screen Space Camera,這樣的話上面的代碼是不能滿足要求的,花了一點(diǎn)時(shí)間才發(fā)現(xiàn)是這個(gè)原因,導(dǎo)致上面的代碼不適用的,最后把代碼重寫了一下,終于可以成功了!

public class JoyStick : MonoBehaviour, IDragEvent
{
 private Canvas canvas;
 private RectTransform rectTransform;//坐標(biāo)
 private static Quaternion amendAngle;
 private static float mRadius = 0,v=0, h=0;
 private static Transform point;
 private static Vector3 initPos;
 private static Vector2 startPos;
 private void Start()
 {
 point = transform.GetChild(0);
 canvas = GameObject.Find("UIRoot").GetComponent<Canvas>();
 rectTransform = transform as RectTransform; //也可以寫成this.GetComponent<RectTransform>(),但是不建議;
 mRadius = (transform as RectTransform).sizeDelta.x * 0.5f;
 initPos = point.localPosition;
 h = v = 0;
 }
 
 public void OnBeginDrag(PointerEventData eventData)
 {
 RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, canvas.worldCamera, out startPos);
 startPos = eventData.position - startPos;
 h = v = 0; 
 }
 
 public void OnDrag(PointerEventData eventData)
 {
 point.localPosition = eventData.position - startPos;
 Vector3 dir = (point.localPosition - initPos).normalized;
 v = dir.normalized.x; h = dir.normalized.y;
 if (Vector3.SqrMagnitude(point.localPosition - initPos) > mRadius * mRadius)
 point.localPosition = initPos + dir * mRadius;
 }
 
 public void OnEndDrag(PointerEventData eventData)
 {
 point.localPosition = Vector3.zero;
 h = v = 0; 
 }
}

RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, canvas.worldCamera, out startPos)這個(gè)的startPos返回的是點(diǎn)擊屏幕的坐標(biāo),rectTransform是這個(gè)腳本掛載物體上的RectTransform的組件,然后減去eventData.position就知道坐標(biāo)的偏移值了,看一下代碼應(yīng)該都可以了解意思,這里就不過多的解釋了。

看完這篇關(guān)于Unity如何實(shí)現(xiàn)簡單虛擬搖桿的文章,如果覺得文章內(nèi)容寫得不錯(cuò)的話,可以把它分享出去給更多人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI