본문 바로가기

게임 개발 공부 정리

[Unity] C#기초 문법 정리 코드 - 20 09 17

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

public class Test : MonoBehaviour //: MonoBehaviour 유니티의 게임 오브젝트를 뜻한다.
{
    void Start()
    {
        #region 주석

        #region 1.변수
        //1. 변수
        int a = 5;
        float flo = 0.1f;
        string aso = "사람";
        bool isbool = true;
        #endregion 1.변수

        #region 2. 그룹형 변수
        //* 그룹형 변수
        //길이 반환 : str.Length 
        string[] str = { "가나", "나다", "라나", "마바" };
        int[] nums = { 1, 2, 3, 4, 5, 6, 7 };

        int[] numss = new int[3]; //new : 새로운 변수를 만들겠다는 뜻
        numss[0] = 1;
        numss[1] = 2;
        numss[2] = 3;


        //* 리스트 : 기능이 추가 된 가변형 그룹형 변수
        // 길이 반환 : items.Count
        List<string> items = new List<string>();

        items.Add("생명물약");
        items.Add("마나 물약");
        //추가되는 순으로 순서가 정해진다.

        Debug.Log("가지고 있는 아이템");
        Debug.Log(items[0]);
        Debug.Log(items[1]); //출력 결과 : 생명물약, 마나물약

        //해당 번지수의 값을 삭제하고 그 앞에 있던 값들을 1번지 당긴다.
        items.RemoveAt(0);
        Debug.Log(items[0]);

        Debug.Log(items[1]); //여기에 있는 값은 부재가 되므로 오류가 발생하게 된다.

        #endregion 2. 그룹형 변수

        #region 3. 연산자
        string 칭호 = "~한";
        string playername = "정든솔";

        Debug.Log("칭호는?");

        //문자열을 붙여 출력할때 방식
        Debug.Log(칭호 + " " + playername);
        #endregion 3. 연산자

        #region 4. 논리
        int fullevel = 99;
        bool isfullevel = 90 == fullevel;
        Debug.Log("만렙?" + isfullevel); //true 반환
        #endregion 4. 논리

        #region 5. 조건문
        //5. 조건문
        //switch - case 문 사용 가능

        int tmp = 5;
        switch (tmp)
        {
            case 1:
            case 2:
                Debug.Log("값은 1~2이다");
                break;
            default:
                Debug.Log("암것도 아님");
                break;
        }
        #endregion 5. 조건문

        #region 6. 반복문
        //foreach : for의 그룹형변수 탐색 특화
        //foreach(type var_name in group_var)
        string[] tmp_monsters = { "one", "two", "three", "four" };

        foreach (string monster in tmp_monsters)
        {
            Debug.Log("이 지역에 있는 몬스터: " + monster);
        }

        #endregion 6. 반복문

        #region 7. 클래스
        //클래스 : 하나의 사물(오브젝트)와 대응하는 로직

        PlayerMoveController playerMove = new PlayerMoveController();
        playerMove.runSpeed = 5.0f;
        //클래스 안에 있는 변수를 멤버 변수라 부른다.
        //멤버 함수 또한 private/public등을 줘서 접근하여 사용하게 할 수 있다.

        Son class_var = new Son();
        class_var.son_var1 = 1;
        class_var.Mother_fun1(1, 2);
        class_var.mother_var2 = 3;

        #endregion 7. 클래스
    }
    #endregion 주석

    void Update()
    {

    }
}