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

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

ps:將子物體的錨點(diǎn)設(shè)置為居中
2、在父物體上寫(xiě)個(gè)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);
//或者利用子物體和父物體的距離判斷是否超過(guò)最大距離,當(dāng)距離大于等于最大的距離時(shí)候,
//計(jì)算父物體和子物體的向量,然后利用向量*最大距離來(lái)限制拖拽距離
//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é)束拖動(dòng),要將物體歸0,為了加一點(diǎn)緩沖效果
//(1)可以使用dotween等補(bǔ)間動(dòng)畫(huà)插件,會(huì)減少很多
//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);
}
}
加個(gè)使用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);
//或者利用子物體和父物體的距離判斷是否超過(guò)最大距離,當(dāng)距離大于等于最大的距離時(shí)候,
//計(jì)算父物體和子物體的向量,然后利用向量*最大距離來(lái)限制拖拽距離
//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é)束拖動(dòng),要將物體歸0,為了加一點(diǎn)緩沖效果
//(1)可以使用dotween等補(bǔ)間動(dòng)畫(huà)插件,會(huì)減少很多
rectTransform.DoAnchoredPos(Vector2.zero,0.5f).OnUpdate(GetHV);
}
private void GetHV()
{
h = childRectTrans.anchoredPosition.x / maxDis;
v = childRectTrans.anchoredPosition.y / maxDis;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- unity實(shí)現(xiàn)手機(jī)端搖桿控制人物移動(dòng)
- Unity實(shí)現(xiàn)簡(jiǎn)單的虛擬搖桿
- unity實(shí)現(xiàn)虛擬搖桿控制Virtual Joystick
- Unity UGUI通過(guò)搖桿控制角色移動(dòng)
- Unity虛擬搖桿的實(shí)現(xiàn)方法
- unity實(shí)現(xiàn)手游虛擬搖桿
- Unity3D基于UGUI實(shí)現(xiàn)虛擬搖桿
- Unity3D使用UGUI開(kāi)發(fā)原生虛擬搖桿
- Unity實(shí)現(xiàn)簡(jiǎn)單虛擬搖桿
- Unity使用ScrollRect制作搖桿
相關(guān)文章
剖析設(shè)計(jì)模式編程中C#對(duì)于組合模式的運(yùn)用
這篇文章主要介紹了設(shè)計(jì)模式編程中C#對(duì)于組合模式的運(yùn)用,理論上來(lái)說(shuō)組合模式包含抽象構(gòu)件、樹(shù)葉構(gòu)件和樹(shù)枝構(gòu)件三個(gè)角色,需要的朋友可以參考下2016-02-02
WPF設(shè)置窗體可以使用鼠標(biāo)拖動(dòng)大小的方法
這篇文章主要介紹了WPF設(shè)置窗體可以使用鼠標(biāo)拖動(dòng)大小的方法,涉及針對(duì)窗口的操作與設(shè)置技巧,具有很好的借鑒價(jià)值,需要的朋友可以參考下2014-11-11
c# 使用計(jì)時(shí)器和觀察者模式實(shí)現(xiàn)報(bào)警推送需求
這篇文章主要介紹了c# 使用計(jì)時(shí)器和觀察者模式實(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

