본문 바로가기
유니티 최고/유니티 구현

간단한 유니티(Unity) 모바일 게임 만들기

by Lee_story_.. 2022. 9. 1.
728x90

 

이번에는 간단한 기능들을 조합해서 모바일 버전의 게임을 한번 만들어 보겠습니다.

 

 

 

 

 

 

 

 

공을 통해 플레이 하는 게임으로 위처럼 설정해주고 

아래처럼 코드를 작성해줍시다.

 

아래의 코드는 크게

1. 움직임

2.버튼에 들어갈 함수

3.게임 새로하기함수

4. 게임 종료 함수

 

등을 넣어 주었습니다.

 

원래 3,4 번 의 기본적인 함수들은 게임매니저를 만들어 관리해줘야 할것 같았지만

큰 프로젝트가 아니기에 여기서 관리하였습니다.

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CharacterMove : MonoBehaviour
{
    bool left;
    bool right;
    

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.A))//왼
        {
            GetComponent<Rigidbody>().AddForce(Vector3.right * 400);
        }

        if (Input.GetKeyDown(KeyCode.D))//오
        {
            GetComponent<Rigidbody>().AddForce(Vector3.left * 400);
        }
    
    }
   
    public void left_btn()// 버튼부분
    {
        GetComponent<Rigidbody>().AddForce(Vector3.right * 400);
    }

    public void rignt_btn()
    {
        GetComponent<Rigidbody>().AddForce(Vector3.left * 400);
    }





}

 

 

    public void scene_f()
    {

        SceneManager.LoadScene("main");
        Time.timeScale = 1;
    }

    public void fin()
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#else
        Application.Quit(); // 어플리케이션 종료
#endif

    }

 

 

그다음은 랜덤위치에서 생성되는 장애물을 만들어 보았습니다.

 

 

먼저 큐브를 만들어 그걸 prefab으로 만들어 준 후 그안에 아래와 같이 추가했습니다.

 

 

move 스크립에는 장애물의 이동속도와 생성시 매터리얼을 랜덤으로 변경하여 생성하도록,

그리고 캐릭터와 충돌시삭제, 화면밖의 벽과 충돌하면 삭제 등을 구현해주었습니다.

 

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

public class move : MonoBehaviour
{
    public float delta = 0.1f;
    
    public Material[] mat;
    // Start is called before the first frame update
    void Start()
    {
        int idx=Random.Range(0, mat.Length);    
        GetComponent<MeshRenderer>().material = mat[idx];
    }

    // Update is called once per frame
    void Update()
    {

        float newXPosition = transform.localPosition.z + delta;//장애물 이동
        transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, newXPosition);
    }

    private void OnTriggerEnter(Collider other) // 충돌 감지
    {
        
        if (other.gameObject.name == "Ball")
        {
            Time.timeScale = 0;
            Destroy(gameObject);
        }

        Destroy(gameObject);
    }
}

 

그리고 

spawn 스크립에서는 게임 시작 2초후 부터 SpawnEnemy함수가 실행하도록,

그리고 그안에서 3초후 2초마다 생성되도록 조금 나누어서 관리해보았습니다.

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

public class spawn : MonoBehaviour
{
    public GameObject Prefab;
    Vector3 randomPos;

    // Start is called before the first frame update
    void Start()
    {
        Invoke("SpawnEnemy", 2);
    }

    void SpawnEnemy()
    {
        InvokeRepeating("SpawnEnemy_1", 3, 2);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void SpawnEnemy_1()
    {
        randomPos= new Vector3(Random.Range(2.5f,-2.5f),2,-60);
        GameObject opt = (GameObject)Instantiate(Prefab, randomPos, Quaternion.identity);
        
    }
    
}

 

 

 

 

다음은 약간의 게임적 요소를 위한 타이머도 만들어 보았습니다.

 

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

public class timer : MonoBehaviour
{
    public TextMeshProUGUI timeText;
    
    private float time;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        time += Time.deltaTime;
        timeText.text = string.Format("{0:N2}", time);


    }
}

 

그 후 

게임상에 빈 오브젝트를 하나 만들고 적용시켜 주었습니다.

 

 

 

 

다음은 모바일 버전을 위한 버튼 생성입니다.

 

게임화면상에 적절한 위치에 생성해주고

 

캐릭터인 볼을

 

버튼의 원클릭 함수 안에 넣은후

 

알맞은 함수들을 넣어줍시다!

 

 

 

일단 여기까지가 게임 구성이고 실행버튼을 눌러 컴퓨터 상에서도 플레이 가능합니다!

 

 

 

이제 이걸 빌드해봅시다

 

안드로이드로 빌드하기 위해서는 다음과 같은파일들을 먼저 다운 받아야합니다!

 

이걸 다운 받고 나면

 

 

아래처럼 빌드설정을 스위칭 해주고 빌드를 누르면?

 

 

 

 

 

apk파일이 나오고 이걸 휴대폰에 설치해주기만 하면 

 

 

아래처럼 게임을 플레이 하실수 있습니다!

 

 

여기까지! 간단한 유니티 안드로이드 게임 이였습니다!

 

 

 

 

 

틀린점이 있다면 댓 달아주세요!

 

 

 

댓글