You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.0 KiB
76 lines
2.0 KiB
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class VirtualPad : Singleton<VirtualPad>, IDragHandler, IPointerUpHandler, IPointerDownHandler
|
|
{
|
|
[SerializeField]
|
|
private Image Controller, stick;
|
|
|
|
private Canvas canvas;
|
|
private Camera uiCamera;
|
|
private RectTransform canvasRect;
|
|
|
|
private bool istouch = false;
|
|
private bool isDrag = false;
|
|
|
|
private Vector2 saveVec;
|
|
private Vector2 moveVec;
|
|
|
|
private void Awake()
|
|
{
|
|
_instance = this;
|
|
canvas = transform.parent.GetComponent<Canvas>();
|
|
canvasRect = canvas.GetComponent<RectTransform>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (isDrag)
|
|
{
|
|
BattleMgr.Instance.MoveToDir(moveVec);
|
|
}
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
if(BattleMgr.Instance.CurrentBattleType == BattleMgr.BattleType.Pvp) return;
|
|
|
|
Controller.enabled = true;
|
|
stick.enabled = true;
|
|
isDrag = true;
|
|
|
|
Vector2 dragVec;
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, eventData.position, uiCamera, out dragVec);
|
|
moveVec = (dragVec - saveVec).normalized;
|
|
float dragDist = Mathf.Min(Vector2.Distance(saveVec, dragVec), 82f);
|
|
stick.rectTransform.anchoredPosition = saveVec + (moveVec * dragDist);
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
istouch = false;
|
|
isDrag = false;
|
|
moveVec = Vector2.zero;
|
|
Controller.enabled = false;
|
|
stick.enabled = false;
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, eventData.position, uiCamera, out saveVec);
|
|
Controller.rectTransform.anchoredPosition = saveVec;
|
|
stick.rectTransform.anchoredPosition = saveVec;
|
|
istouch = true;
|
|
}
|
|
|
|
public static bool isTouchScreen()
|
|
{
|
|
return Instance.istouch;
|
|
}
|
|
|
|
public static void setCamera(Camera uicam)
|
|
{
|
|
Instance.uiCamera = uicam;
|
|
}
|
|
}
|