본문 바로가기

코딩 공부/Unity C#

Unity 설치 및 환경 구성 -1-

C# Progarmming for Unity Game Development 

 

Visual Studio modules 설치

- .NET 데스크톱 개발 

- Unity 개발

VS 콘솔 앱 작성

1. 새 프로젝트 console app(C#) 선택

2. 프레임워크 버전 선택 (+ '최상위 문 사용 안 함' 체크)

 

// using System은 기본적으로 포함되어 있음

namespace FirstConsoleApp	// 파일명(FirstConsoleApp)의 네임스페이스 생성 
{
	///
    /// 클래스 설명 주석
    ///
    internal class Program
    {
        static void Main(string[] args)    // 메소드
        {
            Console.WriteLine("hello");		// hello 출력
        }
    }
}

 

Unity 설치

- Unity Hub 설치 후 Install에서 안정적인 최신 버전 설치

- 계정 옆 톱니바퀴 아이콘으로 프로젝트 저장 경로 설정

효율적 유니티 레이아웃

- Assets 폴더 정리 잘 하기

 

PrintScripts C# 스크립트(VS)

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

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    // Start는 스크립트가 활성화될 때 호출
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

유니티에서  문장을 출력하는 함수는 debug.log / print (debug.log가 더 보편적)

(console.writeline은 컴파일에는 문제가 없지만 게임을 실행할 때 아무 출력도 나오지 않음)

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

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Howdy hoo");   
    }
}

 

 

- vs에서 코드를 다 작성하면 'f6' / '솔루션 빌드' 클릭

- 유니티에 C# 디버거가 연결되었다는 창이 뜨면 'Enable ebugging for this session' 클릭

- 스크립트 어셈블리 다시 로드 후 편집기로 돌아오기

- 유니티에서 게임을 실행하려면 상단의 '화살표 버튼' 클릭 / ctrl P 

 

이 상태로만 하면 실행해도 아무 변화가 없음

  →스크립트는 대부분 게임 오브젝트에 연결되어야 함!

 

- PrintMessage(스크립트)를 Main Camera(오브젝트) 드래그하여 attach

- 연결 후 ctrl s 로 저장

콘솔창에 스크립트가 뜬다

※출력이 뜨지 않는다면 화살표 말풍선 아이콘을 클릭하지는 않았는지 확인해본다

(화살표 말풍선 아이콘이 눌려있으면 출력이 뜨지 않음)