Skip to content
김광수 edited this page Oct 11, 2023 · 1 revision

< Readme

Util

자주 사용될 만한 메서드들을 static 클래스 Util에 static 메서드로 정의하였습니다.

Code Summary

Method

  • SelectNumber()

    • start 부터 end 전까지의 숫자중 select개를 선택해서 List로 리턴합니다.
  • SelectIndex()

    • List와 select를 받아서 List의 인덱스중 select 개를 선택해서 인덱스의 List를 리턴합니다.
  • SelectItemsFromList()

    • List에서 select개의 요소를 선택해서 새로 만든 List를 리턴합니다.
  • SelectItemFromList()

    • List에서 한개의 요소를 선택해서 리턴합니다.
  • RemoveRandomItemsFromList()

    • List에서 number개의 요소를 랜덤으로 선택해서 삭제합니다.

Code

using System.Collections.Generic;
using UnityEngine;

public static class Util
{
    public static List<int> SelectNumber(int start, int end, int select)
    {
        List<int> ints = new List<int>();
        int count = end - start;

        for (int i = start; i < count; i++)
        {
            ints.Add(i);
        }

        for (int i = 0; i < count - select; i++)
        {
            int randInt = Random.Range(0, ints.Count);
            ints.RemoveAt(randInt);
        }

        return ints;

    }

    public static List<int> SelectIndex<T>(List<T> list, int select)
    {
        List<int> ints = new List<int>();

        for (int i = 0; i < list.Count; i++)
        {
            ints.Add(i);
        }

        for (int i =0; i <list.Count -select; i++)
        {
            int randInt = Random.Range(0, ints.Count);
            ints.RemoveAt(randInt);
        }

        return ints;
    }
    public static List<T> SelectItemsFromList<T>(List<T> list, int select)
     {
        // list에서 select개의 요소롤 선택해 list를 return한다.

        List<int> ints = new List<int>();
        List<T> returnList = new List<T>();

        for (int i = 0; i < list.Count; i++)
        {
            ints.Add(i);
        }

        for (int i = 0; i < select; i++)
        {
            int randInt = Random.Range(0, ints.Count);
            int selectedIndex = ints[randInt];

            returnList.Add(list[selectedIndex]);
            ints.RemoveAt(randInt);
            
        }

        return returnList;
        
    }

    public static T SelectItemFromList<T>(List<T> list)
    {
        
        int randInt = Random.Range(0, list.Count);
        return list[randInt];
    }

    public static void RemoveRandomItemsFromList(List<int> list,int number)
    {
        for(int i=0; i< number; i++)
        {
            int randInt = Random.Range(0, list.Count);
            list.RemoveAt(randInt);
        }
        
    }

Clone this wiki locally