본문 바로가기

Unity/Unity Learn

Unity Essentials > Essentials of programming in Unity

https://learn.unity.com/project/essentials-of-programming-in-unity?uv=2019.4&pathwayId=5f7bcab4edbc2a0023e9c38f&missionId=5f777d9bedbc2a001f6f5ec7

 

Essentials of programming in Unity - Unity Learn

This learning project will give you a sample of the essential tasks of a Unity programmer. These tasks can also be useful in any other role when you want to customize the ways GameObjects behave. Although many tasks in Unity don’t require programming, it

learn.unity.com

 

 

◈ Get started with scripts

4.Create a new script

새 GameObject가 선택된 상태에서 Inspector 창에서 Add Component를 선택한다. 

New Script 옵션을 선택한다.

 

 

이제 스크립트가 빈 GameObject에 구성 요소로 추가되고 이제 프로젝트의 Assets 폴더에도 나타난다.

 



 

 

 

 

◈ Code in the default script

2. The default script

새 스크립트를 만들 때 MonoBehaviour라는 기본 제공 클래스를 상속하는 새 public 클래스를 만든다 .

 

스크립트에는 Start()Update() 의 두 가지 함수를 포함하고 있다 .

Start()는 게임 시작 시 한 번 실행되고, Update()는 게임의 모든 프레임에서 실행된다

 

 

 

 

 

 

◈ Change a GameObject with script

4. Experiment with scale

Scale Change에서 프레임당 변화량 대신 초당 변화량을 조정. 

 

Unity Scripting API프레임 사이의 시간을 초 단위로 감지하기 위해 Time.deltaTime값을 제공한다. 

 

transform.localScale += (scaleChange * Time.deltaTime);

 

 

5. Try more transforms

회전을 증가시키는 스크립트는 약간 다르다. 

Rotate() 메서드는 게임 오브젝트의 회전에 추가하는 반면, 

다른 스크립트는 += 연산자를 사용하여 스크립트에서 계산된 속성을 변경한다.

 

Increment position

// Update is called once per frame
void Update()
{
	transform.position += (positionChange * Time.deltaTime);
}

 

Increment rotation

// Update is called once per frame
void Update()
{
	transform.Rotate (rotateChange * Time.deltaTime);
}