using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class JoystickMove : Joystick
{
public RectTransform frame2;
public RectTransform bg;
private Vector2 touchPos;
public override void OnPointerUp(PointerEventData eventData)
{
this.input = Vector2.zero;
this.handle.anchoredPosition = touchPos;
this.frame2.anchoredPosition = touchPos;
this.handle.gameObject.SetActive(false);
this.frame2.gameObject.SetActive(false);
this.SetJoystickColor(false);
this.isDragged = false;
}
public override void OnDrag(PointerEventData eventData)
{
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
this.gameObject.GetComponent<RectTransform>(), eventData.position,
eventData.pressEventCamera, out Vector2 localVector))
{
var moveJoystickDir = localVector - this.touchPos;
if (moveJoystickDir.magnitude < this.handleRange)
{
this.handle.anchoredPosition = localVector;
}
else
{
this.handle.anchoredPosition = this.touchPos + moveJoystickDir.normalized * this.handleRange;
}
this.input = moveJoystickDir;
}
this.SetJoystickColor(true);
}
public override void OnPointerDown(PointerEventData eventData)
{
this.handle.gameObject.SetActive(true);
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
this.gameObject.GetComponent<RectTransform>(), eventData.position,
eventData.pressEventCamera, out Vector2 localVector))
{
this.touchPos = localVector;
this.handle.anchoredPosition = localVector;
this.frame.anchoredPosition = localVector;
this.frame2.anchoredPosition = localVector;
}
this.OnDrag(eventData);
this.isDragged = true;
}
protected override void SetJoystickColor(bool isOnDraged)
{
base.SetJoystickColor(isOnDraged);
Color pointedFrameColor;
Color pointedFrame2Color;
if (isOnDraged)
{
pointedFrameColor = new Color(255, 0, 0, 0.5f);
pointedFrame2Color = new Color(255, 0, 0, 0f);
}
else
{
pointedFrameColor = new Color(255, 255, 255, 0f);
pointedFrame2Color = new Color(255, 255, 255, 0.5f);
}
this.frame.gameObject.GetComponent<Image>().color = pointedFrameColor;
this.frame2.gameObject.GetComponent<Image>().color = pointedFrame2Color;
}
}
조이스틱 범위가 넓어 npc와 상호작용이 불가했던 부분 수정하고 최초에 조이스틱이 존재하는 것을 알리기 위해 핸들을 보여준 이후 두 번째 터치부터는 화면상에 표시되지 않게 하여 보다 UI구성을 보다 깔끔하게 개선하였습니다.
'C# > 문제 해결' 카테고리의 다른 글
Unity) 실제로 충돌하지 않았는데 충돌반응 발생 버그 해결 (0) | 2023.05.18 |
---|---|
파괴된 오브젝트의 클래스에 등록된 이벤트 호출문제 (0) | 2023.05.13 |
오브젝트풀링으로 인한 코루틴 강제 종료관련 문제 해결 (0) | 2023.05.05 |
Unity) [유니티 2D] Rigidbody2D를 활용한 조작 문제 (0) | 2023.03.06 |
Unity) [유니티 2D] 애니메이션 연동 (2) | 2023.03.04 |