골드메탈 유니티 강좌를 보고 정리한 내용입니다.
변수 타입과 조건문, 반복문, 함수, 클래스 등 기본문법에 대해서 따라해본 코드 내용입니다.
NewBehaviourScript.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class NewBehaviourScript : MonoBehaviour { string title = "전설의"; string playerName = "나검사"; int level = 5; float strength = 15.5f; int exp = 1500; int health = 30; int mana = 25; bool isFullLevel = true; void Start() { Debug.Log("Hello Unity"); //1. 변수 /*int level = 5; float strength = 15.5f; string playerName = "나검사"; bool isFullLevel = true;*/ Debug.Log("용상의 이름은?"); Debug.Log(playerName); Debug.Log("용상의 레벨은?"); Debug.Log(level); Debug.Log("용상의 힘은?"); Debug.Log(strength); Debug.Log("용상의 만랩인가?"); Debug.Log(isFullLevel); //2. 그룹형 변수 //2-1.문자열 배열 string[] monsters = { "슬라임", "사막뱀", "악마" }; Debug.Log("맵에 존재하는 몬스터"); Debug.Log(monsters[0]); Debug.Log(monsters[1]); Debug.Log(monsters[2]); //2-2. 정수형 배열 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]); //2-3. 리스트 List<string> items = new List<string>(); items.Add("생명물약30"); //리스트 값 넣기 items.Add("마나물약30"); Debug.Log("가지고 있는 아이템"); Debug.Log(items[0]); Debug.Log(items[1]); //items.RemoveAt(0); //특정 인덱스(0번째) 리스트 삭제 방법 //3. 연산자 //int exp = 1500; exp = 1500 + 320; exp = exp - 10; level = exp / 300; strength = level * 3.1f; Debug.Log("용사의 총 경험치는?"); Debug.Log(exp); Debug.Log("용사의 레벨은?"); Debug.Log(level); Debug.Log("용사의 힘은?"); Debug.Log(strength); int nextExp = 300 - (exp % 300); Debug.Log("다음 레벨까지 남은 경험치?"); Debug.Log(nextExp); //string title = "전설의"; Debug.Log("용사의 이름은?"); Debug.Log(title + " " + playerName); int fullLevel = 99; isFullLevel = level == fullLevel; Debug.Log("용사는 만렙입니까? " + isFullLevel); bool isEndTutorial = level > 10; Debug.Log("튜토리얼이 끝난 용사입니까? " + isEndTutorial); //int health = 30; //int mana = 25; //bool isBadCondition = health <= 50 && mana <= 20; bool isBadCondition = health <= 50 || mana <= 20; Debug.Log("용사의 상태가 나쁩니까? " + isBadCondition); string condition = isBadCondition ? "나쁨" : "좋음"; Debug.Log("용사의 상태가 나쁩니까? " + condition); //4. 키워드 //int float string bool new list //5. 조건문 if (condition == "나쁨") { Debug.Log("플레이어 상태가 나쁘니 아이템을 사용하세요"); } else { Debug.Log("플레이어 상태가 좋습니다."); } if (isBadCondition && items[0] == "생명물약30") { items.RemoveAt(0); health += 30; Debug.Log("생명포션30을 사용하였습니다."); } else if (isBadCondition && items[0] == "마나물약30") { items.RemoveAt(0); mana += 30; Debug.Log("마나포션30을 사용하였습니다."); } /** switch (변수) { case 값1: break; case 값2: break; case 값3: break; } **/ switch (monsters[1]) { case "슬라임": case "사막뱀": Debug.Log("소형 몬스터가 출현!"); break; case "악마": Debug.Log("중형 몬스터가 출현!"); break; case "골렘": Debug.Log("대형 몬스터가 출현!"); break; default: Debug.Log("??? 몬스터가 출현!"); break; } //6. 반복문 while (health > 0) { health--; if (health > 0) { Debug.Log("독 데미지를 입었습니다. " + health); } else { Debug.Log("사망하였습니다."); } if (health == 10) { Debug.Log("해독제를 사용합니다."); break; } } for (int count = 0; count < 10; count++) { health++; Debug.Log("붕대로 치료 중 ..." + health); } for (int index = 0; index < monsters.Length; index++) { Debug.Log("이 지역에 있는 몬스터 : " + monsters[index]); } foreach (string monster in monsters) { Debug.Log("이 지역 있는 몬스터 : " + monster); } //7. 함수(메소드) //health = heal(health); heal(); for (int index = 0; index < monsters.Length; index++) { Debug.Log("용사는 " + monsters[index] + "에게 " + battle(monsterLevel[index])); } //8. 클래스 //Actor player = new Actor(); Player player = new Player(); player.id = 0; player.name = "나법사"; player.title = "현명한"; player.strength = 2.4f; player.weapon = "나무 지팡이"; Debug.Log(player.talk()); Debug.Log(player.hasWeapon()); player.levelUp(); Debug.Log(player.name + "의 레벨은 " + player.level + " 입니다."); Debug.Log(player.move()); } /*int heal(int currentHealth) { currentHealth += 10; Debug.Log("힐을 받았습니다. " + currentHealth); return currentHealth; }*/ void heal() { health += 10; Debug.Log("힐을 받았습니다. " + health); } string battle(int monsterLevel) { string result; if (level >= monsterLevel) { result = "이겼습니다."; } else { result = "졌습니다."; } return result; } }
Actor.cs
public class Actor { public int id; public string name; public string title; public string weapon; public float strength; public int level; public string talk() { return "대화를 걸었습니다"; } public string hasWeapon() { return weapon; } public void levelUp() { level = level + 1; } }
Player.cs
public class Player : Actor { public string move() { return "플레이어는 움직입니다."; } }
'UNITY' 카테고리의 다른 글
유니티 키보드와 마우스 입력 (0) | 2022.06.30 |
---|---|
유니티 게임오브젝트의 흐름 LifeCyle (0) | 2022.06.29 |
댓글