Files
WildRoot/Assets/VirtualButtonsForUnity/VirtualButtonsForUnity/Scripts/VirtualJoystick.cs
T
jongjae0305 8298b2559c [Update] 게임형식 및 오류수정
1. 게임형태 변경
  - Game스텝에서 기존 1단계, 2단계 수정(회전만 가능하게).
  - ShootButton을 만들어 플레이어도 총알을 쏠수 있도록 수정.
  - Bullet, BulletSpawner, PlayerController를 수정
  - SpawnZone을 생성하여 기존 고정이던 스포너를 랜덤으로 생성.

2. 오류 수정
 - 모바일 버전 시 플레이어가 한방향만 보던 형상 수정.
 - 펜스에서 플레이어 캐릭터가 닿으면 회전하던 형상 수정.
2026-04-29 09:22:08 +09:00

137 lines
4.3 KiB
C#

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;
private Vector2 lastDirection = 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)
{
Vector2 currentInput = handle.anchoredPosition / movementRange;
if (currentInput.magnitude > 0.1f)
{
lastDirection = currentInput;
}
return currentInput;
}
return Vector2.zero;
}
public Vector2 GetLastDirection()
{
return lastDirection;
}
}