给摇杆挂载的脚本:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class mover : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler
{
public RectTransform backrect;//需要在unity中将背景UI拖到里面
public RectTransform selfrect;//需要在unity中将摇杆UI拖到里面
public float radis;//摇杆半径
public Vector2 direction;//摇杆方向
public bool ismoving = false;
public Action<Vector2> MoveEvent;//作为人物移动事件的调用
public void Awake()
{
radis = backrect.sizeDelta.x / 2;
}
public void Update()
{
if (ismoving)
if (MoveEvent != null)
MoveEvent.Invoke(direction);
}
public void OnBeginDrag(PointerEventData eventData)
{
selfrect.anchoredPosition = eventData.position - backrect.anchoredPosition;
ismoving = true;
}
public void OnDrag(PointerEventData eventData)
UGUI实现虚拟摇杆(控制物体前后左右移动)
最新推荐文章于 2023-03-22 07:00:43 发布