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.
86 lines
2.3 KiB
86 lines
2.3 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 float fdt;
|
|
|
|
private Vector2 saveVec;
|
|
private Vector2 resultVec;
|
|
private Vector2 MoveVec;
|
|
private Vector2 DragVec;
|
|
|
|
private void Awake()
|
|
{
|
|
_instance = this;
|
|
canvas = transform.parent.GetComponent<Canvas>();
|
|
canvasRect = canvas.GetComponent<RectTransform>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (isDrag)
|
|
{
|
|
BattleMgr.MoveToDir(MoveVec);
|
|
}
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
if(BattleMgr.CurrentBattleType != BattleMgr.BattleType.Pvp)
|
|
{
|
|
if (!isDrag)
|
|
{
|
|
Controller.enabled = true;
|
|
stick.enabled = true;
|
|
}
|
|
isDrag = true;
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, eventData.position, uiCamera, out DragVec);
|
|
resultVec = DragVec - saveVec;
|
|
MoveVec = resultVec.normalized;
|
|
fdt = Vector2.Distance(saveVec, DragVec) < 82f ? Vector2.Distance(saveVec, DragVec) : 82f;
|
|
stick.rectTransform.anchoredPosition = saveVec + (MoveVec * fdt);
|
|
}
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
istouch = false;
|
|
isDrag = false;
|
|
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;
|
|
}
|
|
|
|
public static Vector2 GetPointDirection()
|
|
{
|
|
return new Vector2(Instance.resultVec.x >= 0 ? 1 : -1, Instance.resultVec.y >= 0 ? 1 : -1);
|
|
}
|
|
}
|