Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

8 STRENGTH

유니티 5일차. 스와이프 기능 본문

유니티

유니티 5일차. 스와이프 기능

다민 2022. 7. 21. 03:43

On mouse down이라는 편한 비주얼 스크립트 기능이 있다


UI(User Interface)

사용자가 제품/서비스 사용할 때 보게 되는 부분 (디자인)

UX(User Experience)

사용자가 제품/서비스를 이용하면서 느끼는 부분(경험)

 


TEXTmeshpro : 유니티에서 제공해주는 폰트 전용 도구 하지만 한글이 안들어감 왜냐면 기본으로 지원해주는 폰트가 한글을 지원 안하니까 해결하기 위해서는 외부 폰트를 직접 다운받아서 추가하면 됨 or 구버전인 그냥 text를 쓰면 들어감 근데 text는 구버전이라서 신규 폰트를 쓰면 깨짐 현상이 있을 수 있음

 

https://noonnu.cc/font_page/800


GameDirector

주로 UI에서 작업을 진행하고 관리하는 역할로 쓰이는 이름

 


car swipe 게임 (swipe 기능 구현)

더보기

CarController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CarController : MonoBehaviour
{

    float speed = 0;
    Vector2 startPos;
    //vector2는 2d 프로젝트에서 좌표를 표현하는 데이터
    //vector3는 3d

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("GAME START!");
    }
    
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //speed = 0.2f;

            startPos = Input.mousePosition;
            //input(유니티에서 사용하는 입력에 관련된 것이 모여있음)
        }
        else if (Input.GetMouseButtonUp(0)) //else if는 visual scritping에는 따로 없음
        {
            // MouseButton에 넣어주는 숫자 0은 마우스 왼쪽 버튼
            // MouseButton에 넣어주는 숫자 1은 마우스 오른쪽 버튼
            // MouseButton에 넣어주는 숫자 2는 마우스 휠 버튼

            Vector2 endPos = Input.mousePosition;


            // 스와이프 길이 설정

            float swipeLength = endPos.x - startPos.x;
            // endPos의 x좌표에서 startPos의 x좌표를 뺀 값 = 스와이프 길이
            // 마우스 클릭하고 떼기까지의 작업만큼 길이가 설정된다

            //속도에 대한 초기화
            speed = swipeLength / 500.0f;


            //효과음 기능을 추가합니다.
            GetComponent<AudioSource>().Play();

        }

        transform.Translate(speed, 0, 0);

        speed *= 0.96f;
    }
}

 

더보기

GameDirector.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameDirector : MonoBehaviour
{

    GameObject car; // 유니티에 있는 오브젝트 정의
    GameObject flag;
    GameObject distance; // 거리 표현용 텍스트

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("각 오브젝트를 찾는데 성공했습니다");
        // 오브젝트를 찾는 기능 GameObject.Find("오브젝트의 이름")
        car = GameObject.Find("car");
        flag = GameObject.Find("flag");
        distance = GameObject.Find("distance");
    }

    // Update is called once per frame
    void Update()
    {
        // 길이에 대한 표현 진행 (깃발과 자동차 사이의 거리)
        float length = flag.transform.position.x - car.transform.position.x;

        // 텍스트를 스크립트를 통해서 변경할 수 있도록 작성
        distance.GetComponent<Text>().text = " 목표 지점까지" + length.ToString("F2") + "m";

        if (length < 5)
        {
            distance.GetComponent<Text>().color = Color.red;
        }
        else
        {
            distance.GetComponent<Text>().color = Color.white;
        }

        if(length < 1 && length >-1)
        {
            distance.GetComponent<Text>().text = "YOU WIN!";
        }
        else if (length < -1)
        {
            distance.GetComponent<Text>().text = "GAME OVER";
        }    
        
        //C#문법
        // 1. <데이터형태>가 적혀있는 경우 그 형태를 취급한다는 뜻 (템플릿)
        // 2. ToString() : 값(주로 숫자)을 문자로 바꾸는 기능, F2는 소수점 두자리수를 의미함.

    }
}