유니티/수업 내용
[UGUI] Joystick
hyunjin-dev-log
2022. 1. 18. 19:20
더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class MyJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
public GameObject decoTopRight;
public GameObject decoTopLeft;
public GameObject decoBottomLeft;
public GameObject decoBottomRight;
private VariableJoystick joystick;
public UnityAction<Vector2> OnDragAction;
public UnityAction OnDownAction;
public UnityAction OnUpAction;
void Start()
{
this.joystick = this.GetComponent<VariableJoystick>();
this.joystick.SnapX = true;
this.joystick.SnapY = true;
this.AllDecoDisable();
}
public void OnDrag(PointerEventData eventData)
{
//Debug.Log(this.joystick.Direction);
var dir = this.joystick.Direction;
var dirX = dir.x;
var dirY = dir.y;
if (dirX == 1 && dirY == 1)
{
this.AllDecoDisable();
this.decoTopRight.SetActive(true);
}
else if (dirX == -1 && dirY == 1)
{
this.AllDecoDisable();
this.decoTopLeft.SetActive(true);
}
else if (dirX == -1 && dirY == -1)
{
this.AllDecoDisable();
this.decoBottomLeft.SetActive(true);
}
else if (dirX == 1 && dirY == -1)
{
this.AllDecoDisable();
this.decoBottomRight.SetActive(true);
}
this.OnDragAction(this.joystick.Direction);
}
private void AllDecoDisable()
{
this.decoTopRight.SetActive(false);
this.decoTopLeft.SetActive(false);
this.decoBottomRight.SetActive(false);
this.decoBottomLeft.SetActive(false);
}
public void OnPointerUp(PointerEventData eventData)
{
this.AllDecoDisable();
this.OnUpAction();
}
public void OnPointerDown(PointerEventData eventData)
{
this.OnDownAction();
}
}
더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestJoystick : MonoBehaviour
{
public MyJoystick joystick;
void Start()
{
this.joystick.OnDragAction = (dir) =>
{
Debug.Log(dir);
};
this.joystick.OnDownAction = () =>
{
Debug.Log("down");
};
this.joystick.OnUpAction = () =>
{
Debug.Log("up");
};
}
}