思路来源,参考文章:https://www.pianshen.com/article/7779917253/
EventTriggerListener.cs
using UnityEngine;
using UnityEngine.EventSystems;
public class EventTriggerListener : EventTrigger
{
public delegate void VoidDelegate(GameObject go);
public delegate void VectorDelegate(GameObject go, Vector2 delta);
public VoidDelegate onClick;
public VoidDelegate onDown;
public VoidDelegate onEnter;
public VoidDelegate onExit;
public VoidDelegate onUp;
public VoidDelegate onSelect;
public VoidDelegate onUpdateSelect;
public VectorDelegate onDrag;
public VoidDelegate onDragOut;
public static EventTriggerListener Get(GameObject go)
{
if (go == null)
{
Debug.LogError("EventTriggerListener_go_is_NULL");
return null;
}
else
{
EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
if (listener == null) listener = go.AddComponent<EventTriggerListener>();
return listener;
}
}
public override void OnDrag(PointerEventData eventData)
{
onDrag?.Invoke(gameObject, eventData.delta);
}
public override void OnEndDrag(PointerEventData eventData)
{
onDragOut?.Invoke(gameObject);
}
public override void OnPointerClick(PointerEventData eventData)
{
onClick?.Invoke(gameObject);
}
public override void OnPointerDown(PointerEventData eventData)
{
onDown?.Invoke(gameObject);
}
public override void OnPointerEnter(PointerEventData eventData)
{
onEnter?.Invoke(gameObject);
}
public override void OnPointerExit(PointerEventData eventData)
{
onExit?.Invoke(gameObject);
}
public override void OnPointerUp(PointerEventData eventData)
{
onUp?.Invoke(gameObject);
}
public override void OnSelect(BaseEventData eventData)
{
onSelect?.Invoke(gameObject);
}
public override void OnUpdateSelected(BaseEventData eventData)
{
onUpdateSelect?.Invoke(gameObject);
}
}
JoystickControl.cs
using UnityEngine;
public class JoystickControl : MonoBehaviour
{
public delegate void GetAxis(Vector2 v2);
public event GetAxis GetAxisHandle;
private Transform handleTrans;
private float dis;
private Vector3 Origin;
private bool isDrag = false;
private Vector2 axisV2;
private Vector3 MovePosiNorm;
private float ActiveMoveDistance = 1;
[SerializeField]
private float MoveMaxDistance = 45;//最大拖动距离
public Vector2 AxisV2
{
get { return axisV2; }
set
{
if (axisV2 != value)
{
GetAxisHandle(value);
axisV2 = value;
}
}
}
void Awake()
{
handleTrans = transform.GetChild(0);
EventTriggerListener.Get(handleTrans.gameObject).onDown = OnMoveStart;
EventTriggerListener.Get(handleTrans.gameObject).onDrag = OnDrag;
EventTriggerListener.Get(handleTrans.gameObject).onDragOut = OnDragOut;
}
void Start()
{
Origin = handleTrans.localPosition;
}
void Update()
{
dis = Vector3.Distance(handleTrans.localPosition, Origin);//拖动距离,这不是最大的拖动距离,是根据触摸位置算出来的
if (dis >= MoveMaxDistance)//如果大于可拖动的最大距离
{
Vector3 vec = Origin + (handleTrans.localPosition - Origin) * MoveMaxDistance / dis; //求圆上的一点:(目标点-原点) * 半径/原点到目标点的距离
handleTrans.localPosition = vec;
}
if (Vector3.Distance(handleTrans.localPosition, Origin) > ActiveMoveDistance) //距离大于有效移动距离
{
MovePosiNorm = (handleTrans.localPosition - Origin).normalized;
AxisV2 = new Vector2(MovePosiNorm.x, MovePosiNorm.y);
}
else
{
MovePosiNorm = Vector3.zero;
AxisV2 = Vector2.zero;
}
}
void OnDrag(GameObject go, Vector2 delta)
{
if (!isDrag)
{
isDrag = true;
}
handleTrans.localPosition += new Vector3(delta.x, delta.y, 0);
}
void OnDragOut(GameObject go)
{
isDrag = false;
handleTrans.localPosition = Origin;
}
void OnMoveStart(GameObject go)
{
Debug.Log("Move Start");
}
}
PlayerMoveControl.cs(人物移动)
using UnityEngine;
public class PlayerMoveControl : MonoBehaviour
{
public Transform joystickObj;
private JoystickControl joystickCc;
public float moveSpeed = 10;
private Vector2 v2;
private void Start()
{
joystickCc = joystickObj.GetComponent<JoystickControl>();
joystickCc.GetAxisHandle += (Vector2 v2) =>
{
this.v2 = v2;
Debug.Log(v2);
};
}
void Update()
{
//Debug.Log(v2 + "?????:" + joystickCc.AxisV2);//两种方法 1: += 2:joystickCc.AxisV2
Vector3 nextPos = new Vector3(v2.x, 0, -v2.y) * Time.deltaTime;
transform.LookAt(transform.position + nextPos);
transform.position += nextPos;
//float v = Input.GetAxis("Horizontal");
//float h = Input.GetAxis("Vertical");
//Debug.Log(v + " " + h);
//Vector3 nextPos = new Vector3(h, 0, -v) * Time.deltaTime;
//transform.LookAt(nextPos + transform.position);
//transform.position += nextPos;
}
}