게임개발/게임플랫폼 응용 프로그래밍

Enum , List<> , Foreach 연습하기

pudding81 2024. 2. 28. 11:47

 

Enum : 열거형

 

 

List <>: 동적 배열

 

 List<T>는 T 타입의 값들을 저장하는 동적 배열이며,

Add, Remove, Insert, Sort 등의 메소드를 통해 동적 배열을 관리할 수 있습니다. 

 

 

 

Foreach : 배열의 출력 

 

Foreach는 배열 뿐만 아니라 리스트, 딕셔너리, 셋 등의 컬렉션 타입에도 적용할 수 있습니다.

 

 

 

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

public class Item 
{
   
    public enum ItemType
    {
        SWORD , DAGGER
    }
    
    public ItemType type;
    public string grade;

    public Item()
    {

    }

    public Item(ItemType type, string grade)
    {
        this.type = type;   
        this.grade = grade;
    }



}

 

using System.Collections;
using System.Collections.Generic;
using UnityEditorInternal.VersionControl;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    List<Item> items;
  

    void Start()
    {
        //아이템이 들어갈 공간(가방)을 만든다
        items = new List<Item>();

        //아이템(장검)을 생성한다
        Item sword = new Item(Item.ItemType.SWORD,"Normal");
        //아이템(장검)을 가방에 넣는다
        items.Add(sword);



        //아이템(단검)을 생성한다
        //아이템(단검)을 가방에 넣는다
        items.Add(new Item(Item.ItemType.DAGGER,"Legend"));



        //가방에 있는 아이템의 종류를 출력한다
        //SWORD
        //DAGGER

        foreach(Item item in items)
        {
            Debug.LogFormat("{0} {1}",item.type,item.grade);

        }

        // 가방에 있는 아이템의 등급을 출력한다.
        // 아이템의 등급은 일반, 고급(Magic), 희귀(Rare), 영웅(Epic), 전설이 있다.
        //SWORD (Normal)
        //DAGGER (Legend)




    }



}