-
[Unity][2D] 드래그 해서 타일 깔기프로그래밍/Unity 2021. 11. 27. 17:58728x90
이전 포스트에 이어서 모든 방향으로 타일 깔기 도전
switch 조건문을 이용해서 마우스 방향에 따른 Mouse direction 값을 조정해주는 방법으로 진행하였다.
<실행 화면>
<실행 코드>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Tilemaps;
using UnityEngine.EventSystems;
public class button_Event : MonoBehaviour
{
private bool UI1_active = false;
private bool begin = true;
public Button button2;
Vector2 current_MousePosition;
Vector2 begin_MousePosition;
Camera Camera;
public Tilemap Tilemap;
[SerializeField]
private TileBase target_tile;
private int mouse_direction = 0; // Mouse 동작 방향을 구분하기 위함
Vector3Int current_pos = new Vector3Int(0, 0, 0);
Vector3Int begin_pos = new Vector3Int(0, 0, 0);
Vector3Int temp_pos = new Vector3Int(0, 0, 0);
// Start is called before the first frame update
void Start()
{
Camera = GameObject.Find("Main Camera").GetComponent<Camera>();
button2.onClick.AddListener(UI1_Activity);
}
void Update()
{
if (UI1_active == true)
{
button2.GetComponentInChildren<Text>().text = "button1 activated";
if (Input.GetMouseButton(0))
{
if (begin == true)
{
begin_MousePosition = Input.mousePosition;
begin_MousePosition = Camera.ScreenToWorldPoint(begin_MousePosition);
begin_pos.x = Tilemap.WorldToCell(begin_MousePosition).x;
begin_pos.y = Tilemap.WorldToCell(begin_MousePosition).y;
begin = false;
}
current_MousePosition = Input.mousePosition;
current_MousePosition = Camera.ScreenToWorldPoint(current_MousePosition);
current_pos.x = Tilemap.WorldToCell(current_MousePosition).x;
current_pos.y = Tilemap.WorldToCell(current_MousePosition).y;
// 마우스 방향에 따라 mouse_direction 값 정해주기
if (begin_pos.x <= current_pos.x && begin_pos.y >= current_pos.y)
mouse_direction = 0; // right down
else if (begin_pos.x <= current_pos.x && begin_pos.y < current_pos.y)
mouse_direction = 1; // right up
else if (begin_pos.x > current_pos.x && begin_pos.y >= current_pos.y)
mouse_direction = 2; // left down
else
mouse_direction = 3; // left up
switch (mouse_direction)
{
case 0:
for (int i = begin_pos.x; i <= current_pos.x; i++)
{
for (int j = begin_pos.y; j >= current_pos.y; j--)
{
temp_pos.x = i;
temp_pos.y = j;
Tilemap.SetTile(temp_pos, target_tile);
}
}
break;
case 1:
for (int i = begin_pos.x; i <= current_pos.x; i++)
{
for (int j = begin_pos.y; j <= current_pos.y; j++)
{
temp_pos.x = i;
temp_pos.y = j;
Tilemap.SetTile(temp_pos, target_tile);
}
}
break;
case 2:
for (int i = begin_pos.x; i >= current_pos.x; i--)
{
for (int j = begin_pos.y; j >= current_pos.y; j--)
{
temp_pos.x = i;
temp_pos.y = j;
Tilemap.SetTile(temp_pos, target_tile);
}
}
break;
case 3:
for (int i = begin_pos.x; i >= current_pos.x; i--)
{
for (int j = begin_pos.y; j <= current_pos.y; j++)
{
temp_pos.x = i;
temp_pos.y = j;
Tilemap.SetTile(temp_pos, target_tile);
}
}
break;
}
}
if (Input.GetMouseButtonUp(0))
begin = true;
}
else
{
button2.GetComponentInChildren<Text>().text = "button1 deactivated";
}
}
private void UI1_Activity()
{
UI1_active = !UI1_active;
}
}728x90'프로그래밍 > Unity' 카테고리의 다른 글
Unity C# Random Range 사용법 (int/float) (0) 2023.02.05 [Unity][2D] 유니티 스프라이트 애니메이션 넣기 (0) 2021.12.12 [Unity][2D] 드래그해서 타일 깔기 (0) 2021.11.21 [Unity][2D] EventSystems를 활용한 UI Control (0) 2021.11.07 [Unity][2D] 카메라 줌 인/줌 아웃 (0) 2021.10.31