Unity/유니티 기본

[4.30] LobbyScene R&D

ljw4104 2021. 4. 30. 17:08

1. 버튼을 클릭하면 UI 상의 캐릭터가 일정한 각도로 방향에 맞게 회전

결과물

using UnityEngine;
using UnityEngine.UI;

public class LobbyScene : MonoBehaviour
{
    public GameObject obj;
    public Button btnL;
    public Button btnR;
    // Start is called before the first frame update
    void Start()
    {
        this.btnL.onClick.AddListener(() =>
        {
            Debug.Log("L Button pressed");
            this.obj.transform.Rotate(0, 30, 0);
        });

        this.btnR.onClick.AddListener(() =>
        {
            Debug.Log("R Button pressed");
            this.obj.transform.Rotate(0, -30, 0);
        });
    }
}

 

 

 

2. 드래그 하는 방향으로 캐릭터를 회전

IDragHandler Interface를 이용했다.

 

LobbyScene.cs

using UnityEngine;
using UnityEngine.UI;

public class LobbyScene : MonoBehaviour
{
    public GameObject obj;
    public Button btnL;
    public Button btnR;
    public UIDragTest test;

    // Start is called before the first frame update
    void Start()
    {
        this.btnL.onClick.AddListener(() =>
        {
            Debug.Log("L Button pressed");
            this.obj.transform.Rotate(0, 30, 0);
        });

        this.btnR.onClick.AddListener(() =>
        {
            Debug.Log("R Button pressed");
            this.obj.transform.Rotate(0, -30, 0);
        });

        this.test.onDragX = (delta) =>
        {
            this.obj.transform.Rotate(0, delta * -1, 0);
        };
    }
}

 

UIDragTest.cs

using UnityEngine;
using UnityEngine.EventSystems;

public class UIDragTest : MonoBehaviour, IDragHandler
{
    public System.Action<float> onDragX;
    public void OnDrag(PointerEventData eventData)
    {
        this.onDragX(eventData.delta.x);
    }
}