본문 바로가기

Unity/멋쟁이사자처럼 부트캠프

[멋쟁이사자처럼 부트캠프 TIL회고] Unity 게임 개발 3기 9일차 자료구조 Array

Extract Method (함수로 만들기):

블록 지정  > ctrl + R,M

혹은 

블록지정 > 우클릭 Refactor > 메서드 추출

 

#region 기능 사용

가독성향상

#region 구역이름
private int intNum = 123;

#endregion

 

이차원배열과 Random기능을 활용하여

랜덤한 오브젝트를 타일 맵으로 생성

void MapGenerationExample()
    {
    //랜덤한 맵 타일 생성
        for (int x = 0; x < mapTiles.GetLength(0); x++)
        {
            for (int y = 0; y < mapTiles.GetLength(1); y++)
            {
                mapTiles[x, y] = Random.value > 0.8f ? 1 : 0;
            }
        }

	//위에서 생성한 랜덤한 맵타일이 1인경우 큐브, 0인경우 Sphere구를 생성
    //부모(현재 오브젝트)의 자식 오브젝트로 생성하도록 수정
        for (int x = 0; x < mapTiles.GetLength(0); x++)
        {
            for (int y = 0; y < mapTiles.GetLength(1); y++)
            {
                Vector3 pos = new Vector3(x, y, 0);
                
                if (mapTiles[x, y] == 1)
                {
                    Instantiate(Cube, transform.position + pos, Quaternion.identity, transform);
                }
                else
                {
                    Instantiate(Sphere,transform.position+pos,Quaternion.identity,transform);
                }
            }
        }
    }

공식문서 상의 Instantiate()함수의 사용방법

 

Format Specifieres:

예) 

for (int i = 0; i < playerScores.Length; i++)
    {
        playerScores[i]  = Random.Range(100, 1000);
    }
        
double averageScore = playerScores.Average();
Debug.Log($"평균 점수2: {averageScore:F2}");

F2에서 F는 float Format Specifieres (형식 지정자)

 

.NET의 Format Specifieres 관련 문서

 

Standard numeric format strings - .NET

In this article, learn to use standard numeric format strings to format common numeric types into text representations in .NET.

learn.microsoft.com