它的原理就是利用ScrollRect来限制摇块的摇动区域,但是ScrollRect是矩形的,我们的摇杆的摇动区域应该是个圆形的才对。所以顺着这个思路我们可以写个类继承ScrollRect,自己稍做处理就ok
如下图所示, ScrollCircleMove就是摇杆的背景, 里面的Image就是摇杆块。
代码如下:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ScrollCircleMove : ScrollRect
{
public float m_Speed = 10f;
public GameObject m_Player;
private Animator m_Animator;
private Vector3 m_PlayerDir;//摇杆方向
private bool m_bDrag = false;
protected float m_Radius = 0f;
protected override void Start()
{
base.Start();
// 能移动的半径 = 摇杆的宽 * Dis
m_Radius = (transform as RectTransform).sizeDelta.x * 0.5f;
m_Player = GameObject.FindGameObjectWithTag("Player");
m_Animator = m_Player.GetComponent<Animator>();
m_Speed = 5f;
}
//摇杆控制的核心部分
public override void OnDrag(UnityEngine.EventSystems.PointerEventData eventData)
{
base.OnDrag(eventData);
// 获取摇杆,根据锚点的位置
var contentPostion = this.content.anchoredPosition;
// 判断摇杆的位置 是否大于 半径
if (contentPostion.magnitude > m_Radius)
{
// 设置摇杆最远的位置
contentPostion = contentPostion.normalized * m_Radius;
SetContentAnchoredPosition(contentPostion);
}
// 获取摇杆方向
m_PlayerDir = content.anchoredPosition.normalized;
}
public override void OnBeginDrag(PointerEventData eventData)
{
base.OnBeginDrag(eventData);
m_bDrag = true;
m_Animator.SetBool("Move", true);
}
public override void OnEndDrag(PointerEventData eventData)
{
base.OnEndDrag(eventData);
m_bDrag = false;
m_Animator.SetBool("Move", false);
}
void Update()
{
if (m_bDrag)
{
m_Player.GetComponent<Rigidbody>().MovePosition(m_Player.transform.position + new Vector3(m_PlayerDir.x, 0, m_PlayerDir.y) * Time.deltaTime * m_Speed);
}
}
}