기존에 찾았던 EventTrigger를 이용하는 게 아닌, 다른 방식도 찾아보기로 했다
유니티 드래그 앤 드랍 방식 구현해보자
Drag & Drop ............. 어떻게 구현하는거지? 그래서 유니티 공홈에 들어가서 검색했다. EventTrigger.OnDrag (링크) Called by the EventSystem every time the pointer is moved during dragging. See Eve..
27th.tistory.com
- IPointerDownHandler (링크)
- Interface to implement if you wish to receive OnPointerDown callbacks.
- Detects ongoing mouse clicks until release of the mouse button. Use IPointerUpHandler to handle the release of the mouse button
//Attach this script to the GameObject you would like to have mouse clicks detected on
//This script outputs a message to the Console when a click is currently detected or when it is released on the GameObject with this script attached
using UnityEngine;
using UnityEngine.EventSystems;
public class Example : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
//Detect current clicks on the GameObject (the one with the script attached)
public void OnPointerDown(PointerEventData pointerEventData)
{
//Output the name of the GameObject that is being clicked
Debug.Log(name + "Game Object Click in Progress");
}
//Detect if clicks are no longer registering
public void OnPointerUp(PointerEventData pointerEventData)
{
Debug.Log(name + "No longer being clicked");
}
}
그리고 유튜브를 보고 해 보았다!
영상을 따라 하며 스크립트를 작성해주었다.
기존의 방식은 Drag.cs에, 얘는 Drag2.cs이다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Drag2 : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
{
[SerializeField] private Canvas canvas;
private RectTransform rectTransform;
private CanvasGroup canvasGroup;
private void Awake()
{
rectTransform = GetComponent<RectTransform>();
canvasGroup = GetComponent<CanvasGroup>();
}
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("OnBeginDrag");
canvasGroup.alpha = .6f;
canvasGroup.blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
Debug.Log("OnDrag");
rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("OnEndDrag");
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("OnPointerDown");
}
}
드래그한 후 drop 했을 때, 캐릭터 오브젝트의 위치에 고정하는 스크립트를 따로 작성해주었다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class FixItem : MonoBehaviour, IDropHandler
{
public void OnDrop(PointerEventData eventData)
{
Debug.Log("OnDrop");
if (eventData.pointerDrag != null)
{
eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
}
}
}
그리고 테스트용으로 각각 할당하고 플레이해보자
사각형을 움직여서 캡슐 위에 둘 거다!
너무 슬픈 소식....
영상을 따라 했는데도 FixItem.cs에 작성한 OnDrop이 뜨질 않는다 ㅠ
왜 안되지.......................?
싶어서 Collider를 추가해보았더니.. 된다!!!!!
저번부터 안 되는 건 콜라이더를 추가하면 되네........
왜지?
예시들은 추가 안 하는데 왜 나는 추가해야 되지?? 궁금해 미챠~~
공홈에 들어가서 다시 읽어보았더니, 콜라이더는 모든 충돌기의 기본 클래스라고 한다.
그래서 그런 걸까..? 싶기도 함...
아 그리고 하면서 알아낸 건, Canvas에서 해야 한다는 것...?
저번에 혼자 옷 입히기 연구해본 건 캔버스에서도 하고 그냥 그 밖에서도 했는데
다시 구현하려니까 안되기도 하더라고..
(이 방법은 캔버스를 참조하니 더더욱 캔버스 안에서 해야 하고)
우선 이 방법의 드래그 앤 드롭은 성공!
팀원분께 받은 우리 캐릭터와 옷 이미지에 입혀봤는데,...
음 이 드래그앤드롭 방식의 드롭되는 부분이, 저 캡슐의 중앙에 오도록 하는 거 같은데.
그래서 팀원분께 받은 이미지도 사이즈를 일부러 맞추신 듯하다. 모든 이미지가 게임 해상도 크기로 되어있다.
이대로 진행하면 중앙이 맞춰지기 편했으나,
이미지 크기가 크다 보니 게임을 플레이했을 때 선택이 힘들다..!
이건 어떻게 해봐야 할지.. 고민해봐야겠다!!
오늘은 이대로 끝내자.
아 그리고 드래그하며 조정되는 알파 값은 지워버렸다.
그 부분을 빼고 스크립트 작성을 했는데, 안되길래 다 지우고 전부 다시 적었거든....
힘 내보자!!!!!!!!!!!!!!!!!!!!!!
+
드래그 앤 드롭 다른 유튜브!
'Project > [Unity] 옷입히기 게임' 카테고리의 다른 글
유니티 스크린샷 찍어보자! png파일로 저장! (0) | 2022.04.27 |
---|---|
화살표를 이용해서 아이템을 바꿔보자 + 색상 변경 고민 (즐거운 뒤죽박죽~~) (0) | 2022.04.10 |
유니티 옷 입히기 입은 옷 다 삭제하는 버튼 만들기 (씬 리로드) (0) | 2022.04.07 |
유니티 드래그 앤 드롭을 통해 지정된 위치에 아이템을 착용하도록 하자 (0) | 2022.04.06 |
유니티 드래그 앤 드롭 방식 구현해보자 (0) | 2022.04.05 |
댓글