ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Unity][2D] 드래그해서 타일 깔기
    프로그래밍/Unity 2021. 11. 21. 18:08
    728x90

    우선 드래그해서 타일을 깔기 위해 이전에 Grid 크기가 맞지 않았던 현상을 고치기 위해 Grid의 Cell Size를 임의로 맞추어 주었다. 필요하면 Grid Scale도 바꾸어주면 된다.

    이전과 다른 점은 Canvas에서 hierarchy상 버튼을 이름을 찾는게 아니라 Canvas에 활성화 버튼을 지정해주고 AddListener를 사용해서 버튼의 활성화 상태만 저장해주었다.

    이후 활성화 상태를 이용하여 Update Function 상에서 타일을 세팅하게 해주었다.

     

    아직 마우스를 위에서 아래로 드래그할 때만 적용해 두어서 수정이 필요한 상태!

    [재생시 화면]

     

    누른 상태 자체는 GetMouseButtonDown를 써야 깔끔하다고 서칭했었는데 드래그를 할 때는 GetMouseButton(0)이 유용했다.

    GetMouseButtonDown으로 돌아가게 만들려니 시간이 오래걸렸다.

     

    다음은 마우스 어느 방향이든지 타일을 채울 수 있도록 하는 것이 목표다.

     

    [코드]

    // ======================================

    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;


        Vector3Int current_pos = new Vector3Int(0, 0, 0);
        Vector3Int begin_pos = new Vector3Int(0, 0, 0);  // <= 처음 누르는 mouse position을 알기 위함
        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) < == 처음 누르는 mouse position을 알기 위함
                    {
                        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;
                    }


                    // 마우스가 움직이는 현재 mouse position을 알기 위함
                    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;
                    
                    for (int i = begin_pos.x; i <= current_pos.x; i++) //<= 처음 position에서 지금 position까지의 가로 세로 타일 모두 다 채움
                    {
                        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);
                        }
                            
                    }
                }
                if (Input.GetMouseButtonUp(0)) // <= 마우스를 뗄 때 begin 값은 다시 True로
                    begin = true;

            }

            else
            {
                button2.GetComponentInChildren<Text>().text = "button1 deactivated";
            }
        }

        private void UI1_Activity() // <== 버튼을 누를 때 마다 타일을 세팅하는 작업을 activate/deactivate 하기 위함
        {
            UI1_active = !UI1_active;
        }
    }

     

    // ======================================

    728x90
Designed by Tistory.