Unity實(shí)現(xiàn)簡單搖桿的制作
更新時間:2021年09月16日 15:17:29 作者:Zero_LJ
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡單搖桿的制作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
利用UGUI制作一個簡單搖桿,效果圖

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

ps:將子物體的錨點(diǎn)設(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)距離大于等于最大的距離時候,
//計(jì)算父物體和子物體的向量,然后利用向量*最大距離來限制拖拽距離
//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,為了加一點(diǎn)緩沖效果
//(1)可以使用dotween等補(bǔ)間動畫插件,會減少很多
//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)距離大于等于最大的距離時候,
//計(jì)算父物體和子物體的向量,然后利用向量*最大距離來限制拖拽距離
//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,為了加一點(diǎn)緩沖效果
//(1)可以使用dotween等補(bǔ)間動畫插件,會減少很多
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)文章
剖析設(shè)計(jì)模式編程中C#對于組合模式的運(yùn)用
這篇文章主要介紹了設(shè)計(jì)模式編程中C#對于組合模式的運(yùn)用,理論上來說組合模式包含抽象構(gòu)件、樹葉構(gòu)件和樹枝構(gòu)件三個角色,需要的朋友可以參考下2016-02-02
WPF設(shè)置窗體可以使用鼠標(biāo)拖動大小的方法
這篇文章主要介紹了WPF設(shè)置窗體可以使用鼠標(biāo)拖動大小的方法,涉及針對窗口的操作與設(shè)置技巧,具有很好的借鑒價(jià)值,需要的朋友可以參考下2014-11-11
c# 使用計(jì)時器和觀察者模式實(shí)現(xiàn)報(bào)警推送需求
這篇文章主要介紹了c# 使用計(jì)時器和觀察者模式實(shí)現(xiàn)報(bào)警推送需求,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
C#修改及重置電腦密碼DirectoryEntry實(shí)現(xiàn)方法
這篇文章主要介紹了C#修改及重置電腦密碼DirectoryEntry實(shí)現(xiàn)方法,實(shí)例分析了C#修改及重置電腦密碼的相關(guān)技巧,需要的朋友可以參考下2015-05-05

