유니티/기초

유니티 입문 - 기본 문법(1)

무직백수취업준비생 2021. 4. 9. 10:16
728x90
반응형
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    void Start()
    {
        Debug.Log("Hello Unity!");

        int level;
        float strength;
        string playerName;
        bool isFullLevel;
    }
}

저번에 Hello Unity! 문구 출력에 이어서 오늘은 기본 문법 학습을 진행하겠습니다.

 

변수 : 데이터를 메모리에 저장하는 장소

 

변수에는 크게 int, float, string, bool 이렇게 네가지 종류가 있습니다.

 

int : 정수형 데이터 (1, 2, 3, ...)

 

float : 숫자형 데이터 (1.1, 1.2, 2.5, 6.6, ...)

 

string : 문자열 데이터 (가나다라, abcd, ...)

 

bool : 논리형 데이터 (true, false)

 

 

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

public class NewBehaviourScript : MonoBehaviour
{
    void Start()
    {
        Debug.Log("Hello Unity!");

        int level = 5;
        float strength = 15.5f;
        string playerName = "남주형";
        bool isFullLevel = false;
    }
}

 

이렇게 변수의 타입과 이름을 정하는 것이 선언, 그리고 그 값을 넣는것이 초기화 입니다.

 

프로그래밍은 크게 선언 -> 초기화 -> 호출(사용) 의 순서로 진행됩니다.

 

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

public class NewBehaviourScript : MonoBehaviour
{
    void Start()
    {
        Debug.Log("Hello Unity!");

        int level = 5;
        float strength = 15.5f;
        string playerName = "남주형";
        bool isFullLevel = false;

        Debug.Log("용사의 이름은?");
        Debug.Log(playerName);
        Debug.Log("용사의 레벨은?");
        Debug.Log(level);
        Debug.Log("용사의 힘은?");
        Debug.Log(strength);
        Debug.Log("용사는 만렙인가?");
        Debug.Log(isFullLevel);
    }
}

해당 코드를 삽입하고 실행해보겠습니다.

 

콘솔창에서 변수의 값이 출력되는것을 볼 수 있습니다.

 

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

public class NewBehaviourScript : MonoBehaviour
{
    void Start()
    {
        Debug.Log("Hello Unity!");

        //1. 변수
        int level = 5;
        float strength = 15.5f;
        string playerName = "남주형";
        bool isFullLevel = false;

        Debug.Log("용사의 이름은?");
        Debug.Log(playerName);
        Debug.Log("용사의 레벨은?");
        Debug.Log(level);
        Debug.Log("용사의 힘은?");
        Debug.Log(strength);
        Debug.Log("용사는 만렙인가?");
        Debug.Log(isFullLevel);
    }
}

다음으로 넘어가기전에 해당 내용이 변수임을 메모하도록 합니다.

 

단순히 변수라고 입력하면 오류가 나기때문에 /를 두번 사용해 주석으로 달아둡니다.

 

주석 : 실행되지 않는 메모와 같은 역활

 

 

 

 

그룹형 변수 : 변수들을 묶은 하나의 장소

 

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

public class NewBehaviourScript : MonoBehaviour
{
    void Start()
    {
        Debug.Log("Hello Unity!");

        //1. 변수
        int level = 5;
        float strength = 15.5f;
        string playerName = "남주형";
        bool isFullLevel = false;

        Debug.Log("용사의 이름은?");
        Debug.Log(playerName);
        Debug.Log("용사의 레벨은?");
        Debug.Log(level);
        Debug.Log("용사의 힘은?");
        Debug.Log(strength);
        Debug.Log("용사는 만렙인가?");
        Debug.Log(isFullLevel);

        //2. 그룹형 변수
        string[] monster = { "슬라임", "사막뱀", "악마" };

        Debug.Log("맵에 존재하는 몬스터");
        Debug.Log(monster[0]);
        Debug.Log(monster[1]);
        Debug.Log(monster[2]);
    }
}

 

해당 코드를 실행하면 0번, 1번, 2번 위치에 저장된 몬스터의 이름이 출력되게 됩니다.

 

 

 

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

public class NewBehaviourScript : MonoBehaviour
{
    void Start()
    {
        Debug.Log("Hello Unity!");

        //1. 변수
        int level = 5;
        float strength = 15.5f;
        string playerName = "남주형";
        bool isFullLevel = false;

        

        //2. 그룹형 변수
        string[] monster = { "슬라임", "사막뱀", "악마" };
        int[] monsterLevel = new int[3];
        monsterLevel[0] = 1;
        monsterLevel[1] = 6;
        monsterLevel[2] = 20;

        Debug.Log("맵에 존재하는 몬스터의 레벨");
        Debug.Log(monsterLevel[0]);
        Debug.Log(monsterLevel[1]);
        Debug.Log(monsterLevel[2]);
    }
}

다른방법으로 그룹형변수에서 값을 출력해보겠습니다.

 

 

이다음으로 리스트를 활용해 보도록 하겠습니다.

 

리스트 : 기능이 추가된 가변형 그룹형 변수

 

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

public class NewBehaviourScript : MonoBehaviour
{
    void Start()
    {
        Debug.Log("Hello Unity!");

        //1. 변수
        int level = 5;
        float strength = 15.5f;
        string playerName = "남주형";
        bool isFullLevel = false;

        

        //2. 그룹형 변수
        string[] monster = { "슬라임", "사막뱀", "악마" };
        int[] monsterLevel = new int[3];
        monsterLevel[0] = 1;
        monsterLevel[1] = 6;
        monsterLevel[2] = 20;

        Debug.Log("맵에 존재하는 몬스터의 레벨");
        Debug.Log(monsterLevel[0]);
        Debug.Log(monsterLevel[1]);
        Debug.Log(monsterLevel[2]);

        List<string> items = new List<string>();
        items.Add("체력물약30");
        items.Add("활력물약30");

        Debug.Log("가지고 있는 아이템");
        Debug.Log(items[0]);
        Debug.Log(items[1]);
    }
}

 

List<> 꺽쇠 안에는 사용할 변수의 종류를 지정해줘야 합니다.

 

여기서 List의 특징으로, 원하는 데이터를 삭제할 수도 있습니다.

 

0번 자리에 있는 체력물약30을 삭제하고 출력해보겠습니다.

 

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

public class NewBehaviourScript : MonoBehaviour
{
    void Start()
    {
        Debug.Log("Hello Unity!");

        //1. 변수
        int level = 5;
        float strength = 15.5f;
        string playerName = "남주형";
        bool isFullLevel = false;

        

        //2. 그룹형 변수
        string[] monster = { "슬라임", "사막뱀", "악마" };
        int[] monsterLevel = new int[3];
        monsterLevel[0] = 1;
        monsterLevel[1] = 6;
        monsterLevel[2] = 20;

        Debug.Log("맵에 존재하는 몬스터의 레벨");
        Debug.Log(monsterLevel[0]);
        Debug.Log(monsterLevel[1]);
        Debug.Log(monsterLevel[2]);

        List<string> items = new List<string>();
        items.Add("체력물약30");
        items.Add("활력물약30");

        items.RemoveAt(0);

        Debug.Log("가지고 있는 아이템");
        Debug.Log(items[0]);
        Debug.Log(items[1]);

        
    }
}

보이는것과 같이 index 오류가 나오는걸 확인할 수 있습니다.

 

아이템에서 0번 위치의 체력물약30을 삭제해놓고 탐색하려니 발생하는 오류입니다.

 

크기를 벗어난 탐색은 오류를 발생합니다.

728x90
반응형