85 lines
2.1 KiB
C#
Raw Permalink Normal View History

2025-05-18 01:04:31 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ArrowJoystick : Joystick
{
public float moveThreshold { get { return _moveThreshold; } set { _moveThreshold = Mathf.Abs(value); } }
[SerializeField] private float _moveThreshold = 1;
[SerializeField] private RectTransform arrowHandle1 = null;
[SerializeField] private RectTransform arrowHandle2 = null;
[SerializeField] private RectTransform arrowMask = null;
private Vector2 _fixedPosition = Vector2.zero;
protected override void Start()
{
base.Start();
_fixedPosition = background.anchoredPosition;
handle.gameObject.SetActive(false);
}
public override void OnPointerDown(PointerEventData eventData)
{
background.anchoredPosition = ScreenPointToAnchoredPosition(eventData.position);
//background.gameObject.SetActive(true);
base.OnPointerDown(eventData);
}
public override void OnPointerUp(PointerEventData eventData)
{
//background.anchoredPosition = _fixedPosition;
//background.gameObject.SetActive(false);
base.OnPointerUp(eventData);
}
protected override void HandleInput(float magnitude, Vector2 normalised, Vector2 radius, Camera cam)
{
//if (magnitude > _moveThreshold)
//{
// Vector2 difference = normalised * (magnitude - _moveThreshold) * radius;
// background.anchoredPosition += difference;
//}
var s1 = arrowHandle1.sizeDelta;
s1.x = handle.anchoredPosition.magnitude;
arrowHandle1.sizeDelta = s1;
arrowHandle2.sizeDelta = s1;
base.HandleInput(magnitude, normalised, radius, cam);
}
public float chargeProgress
{
set
{
var sd = arrowHandle1.sizeDelta;
arrowMask.sizeDelta = new Vector2(sd.x * value, sd.y);
}
}
private float _handleTimer;
protected override void OnShortTap()
{
_handleTimer = 0.1f;
handle.gameObject.SetActive(true);
handle.GetComponent<Animator>().Play("handle_click", -1, 0);
}
private void LateUpdate()
{
if (_handleTimer > 0f)
{
_handleTimer -= Time.unscaledDeltaTime;
if (_handleTimer <= 0f)
{
//handle.gameObject.SetActive(false);
}
}
}
}