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

using UnityEngine;
using UnityEngine.EventSystems;
public class JoyStick : MonoBehaviour, IDragHandler, IEndDragHandler
{
Transform point;
Vector3 startPos;//開始位置
Vector3 dir;//方向
float radius = 0;//需要移動的半徑
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,這樣的話上面的代碼是不能滿足要求的,花了一點時間才發(fā)現(xiàn)是這個原因,導(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)這個的startPos返回的是點擊屏幕的坐標(biāo),rectTransform是這個腳本掛載物體上的RectTransform的組件,然后減去eventData.position就知道坐標(biāo)的偏移值了,看一下代碼應(yīng)該都可以了解意思,這里就不過多的解釋了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#如何使用DateTime.Now.AddDays方法獲取任一天的信息
使用DateTime.Now屬性可以得到當(dāng)前的日期信息,此時調(diào)用ToString方法,并在該方法中添加指定的格式化字符串,可以按照要求輸出當(dāng)前日期的信息,本文介紹C#使用DateTime.Now.AddDays方法獲取任一天的信息,感興趣的朋友一起看看吧2024-01-01
C#實現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法
這篇文章主要介紹了C#實現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法,結(jié)合實例形式簡單分析了C#字符數(shù)組轉(zhuǎn)字符串及字符串轉(zhuǎn)字符數(shù)組的具體實現(xiàn)技巧,需要的朋友可以參考下2017-02-02
C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
這篇文章主要介紹了C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法,涉及WinForm中WebBrowser的相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
C#使用throw和throw?ex拋出異常的區(qū)別介紹
這篇文章介紹了C#使用throw和throw?ex拋出異常的區(qū)別,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
C#基于JsonConvert解析Json數(shù)據(jù)的方法實例
最近初接觸C#語言,發(fā)現(xiàn)JSON解析這塊和JAVA差異過大,下面這篇文章主要給大家介紹了關(guān)于C#基于JsonConvert解析Json數(shù)據(jù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04

