Unity實現(xiàn)簡單搖桿的制作
利用UGUI制作一個簡單搖桿,效果圖

1、首先建立兩個Image,然后將其中一個為父物體,另一個為子物體,并且調(diào)整好大小:

ps:將子物體的錨點設(shè)置為居中
2、在父物體上寫個JoyStick.cs腳本:
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class JoyStick : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler
{
public static float h, v; //傳出hv
public float maxDis; //最大距離
private RectTransform childRectTrans;
private Coroutine coroutine = null;
void Start()
{
childRectTrans = transform.GetChild(0) as RectTransform;
}
public void OnBeginDrag(PointerEventData eventData)
{
if (coroutine != null)
{
StopCoroutine(coroutine);
coroutine = null;
}
}
public void OnDrag(PointerEventData eventData)
{
Vector3 outPos;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(this.transform as RectTransform, eventData.position, eventData.pressEventCamera, out outPos))
{
childRectTrans.position = outPos;
//限制拖拽距離
childRectTrans.anchoredPosition = Vector2.ClampMagnitude(childRectTrans.anchoredPosition, maxDis);
//或者利用子物體和父物體的距離判斷是否超過最大距離,當(dāng)距離大于等于最大的距離時候,
//計算父物體和子物體的向量,然后利用向量*最大距離來限制拖拽距離
//if (Vector2.Distance(childRectTrans.position, this.transform.position) > maxDis)
//{
// Vector2 dir = (childRectTrans.position - this.transform.position).normalized;
// childRectTrans.anchoredPosition = dir * maxDis;
//}
GetHV();
}
}
public void OnEndDrag(PointerEventData eventData)
{
//當(dāng)結(jié)束拖動,要將物體歸0,為了加一點緩沖效果
//(1)可以使用dotween等補間動畫插件,會減少很多
//rectTransform.DoAnchoredPos(Vector2.zero,0.5f);
//(2)或者使用攜程 這里使用攜程
if (coroutine == null)
coroutine = StartCoroutine(IEToZeroPos(childRectTrans, 0.1f));
}
private void GetHV()
{
h = childRectTrans.anchoredPosition.x / maxDis;
v = childRectTrans.anchoredPosition.y / maxDis;
}
private IEnumerator IEToZeroPos(RectTransform rectTransform, float duartion)
{
if (duartion == 0f)
{
yield return null;
rectTransform.anchoredPosition = Vector2.zero;
GetHV();
coroutine = null;
yield break;
}
Vector2 currentpos = rectTransform.anchoredPosition;
float offx = currentpos.x / duartion;
float offy = currentpos.y / duartion;
while (rectTransform.anchoredPosition != Vector2.zero)
{
yield return null;
rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x - offx * Time.deltaTime, rectTransform.anchoredPosition.y - offy * Time.deltaTime);
GetHV();
if (rectTransform.anchoredPosition.sqrMagnitude < 8f)
{
rectTransform.anchoredPosition = Vector2.zero;
GetHV();
coroutine = null;
break;
}
}
}
}
另外附上Cube上面的腳本
private void Update()
{
Vector3 dir = new Vector3(JoyStick.h, 0, JoyStick.v);
if (dir.sqrMagnitude > 0)
{
transform.Translate(dir * 3f * Time.deltaTime,Space.World);
Quaternion targatRotate = Quaternion.LookRotation(dir, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targatRotate, 3 * Time.deltaTime);
}
}
加個使用doTween的吧
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections; using DG.Tweening;
public class JoyStick : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler
{
public static float h, v; //傳出hv
public float maxDis; //最大距離
private RectTransform childRectTrans;
private Coroutine coroutine = null;
void Start()
{
childRectTrans = transform.GetChild(0) as RectTransform;
}
public void OnBeginDrag(PointerEventData eventData)
{
if (coroutine != null)
{
StopCoroutine(coroutine);
coroutine = null;
}
}
public void OnDrag(PointerEventData eventData)
{
Vector3 outPos;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(this.transform as RectTransform, eventData.position, eventData.pressEventCamera, out outPos))
{
childRectTrans.position = outPos;
//限制拖拽距離
childRectTrans.anchoredPosition = Vector2.ClampMagnitude(childRectTrans.anchoredPosition, maxDis);
//或者利用子物體和父物體的距離判斷是否超過最大距離,當(dāng)距離大于等于最大的距離時候,
//計算父物體和子物體的向量,然后利用向量*最大距離來限制拖拽距離
//if (Vector2.Distance(childRectTrans.position, this.transform.position) > maxDis)
//{
// Vector2 dir = (childRectTrans.position - this.transform.position).normalized;
// childRectTrans.anchoredPosition = dir * maxDis;
//}
GetHV();
}
}
public void OnEndDrag(PointerEventData eventData)
{
//當(dāng)結(jié)束拖動,要將物體歸0,為了加一點緩沖效果
//(1)可以使用dotween等補間動畫插件,會減少很多
rectTransform.DoAnchoredPos(Vector2.zero,0.5f).OnUpdate(GetHV);
}
private void GetHV()
{
h = childRectTrans.anchoredPosition.x / maxDis;
v = childRectTrans.anchoredPosition.y / maxDis;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
同時兼容JS和C#的RSA加密解密算法詳解(對web提交的數(shù)據(jù)加密傳輸)
這篇文章主要給大家介紹了關(guān)于同時兼容JS和C#的RSA加密解密算法,通過該算法可以對web提交的數(shù)據(jù)進(jìn)行加密傳輸,文中通過圖文及示例代碼介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面來一起看看吧。2017-07-07
C#使用NPOI對Excel數(shù)據(jù)進(jìn)行導(dǎo)入導(dǎo)出
這篇文章介紹了C#使用NPOI對Excel數(shù)據(jù)進(jìn)行導(dǎo)入導(dǎo)出的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
WPF實現(xiàn)雷達(dá)圖(仿英雄聯(lián)盟)的示例代碼
這篇文章主要介紹了如何利用WPF實現(xiàn)雷達(dá)圖(仿英雄聯(lián)盟)的繪制,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-07-07
C#調(diào)用sql2000存儲過程方法小結(jié)
這篇文章主要介紹了C#調(diào)用sql2000存儲過程的方法,以實例形式分別對調(diào)用帶輸入?yún)?shù)及輸出參數(shù)的存儲過程進(jìn)行了詳細(xì)分析,非常具有實用價值,需要的朋友可以參考下2014-10-10
C#采用OpenXml實現(xiàn)給word文檔添加文字
這篇文章主要介紹了C#采用OpenXml實現(xiàn)給word文檔添加文字的方法,包括了用法的實例分析,是非常實用的技巧,需要的朋友可以參考下2014-09-09

