상세 컨텐츠

본문 제목

유니티 list 사용법 노트

Unity/C# note

by 오발탄LAB 2020. 8. 13. 17:07

본문

반응형

 리스트 size 정하기 

 public List<GameObject> storms = new List<GameObject>();
 
 foreach (GameObject storm in GameObject.FindGameObjectsWithTag("Sandstorm"))
        {
            storms.Add(storm);
        }
public List<int> numbers = new List<int>();
private int listSzie = 5;

void Start()
{
	for(int i =0; i < listSize; i++){
    	numbers.Add(0);
    }
}

 

※ inspector에서 list size를 0으로 설정해야 한다.

 

 Game Object 추가하기 - 이름에 숫자 추가 

 

public class List_My : MonoBehaviour
{

    public List<GameObject> goList = new List<GameObject>();

    void Start()
    {
        for (int i = 0; i < 3; i++)
        {
            GameObject obj = new GameObject($"NewObect_{i}");
            goList.Add(obj);
        }
    }
}

 

 

 리스트에서 특정 숫자 개수 파악하기 

public List<Vector3> points = new List<Vector3>();
    
    void Start()
    {
        points.Add(new Vector3(1, 5, 2));
        points.Add(new Vector3(1, 4, 2));
        points.Add(new Vector3(1, 0, 2));
        points.Add(new Vector3(1, -3, 2));
    }

    void Update()
    {
        Debug.Log("CountPoints: " + sumPoints(points));
    }
    
    private int sumPoints(List<Vector3> _points) {
        int countPoints = 0;
        foreach (Vector3 point in _points) {
            if (point.y > 1) {
                countPoints++;
            }
        }
        return countPoints;
    }
    
    output: 2

 

 리스트 정렬하기 

public List<int> numberList = new List<int>();
    
    void Start()
    {
        numberList.AddRange(new int[] { 5, 43, 17, 2 });
        
        foreach (int num in numberList)
        {
            Debug.Log(num);  
        }
			**output: 5, 43, 17, 2 
        
        numberList.Sort();   //작은 수에서 큰 수로 정렬해준다.

        string result = "numbers: ";
        foreach (int _num in numberList)
        {
            result += _num.ToString() + " ";
        }
        Debug.Log(result);
        
        	**output: 2, 5, 17, 43
    }

 요소들 사이에 다른 요소 집어넣기 

    public List<int> numberList = new List<int>();
    
    void Start()
    {
        numberList.AddRange(new int[] { 5, 43, 17, 2 });

        numberList.Insert(numberList.Count - 1, 99);
        
        string result = "result: ";
        foreach (int num in numberList) {
            result += num.ToString() + " ";
        }
        Debug.Log(result);
    }
    	**output: 5 43 17 99 2

 foreach사용하여 특정 tag object를 리스트에 집어넣기 

public List<GameObject> storms = new List<GameObject>();

 foreach (GameObject storm in GameObject.FindGameObjectsWithTag("Sandstorm"))
        {
            storms.Add(storm);
        }

 

 두 가지 요소 저장하기 

public class ListTemplate
{
    public string ListName { get; set; }
    public int ListID; //프로퍼티 아니여도 이렇게 작성해도 상관 없음 

}

public class List_My : MonoBehaviour
{
    public List<ListTemplate> listSample = new List<ListTemplate>();

    void Start()
    {

        listSample.Add(new ListTemplate() { ListName = "red", ListID = 0 });
        listSample.Add(new ListTemplate() { ListName = "blue", ListID = 1 });
        listSample.Add(new ListTemplate() { ListName = "orange", ListID = 2 });
        listSample.Add(new ListTemplate() { ListName = "black", ListID = 12345 });

        foreach (var item in listSample)
        {
            Debug.Log($"List Name: {item.ListName}, List ID: {item.ListID}");
        }
    }

 

 

 list.AddRange(array) 

 

+ list에 array 통째로 추가하기

반응형

관련글 더보기

댓글 영역