[Update] 휴대폰 조이스틱 연결 및 스크립트 변경

1. 휴대폰에서 실시가능하도록 조이스틱 및 버튼 입력
2. 콜라이더 및 트리거 구조 변경
3. 로테이션 단계이상함 감지 - 수정 예정
This commit is contained in:
jongjae0305
2026-04-21 17:45:51 +09:00
parent 15bb71750b
commit 6c84f865d8
209 changed files with 8639 additions and 2654 deletions
@@ -0,0 +1,123 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.OnScreen;
using UnityEngine.UI;
public enum VirtualJoystickType { Fixed, Floating }
public class VirtualJoystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
[SerializeField] private RectTransform centerArea = null;
[SerializeField] private RectTransform handle = null;
[InputControl(layout = "Vector2")]
[SerializeField] private string stickControlPath;
[SerializeField] private float movementRange = 100f;
protected VirtualJoystickType joystickType = VirtualJoystickType.Fixed;
protected bool _hideOnPointerUp = false;
protected bool _centralizeOnPointerUp = true;
private Canvas canvas;
protected RectTransform baseRect = null;
protected OnScreenStick handleStickController = null;
protected CanvasGroup bgCanvasGroup = null;
protected Vector2 initialPosition = Vector2.zero;
protected virtual void Awake()
{
canvas = GetComponentInParent<Canvas>();
baseRect = GetComponent<RectTransform>();
bgCanvasGroup = centerArea.GetComponent<CanvasGroup>();
handleStickController = handle.gameObject.AddComponent<OnScreenStick>();
handleStickController.movementRange = movementRange;
handleStickController.controlPath = stickControlPath;
Vector2 center = new Vector2(0.5f, 0.5f);
centerArea.pivot = center;
handle.anchorMin = center;
handle.anchorMax = center;
handle.pivot = center;
handle.anchoredPosition = Vector2.zero;
initialPosition = centerArea.anchoredPosition;
if (joystickType == VirtualJoystickType.Fixed)
{
centerArea.anchoredPosition = initialPosition;
bgCanvasGroup.alpha = 1;
}
else if (joystickType == VirtualJoystickType.Floating)
{
if (_hideOnPointerUp) bgCanvasGroup.alpha = 0;
else bgCanvasGroup.alpha = 1;
}
}
public void OnPointerDown(PointerEventData eventData)
{
PointerEventData constructedEventData = new PointerEventData(EventSystem.current);
constructedEventData.position = handle.position;
handleStickController.OnPointerDown(constructedEventData);
if (joystickType == VirtualJoystickType.Floating)
{
centerArea.anchoredPosition = GetAnchoredPosition(eventData.position);
if (_hideOnPointerUp)
bgCanvasGroup.alpha = 1;
}
}
public void OnDrag(PointerEventData eventData)
{
if (joystickType == VirtualJoystickType.Floating)
{
handleStickController.OnDrag(eventData);
}
}
public void OnPointerUp(PointerEventData eventData)
{
if (joystickType == VirtualJoystickType.Floating)
{
if (_centralizeOnPointerUp)
centerArea.anchoredPosition = initialPosition;
if (_hideOnPointerUp) bgCanvasGroup.alpha = 0;
else bgCanvasGroup.alpha = 1;
}
PointerEventData constructedEventData = new PointerEventData(EventSystem.current);
constructedEventData.position = Vector2.zero;
handleStickController.OnPointerUp(constructedEventData);
}
protected Vector2 GetAnchoredPosition(Vector2 screenPosition)
{
Camera cam = (canvas.renderMode == RenderMode.ScreenSpaceCamera) ? canvas.worldCamera : null;
Vector2 localPoint = Vector2.zero;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(baseRect, screenPosition, cam, out localPoint))
{
Vector2 pivotOffset = baseRect.pivot * baseRect.sizeDelta;
return localPoint - (centerArea.anchorMax * baseRect.sizeDelta) + pivotOffset;
}
return Vector2.zero;
}
public Vector2 GetInput()
{
if (handle != null && movementRange != 0)
{
return handle.anchoredPosition / movementRange;
}
return Vector2.zero;
}
}