๐ŸŽฎ Unity Study/Unity

[Unity Associate Programmer ์ž๊ฒฉ์ฆ ์ค€๋น„] ์ด๋ก ๊ฐœ๋… ์ •๋ฆฌ

ibelieveinme 2023. 5. 28. 03:46
728x90

์”ฌ ํ”Œ๋กœ ๋ฐ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ

 

์‹ค์‹œ๊ฐ„ 3D ๊ฒฝํ—˜ ๋””์ž์ธ ์†Œ๊ฐœ - Unity Learn

์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ์‹ค์‹œ๊ฐ„ 3D ๊ฒฝํ—˜ ๋””์ž์ธ์˜ ๊ธฐ์ดˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์šธ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ์ œ์ž‘ ๋‹จ๊ณ„๋ฅผ ์š”์•ฝํ•ฉ๋‹ˆ๋‹ค. ๋””์ง€ํ„ธ ๊ฒฝํ—˜์„ ์ œ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ํ•ต์‹ฌ ๋””์ž์ธ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฒ€ํ† 

learn.unity.com

1. ์”ฌ ํ”Œ๋กœ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ

1) ์‹ค์‹œ๊ฐ„ 3D ๊ฒฝํ—˜ ๋””์ž์ธ ์†Œ๊ฐœ

 

[๋””์ž์ธ ํ”„๋กœ์„ธ์Šค]

Pre-projection -> Production -> Post-production -> Operations

์‚ฌ์ „ ์ œ์ž‘ -> ์ •์‹์ œ์ž‘ -> ์‚ฌํ›„ ์ œ์ž‘ -> ์šด์˜

 

ํŒŒ์ดํ”„๋ผ์ธ ๊ด€๋ฆฌ์— ์žˆ์–ด์„œ ๋””์ž์ธ ํ”„๋กœ์„ธ์Šค๋Š” ์ œ์ž‘ ์‚ฌ์ดํด์˜ ์•ž 3 ๋‹จ๊ณ„์ธ ์‚ฌ์ „ ์ œ์ž‘, ์ •์‹ ์ œ์ž‘, ์‚ฌํ›„ ์ œ์ž‘์— ํ•ด๋‹นํ•œ๋‹ค.

๊ตฌ์ฒด์ ์ธ ๋‹จ๊ณ„๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

1๋‹จ๊ณ„: ๋ฌธ์ œ ํŒŒ์•… ๋˜๋Š” ์•„์ด๋””์–ด ์ •์˜

2๋‹จ๊ณ„: ์‚ฌ์šฉ์ž ๋ฐ ์‹œ์žฅ ์กฐ์‚ฌ ์ง„ํ–‰

3๋‹จ๊ณ„: ์‚ฌ์šฉ์ž ํŽ˜๋ฅด์†Œ๋‚˜ ์ƒ์„ฑ ๋ฐ ์‚ฌ์šฉ ์‚ฌ๋ก€ ์ •์˜

4๋‹จ๊ณ„: ๋ธŒ๋ฆฌํ”„ ๊ฐœ์„  ๋ฐ ๋ชฉํ‘œ ๋ช…ํ™•ํ™”

5๋‹จ๊ณ„: ์˜ˆ๋น„ ๋””์ž์ธ ๋งŒ๋“ค๊ธฐ

6๋‹จ๊ณ„: ํ”„๋กœํ† ํƒ€์ดํ•‘

7๋‹จ๊ณ„: ๋ฐ˜๋ณต ๋””์ž์ธ ๋ฐ ๊ฐœ๋ฐœ (ํ”„๋กœํ† ํƒ€์ž…์„ ์Šน์ธํ•˜๋ฉด ๋””์ž์ธ -> ์ œ์ž‘ -> ํ…Œ์ŠคํŠธ -> ํ‰๊ฐ€ -> ๋””์ž์ธ ๊ฐœ์„  ์ž‘์—…์„ ๊ณ„์† ๋ฐ˜๋ณตํ•จ)

 

[๋””์ž์ธ ๋ฐ ๊ฐœ๋ฐœ ์ ‘๊ทผ ๋ฐฉ์‹]

1) ์›Œํ„ฐํ’€ ์ ‘๊ทผ ๋ฐฉ์‹

๋งˆ์น˜ ํญํฌ๊ฐ€ ์•„๋ž˜๋กœ ํ๋ฅด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ํ•œ ๋‹จ๊ณ„๊ฐ€ ์™„๋ฃŒ๋˜๊ณ  ์Šน์ธ๋˜๋ฉด ๋‹ค์Œ ๋‹จ๊ณ„๊ฐ€ ์‹œ์ž‘๋จ.

 

2) ์• ์ž์ผ ์ ‘๊ทผ ๋ฐฉ์‹

๊ณผ์ • ๋ฐ˜๋ณต์ ์ธ ์ ‘๊ทผ ๋ฐฉ์‹

 

3) ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ์ ‘๊ทผ ๋ฐฉ์‹

์• ์ž์ผ + ์›Œํ„ฐํ’€ ์š”์†Œํ•œ ๋ฐฉ์‹. ํ”„๋กœ์ ํŠธ ์‚ฌ์ „ ์ œ์ž‘ ๊ณผ์ •์—์„œ ์„ ํ˜•์ ์ธ ๋ฐฉ์‹์œผ๋กœ ์ค€๋น„๋ฅผ ์™„๋ฃŒํ•œ ๋‹ค์Œ, ํŠน์ • ๊ธฐ๋Šฅ์— ์ค‘์ ์„ ๋‘” ์‚ฌ์ดํด์—์„œ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ œ์ž‘ ๋ฐ ํ…Œ์ŠคํŠธํ•˜๋Š” ์ž‘์—…์ด ํฌํ•จ๋  ์ˆ˜ ์žˆ์Œ.

 

2. ๋ฒ„์ „ ๊ด€๋ฆฌ ์„ค์ •

๋ชจ๋“  ์ข…๋ฅ˜์˜ ํŒŒ์ผ์ด๋‚˜ ์†Œํ”„ํŠธ์›จ์–ด, ์›น์‚ฌ์ดํŠธ, ๊ธฐํƒ€ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ €์žฅ, ๊ด€๋ฆฌ, ์ถ”์ ํ•˜๋„๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ.

๊ฐœ์ธ์ด๋‚˜ ์†Œ๊ทœ๋ชจ ํŒ€, ๋Œ€๊ทœ๋ชจ ์กฐ์ง์€ ๋ณต์žกํ•œ ํ”„๋กœ์ ํŠธ๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ๋ฐฑ์—…ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋ฒ„์ „ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉ.

๋ฒ„์ „๊ด€๋ฆฌ๋ฅผ ์ผ๊ด€๋˜๊ฒŒ ์กฐ์œจํ•˜์ง€ ์•Š์œผ๋ฉด ๋น„ํšจ์œจ์„ฑ, ๋ณ‘๋ชฉ ํ˜„์ƒ, ์ฝ”๋“œ ์†์‹ค, ์žฌ์ž‘์—… ๋“ฑ ๋งŽ์€ ๊ณ ์ถฉ์— ์ง๋ฉดํ•  ์ˆ˜ ์žˆ์Œ.

 

[๋ฒ„์ „ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์˜ ๊ธฐ๋Šฅ 3๊ฐ€์ง€]

1) ๋ฐฑ์—… ์Šคํ† ๋ฆฌ์ง€

2) ์ˆ˜์ • ์‚ฌํ•ญ ์ถ”์ 

3) ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ

 

[๋ฒ„์ „ ๊ด€๋ฆฌ์˜ ์ž‘๋™ ๋ฐฉ์‹]

1) ๋ณ€๊ฒฝ ์‚ฌํ•ญ ํ‘ธ์‰ฌ ๋ฐ ํ’€

2) ๋ณ€๊ฒฝ ์‚ฌํ•ญ ๋ณ‘ํ•ฉ

3) ์ด์ „ ๋ฒ„์ „์œผ๋กœ ๋˜๋Œ๋ฆฌ๊ธฐ.

 

์˜ˆ์‹œ ํ€ด์ฆˆ)

์ •๋‹ต) 4๋ฒˆ. ์ €์žฅ์†Œ์˜ ์—ฌ๋Ÿฌ ๋ธŒ๋žœ์น˜์—์„œ ๋™์ผํ•œ ํŒŒ์ผ์˜ ๊ฐ™์€ ๋ผ์ธ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ.

 

3. ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์›์น™

์„œ๋กœ ๊ด€๋ จ์žˆ๋Š” ๋ฉ”์„œ๋“œ์™€ ๋ณ€์ˆ˜๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ.

 

[๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ 4๊ฐ€์ง€ ์›์น™]

1) ์ถ”์ƒํ™”: ๋‹ค๋ฅดํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋ณผ ์ˆ˜ ์žˆ๋Š” ์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ณต์žกํ•œ ์ฝ”๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋จธ์—๊ฒŒ ์‹ค์ œ๋กœ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋งŒ ๋…ธ์ถœํ•˜๋Š” ํ”„๋กœ์„ธ์Šค.

2) ์ƒ์†: ์ž์‹ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ณธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ํ”„๋กœ์„ธ์Šค.

์ž์‹ ํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค(๊ธฐ๋ณธ ํด๋ž˜์Šค)์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์ž๋™์œผ๋กœ ๊ฐ€์ ธ์˜ด.

3) ๋‹คํ˜•์„ฑ: ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ์ƒ์†๋œ ์ฝ”๋“œ๋ฅผ ์ปค์Šคํ…€ ํ•  ์ˆ˜ ์žˆ์Œ.

๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ž์‹ ํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋“œํ•ด์„œ ์ปค์Šคํ…€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Œ.

4) ์บก์Šํ™”: ์ฝ”๋“œ ๊ฐ„๊ฒฐํ™” ๋ฐ ์•ˆ์ •์„ฑ์„ ์œ„ํ•ด ์ ‘๊ทผ์„ฑ์„ ์ œ์–ดํ•˜๋Š” ๊ฒƒ. 

 

4. ์”ฌ ํ”Œ๋กœ ์ƒ์„ฑ

using UnityEngine.SceneManagement;

๋„ค์ž„์ŠคํŽ˜์ด์Šค ์ถ”๊ฐ€

public void StartNew(){
    SceneManager.LoadScene(1);
}

์ƒˆ๋กœ์šด ์”ฌ ํ˜ธ์ถœ.

LoadScene(์”ฌ ์ธ๋ฑ์Šค) ํ•จ์ˆ˜ ์•ˆ์˜ ์ธ์ž๋Š” Build Settings์— ์žˆ๋Š” ์”ฌ ์ธ๋ฑ์Šค ์ด๋‹ค.

Build Settings ์ฐฝ์€ File > Build Settings์— ์žˆ๋‹ค.

 

์˜ˆ์‹œ๋ฌธํ•ญ)

* ์กฐ๊ฑด๋ถ€ ์ปดํŒŒ์ผ

#if UNITY_EDITOR
using UnityEditor;
#endif

public void Exit()
    {
#if UNITY_EDITOR
        EditorApplication.ExitPlaymode();
#else
        Application.Quit(); // Unity ํ”Œ๋ ˆ์ด์–ด๋ฅผ ์ข…๋ฃŒํ•˜๋Š” ์›๋ณธ ์ฝ”๋“œ
#endif
    }

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ ํ•จ์ˆ˜์ธ Application.Quit() ๊ฐ€ ์—๋””ํ„ฐ์ƒ์—์„œ๋Š” ์•ˆ๋จน๋Š”๋‹ค.

์œ„์™€ ๊ฐ™์ด ์กฐ๊ฑด๋ถ€ ์ปดํŒŒ์ผ์„ ์‚ฌ์šฉํ•ด์„œ Editor์ผ๋•Œ๋Š” EditorApplication.ExitPlaymode() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ข…๋ฃŒํ•ด์•ผ ํ•œ๋‹ค.

์ฝ˜์†”์— 'The name 'EditorApplication' does not exist in the current context'๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋‚˜ํƒ€๋‚˜๋ฉด UnityEditor ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์ž.

 

5. ์”ฌ๊ฐ„ ๋ฐ์ดํ„ฐ ์ง€์†์„ฑ ๊ตฌํ˜„

public class MainManager : MonoBehaviour{

    public static MainManager Instance;

    private void Awake(){
        if(Instance == null) Instance = this;
        DontDestroyOnLoad(gameObject);
    }
}

DontDestroyOnLoad ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

static ํ‚ค์›Œ๋“œ๋Š” ์ด ํด๋ž˜์Šค ๋ฉค๋ฒ„์— ์ €์žฅ๋œ ๊ฐ’์ด ํ•ด๋‹น ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์—์„œ ๊ณต์œ ๋จ์„ ์˜๋ฏธํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ํ•œ ์”ฌ์— MainManager Instance๊ฐ€ 10๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ, ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์— ์ €์žฅ๋œ ๊ฐ’์ด ๋™์ผํ•˜๋‹ค. ํ•˜๋‚˜๋ผ๋„ ๋ณ€๊ฒฝ๋˜๋ฉด ๋‚˜๋จธ์ง€ 9๊ฐœ๋„ ๋ณ€๊ฒฝ๋œ๋‹ค.

 

์ด ๋•Œ, Awake() ํ•จ์ˆ˜ ์•ˆ์—์„œ if(Instance == null) ๊ตฌ๋ฌธ์ด ์—†์„ ๊ฒฝ์šฐ, ์”ฌ์„ ๋‹ค์‹œ ๋Œ์•„์˜ฌ ๋•Œ๋งˆ๋‹ค ๊ณ„์† MainManger ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ์”ฌ์„ ์žฌ๋กœ๋“œํ–ˆ์„ ๋•Œ MainManger ๊ฐ€ ์žˆ์œผ๋ฉด ๋˜ ์ƒ์„ฑํ•˜์ง€ ์•Š๋„๋ก ์กฐ๊ฑด๋ฌธ์„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

์ด๊ฒŒ ๋ฐ”๋กœ ์‹ฑ๊ธ€ํ†ค์ด๋‹ค.

 

6. ์„ธ์…˜ ๊ฐ„ ๋ฐ์ดํ„ฐ ์ง€์†์„ฑ ๊ตฌํ˜„

{
  "name": "John",
  "age": 27,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York"
  }
  "pet": [“dog”, “cat”]
}

์„ธ์…˜ ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์ง€ํ•˜๋ ค๋ฉด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค.

๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ๊ฐ€๋Šฅํ•œ ํ˜•์‹์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์„ Serialization(์ง๋ ฌํ™”), ์ €์žฅ๋œ ํ˜•์‹์„ ๋ฐ์ดํ„ฐ๋กœ ๋ฐ”๊พธ๋Š” ๊ฑธ Deserialization(์—ญ์ง๋ ฌํ™”)๋ผ๊ณ  ํ•œ๋‹ค.

 

Unity์—์„œ ๋ฐ์ดํ„ฐ ์ €์žฅ์— ํ”ํžˆ ์‚ฌ์šฉ๋˜๋Š” Json ํ˜•์‹์€ JavaScript Object Notation ์˜ ์•ฝ์ž์ด๋‹ค.

Key : Value ํŽ˜์–ด๋กœ ๋™์ž‘ํ•œ๋‹ค.

Obejct๋Š” {} ์ค‘๊ด„ํ˜ธ๋กœ, ๋ฐฐ์—ด์€ [] ๋Œ€๊ด„ํ˜ธ๋กœ ๋ฌถ์ธ๋‹ค.

 

Unity์—๋Š” JsonUtility๋ผ๋Š” ํ—ฌํผ ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค.

JsonUtility๋Š” ์ง๋ ฌํ™” ๊ฐ€๋Šฅํ•œ ์œ ํ˜•์ธ MonoBehaviour ๋˜๋Š” [Serializable] ์†์„ฑ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ž‘์„ฑํ•˜๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค/๊ตฌ์กฐ์ฒด์—์„œ๋งŒ ์ž‘๋™ํ•œ๋‹ค.

JsonUtility๋Š” ๊ธฐ๋ณธํ˜•, ๋ฐฐ์—ด, ๋ฆฌ์ŠคํŠธ, ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค.

[Serializable]
public class PlayerData
{
    public int level;
    public Vector3 position;
    public string playerName;
}

์œ„์™€ ๊ฐ™์€ PlayerData ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค.

PlayerData myData = new PlayerData();
myData.level = 1;
myData.position = new Vector3(3.0f, 4.4f, 2.3f);
myData.playerName = "John";

์œ„์ฒ˜๋Ÿผ playerData ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค. 

string json = JsonUtility.ToJson(myData);

ToJson ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด myData ๊ฐ์ฒด๊ฐ€ ์•„๋ž˜์ฒ˜๋Ÿผ json ํ˜•์‹์œผ๋กœ ๋ฐ”๋€๋‹ค.

    “level”: 1,
    “position”: {
        “x” : 3.0,
        “y” : 4.4,
        “z” : 2.3 },
    “playerName”: “John”
PlayerData myData = JsonUtility.FromJson<PlayerData>(json);

๋ฐ˜๋Œ€๋กœ FromJson ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด myData๊ฐ€ JSON ๋ฌธ์ž์—ด์˜ ๊ฐ’์œผ๋กœ ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค.

 

//File์„ ์ด์šฉํ•œ json ํŒŒ์ผ ์ €์žฅ ๋ฐ ๋กœ๋“œ ์ฝ”๋“œ ์˜ˆ์‹œ

using System.IO;

[System.Serializable]
class SaveData{
    public Color TeamColor;
}

public void SaveColor(){
    SaveData data = new SaveData();
    data.TeamColor = TeamColor;

    string json = JsonUtility.ToJson(data);
  
    File.WriteAllText(Application.persistentDataPath + "/savefile.json", json);
}

public void LoadColor(){
    string path = Application.persistentDataPath + "/savefile.json";
    if (File.Exists(path)){
        string json = File.ReadAllText(path);
        SaveData data = JsonUtility.FromJson<SaveData>(json);

        TeamColor = data.TeamColor;
    }
}

๊ฐ์ฒด์ง€ํ–ฅ ์›์น™ ์ ์šฉ

 

์ฃผ๋‹ˆ์–ด ํ”„๋กœ๊ทธ๋ž˜๋จธ: ๊ฐ์ฒด ์ง€ํ–ฅ ์›์น™ ์ ์šฉ - Unity Learn

์ด ๋ฏธ์…˜์—์„œ๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์›์น™์„ ๊ฐ€์ด๋“œ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ด์ „ ๋ฏธ์…˜์—์„œ ์™„๋ฃŒํ•œ ์ธ๋ฒคํ† ๋ฆฌ ํ”„๋กœ์ ํŠธ๋ฅผ ๋ฆฌํŒฉํ„ฐ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์›์น™์˜ ๋‚ด๋ถ€ ์ž‘๋™ ๋ฐฉ์‹์„ ๊ฐœ๋ณ„์ ์œผ๋กœ ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ฉด ์• ํ”Œ

learn.unity.com

 

1. ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์ถ”์ƒํ™”

*์ถ”์ƒํ™”: ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋ณผ ์ˆ˜ ์žˆ๋Š” ์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ณต์žกํ•œ ์ฝ”๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์‹ค์ œ๋กœ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋งŒ ๋…ธ์ถœํ•˜๋Š” ํ”„๋กœ์„ธ์Šค.

๋ณต์žกํ•œ ๋‚ด๋ถ€ ์ž‘๋™ ๋ฐฉ์‹์„ ์ถ”์ƒ์ ์ด๋ฉด์„œ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ์„ฑ์ด ๋” ๋†’์€ ์ฝ”๋“œ๋กœ ๋Œ€์ฒดํ•œ๋‹ค.

 

์˜ˆ๋กœ, 4๋‹จ์› ๊ฒŒ์ž„ํ”Œ๋ ˆ์ด ๋ฉ”์นด๋‹‰ ํ”„๋กœ์ ํŠธ(๊ณต๋†€์ด)์—์„œ Enemy ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž.

private void Start(){
    for (int i = 0; i < 3; i++){
        Instantiate(enemyPrefab, GenerateSpawnPosition(),   
        enemyPrefab.transform.rotation);
    }
}

์œ„์™€ ๊ฐ™์ด Start() ๋ฌธ์— Enemy ๊ฐ์ฒด ์ƒ์„ฑ์ฝ”๋“œ๋ฅผ ๋„ฃ์œผ๋ฉด ์‹คํ–‰์‹œ 1๋ฒˆ๋งŒ ์‹คํ–‰๋˜๊ณ , ๋‹ค์‹œ ์ƒ์„ฑํ•˜๋ ค๋ฉด ๋‹ค๋ฅธ ํ•จ์ˆ˜์— ์ € ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•ด์•ผํ•œ๋‹ค. ์ค‘๋ณต์ฝ”๋“œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉฐ ๋น„ํšจ์œจ์ ์ด๋‹ค.

// ๋” ๋†’์€ ์ˆ˜์ค€์˜ ๋ฉ”์„œ๋“œ ์ƒˆ๋กœ ์ƒ์„ฑ 
void SpawnEnemyWave() {
    for (int i = 0; i < 3; i++){
        Instantiate(enemyPrefab, GenerateSpawnPosition(), 
        enemyPrefab.transform.rotation);
    }
}

private void Start(){
    SpawnEnemyWave(); // Start()๋กœ ๋” ๋†’์€ ์ˆ˜์ค€์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
}

 ์œ„์™€ ๊ฐ™์ด ์ค‘๋ณต๋  ์ฝ”๋“œ๋ฅผ SpawnEnemyWave() ํ•จ์ˆ˜๋ผ๋Š” ๋” ๋†’์€ ์ˆ˜์ค€์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋ฉด Enemy ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ด ํ•„์š”ํ•  ๋•Œ, ํ•จ์ˆ˜ ํ•˜๋‚˜๋งŒ ํ˜ธ์ถœํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๋‹ค๋ฅธ ๊ณ ๋ฏผ์€ ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ์ˆ˜์ •์‹œ์—๋„ SpawnEnemyWave() ํ•จ์ˆ˜ ์•ˆ์—์„œ ํ•œ๋ฒˆ๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค. ๊ตฌํ˜„์ž‘์—…์ด ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ฐœ์„ ๋˜์—ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ž‘์—…์„ ์ถ”์ƒํ™”๋ผ๊ณ  ํ•œ๋‹ค.

 

2. ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์ƒ์†๊ณผ ๋‹คํ˜•์„ฑ

*์ƒ์†: ๊ธฐ๋ณธ ํด๋ž˜์Šค(๋ถ€๋ชจ ํด๋ž˜์Šค)๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ”„๋กœ์„ธ์Šค. ์ด ๊ธฐ๋ณธ ํด๋ž˜์Šค๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹ค๋ฅธ ํด๋ž˜์Šค(์ž์‹ ํด๋ž˜์Šค)๋ฅผ ๋งŒ๋“ฌ.

์ž์‹ ํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์ž๋™์œผ๋กœ ๊ฐ€์ ธ์˜ค๋ฉฐ ์ด๋ฅผ ์ƒ์†์ด๋ผ ํ•จ.

public class SomeClass : MonoBehaviour { }

Unity์—์„œ MonoBehaviour๊ฐ€ ๊ธฐ๋ณธ ํด๋ž˜์Šค ์—ญํ• ์ž„. MonoBehaviour๋ฅผ ์ƒ์†๋ฐ›๊ธฐ์— OnTriggerEnter(), GetComponent, Start(), Update() ๋“ฑ์˜ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ.

 

*๋‹คํ˜•์„ฑ: ๋ถ€๋ชจ์—๊ฒŒ ์ƒ์†๋ฐ›์€ ๊ธฐ๋Šฅ์„ ๋ณ€๊ฒฝํ•˜์—ฌ ์ปค์Šคํ…€ ํ•˜๋Š” ๊ฒƒ.

public class Enemy : MonoBehaviour { 
    public void DealDamage () {
        Player.Health -= 10;
    }
}
public class Thief : Enemy {
    private void Update() {
        if (Player.isSeen) {
            DealDamage(); // ๋ถ€๋ชจ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Œ
        }
    }
}

Thief ํด๋ž˜์Šค์˜ Update()๋ฌธ์—์„œ ํ˜ธ์ถœํ•œ DealDamage() ํ•จ์ˆ˜๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค Enemy ์˜ DealDamage() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

๋ถ€๋ชจ ํด๋ž˜์Šค์— ์ž‘์„ฑ๋œ ํ•จ์ˆ˜๋ฅผ ๊ณ ๋Œ€๋กœ ์‚ฌ์šฉ ์•ˆํ•˜๊ณ  ์ปค์Šคํ…€ํ•˜๊ธฐ ์œ„ํ•ด ์žฌ์„ ์–ธ ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ.

public class Enemy : MonoBehaviour { 
    public virtual void DealDamage () { // ๊ฐ€์ƒ ํ‚ค์›Œ๋“œ๊ฐ€ ์˜ค๋ฒ„๋ผ์ด๋“œ๋ฅผ ํ—ˆ์šฉํ•จ
        Player.Health -= 10;
    }
}

๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ virtual ํ‚ค์›Œ๋“œ๋กœ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ virtual๋กœ ์‹๋ณ„ํ•˜๋ฉด ์˜ค๋ฒ„๋ผ์ด๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‚˜ ํ•„์ˆ˜๋Š” ์•„๋‹ˆ๊ฒŒ ๋œ๋‹ค.

์ฐธ๊ณ ๋กœ ์˜ค๋ฒ„๋ผ์ด๋“œ๊ฐ€ ์„ ํƒ ์‚ฌํ•ญ์ธ virtual ๋ฉ”์„œ๋“œ์™€ ๋‹ฌ๋ฆฌ, abstract ํ‘œ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ๋ฐ˜๋“œ์‹œ ์˜ค๋ฒ„๋ผ์ด๋“œ๋˜์–ด์•ผ ํ•œ๋‹ค.

public class Thief : Enemy {

    // ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๊ฐ€์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•  ์ˆ˜ ์žˆ์Œ
    public override void DealDamage() {
        Player.Health -= 2;
        CommitPettyTheft();
    }
    
    private void Update() {
        if (Player.isSeen) {
            DealDamage();
        }
    }
}

๊ทธ๋Ÿฌ๋ฉด ์ž์‹ ํด๋ž˜์Šค์—์„œ override ํ‚ค์›Œ๋“œ๋กœ DealDamage() ํ•จ์ˆ˜๋ฅผ ์žฌ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด ๋•Œ, ์ž์‹ํด๋ž˜์Šค์˜ Update()๋ฌธ์—์„œ DealDamage()๋Š” Thief ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ override ํ•œ DealDamage() ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

 

๋งŒ์•ฝ base ๋ ˆ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜์—ฌ base.DealDamage(); ๋กœ ํ˜ธ์ถœํ–ˆ๋‹ค๋ฉด, ์›๋ž˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ผ๋Š” ๋œป์ด ๋œ๋‹ค.

 

public virtual void GoTo(Building target){
    m_Target = target;

    if (m_Target != null) {
        m_Agent.SetDestination(m_Target.transform.position);
        m_Agent.isStopped = false;
    }
}

public virtual void GoTo(Vector3 position){
    m_Target = null;
    m_Agent.SetDestination(position);
    m_Agent.isStopped = false;
}

๋˜ํ•œ, ์ƒ์†/๋‹คํ˜•์„ฑ์˜ ๊ฒฝ์šฐ ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋ฅผ ๋‹ฌ๋ฆฌํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

public void Translate(Vector3 translation); 
// transform.Translate(Vector3.forward);๋กœ ๊ตฌํ˜„๋จ

public void Translate(float x, float y, float z); 
// transform.Translate(0, 0, 1);๋กœ ๊ตฌํ˜„๋จ

public void Translate(Vector3 translation, Transform relativeTo);
// transform.Translate(Vector3.forward, Space.Self);๋กœ ๊ตฌํ˜„๋จ

public void Translate(float x, float y, float z, Transform relativeTo); 
// transform.Translate(0, 0, 1, Space.Self);๋กœ ๊ตฌํ˜„๋จ

Unity Rigidbody ์˜ Translate ํ•จ์ˆ˜๊ฐ€ ์ธ์ž๋งŒ ๋‹ค๋ฅด๊ณ  ์—ฌ๋Ÿฌ๊ฐœ ์ •์˜๋˜์–ด ์žˆ๋Š” ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด ํ™• ์™€๋‹ฟ์„ ๊ฒƒ์ด๋‹ค.

 

์˜ˆ์‹œ๋ฌธํ•ญ)

 

* ํ™•์žฅ๋ฉ”์„œ๋“œ

: ๊ธฐ์กด ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•˜๋Š” ๊ธฐ๋ฒ•. ์ƒ์†๊ณผ๋Š” ๋‹ค๋ฆ„

ํด๋ž˜์Šค์˜ ์™ธ๋ถ€์—์„œ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒˆ๋กœ์šด ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ๊ธฐ๋Šฅ.

ํ™•์žฅ๋ฉ”์„œ๋“œ๋Š” static ํด๋ž˜์Šค์—์„œ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค.

public class OriginClass {
  public void Print1(){
    Console.WriteLine("Print1() Method Call");
  }
  public void Print2(){
    Console.WriteLine("Print2() Method Call");
  }
  public void Print3(){
    Console.WriteLine("Print3() Method Call");
  }
}

public static class ExtensionMethod {
  public static void Print4(this OriginClass O){
    Console.WriteLine("Print4() Method Call");
  }
  public static void Print5(this OriginClass O, String str){
    Console.WriteLine("Print5() Method Call");
    Console.WriteLine("Print5() Param: " + str);
  }
}

class Program{
  static void Main(string[] args){
    OriginClass originClass = new OriginClass();
    originClass.Print4();
    originClass.Print5("Hello!");
  }
}

์ถœ์ฒ˜: https://developer-talk.tistory.com/477

 

์˜ˆ์‹œ๋ฌธํ•ญ)

 

*์ธํ„ฐํŽ˜์ด์Šค

: ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ๋ชธ์ฒด์—†๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ํด๋ž˜์Šค๋“ค์—์„œ ์ƒ์†๋ฐ›์•„์„œ ์‚ฌ์šฉํ•œ๋‹ค.

: ๋‹จ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๋“ค์€ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ฉค๋ฒ„๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

: ์ถ”์ƒ ํด๋ž˜์Šค์™€์˜ ์ฐจ์ด์ ์€ ์ถ”์ƒํด๋ž˜์Šค๋Š” ๋‹ค์ค‘์ƒ์†์„ ์ง€์›ํ•˜์ง€ ์•Š์ง€๋งŒ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋‹ค์ค‘์ƒ์†์„ ์ง€์›ํ•˜๋ฉฐ, ์ธํ„ฐํŽ˜์ด์Šค ์ž์ฒด๋กœ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์ด ์•ˆ๋œ๋‹ค.

namespace InterfaceEx{

    class Program {
        static void Main(string[] args) {
            Person myPerson = new Person();
            Lion myLion = new Lion();
            Camel myCamel = new Camel();

            myPerson.Eat();
            myLion.Eat();
            myCamel.Eat();

            Console.WriteLine();

            MyEatFunc(myPerson);
            MyEatFunc(myLion);
            MyEatFunc(myCamel);
        }

        private static void MyEatFunc(object obj) {
            IAnimal target = obj as IAnimal;
            if (target != null) {
                target.Eat();
            }
        }
    }
    
    class Person : IAnimal{
        public void Eat(){ Console.WriteLine("๋ฐฅ์„ ๋จน์Šต๋‹ˆ๋‹ค."); }
    }

    class Lion : IAnimal{
        public void Eat() { Console.WriteLine("๊ณ ๊ธฐ๋ฅผ ๋จน์Šต๋‹ˆ๋‹ค."); }
    }

    class Camel {
        public void Eat(){  Console.WriteLine("ํ’€์„ ๋จน์Šต๋‹ˆ๋‹ค."); }
    }

    interface IAnimal{
        void Eat();
    }
}

์ถœ์ฒ˜: https://frozenpond.tistory.com/27

 

=> ์‹คํ–‰๊ฒฐ๊ณผ:

๋ฐฅ์„ ๋จน์Šต๋‹ˆ๋‹ค.

๊ณ ๊ธฐ๋ฅผ ๋จน์Šต๋‹ˆ๋‹ค.

ํ’€์„ ๋จน์Šต๋‹ˆ๋‹ค.

 

๋ฐฅ์„ ๋จน์Šต๋‹ˆ๋‹ค.

๊ณ ๊ธฐ๋ฅผ ๋จน์Šต๋‹ˆ๋‹ค.

 

=> ํ•ด์„:

MyEatFunc(object obj) ํ•จ์ˆ˜์—์„œ myCamel์€ IAnimal ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— target์ด ๋˜์ง€ ์•Š์•„์„œ Eat() ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

3. ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์บก์Šํ™”

์ฝ”๋“œ์˜ ์•ˆ์ •์„ฑ์„ ์œ„ํ•ด ์•ก์„ธ์Šค ๊ถŒํ•œ์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐ์‹ธ๋Š” ๊ฒƒ.

์ฝ”๋“œ๊ฐ€ ์›๋ž˜ ์˜๋„ํ•œ ๋Œ€๋กœ๋งŒ ์‚ฌ์šฉ๋˜๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์กฐ์ž‘ํ•˜๋Š” ๊ฐ’๊ณผ ๋ฐ์ดํ„ฐ๊ฐ€ ์†์ƒ๋˜์ง€ ์•Š๋„๋ก ๋ณด์žฅํ•˜๋Š” ํ”„๋กœ์„ธ์Šค.

*์ ‘๊ทผ์ œ์–ด์ž: private, public, protected, default

 

1) private ๋ณ€์ˆ˜ ์ธ์ŠคํŽ™ํ„ฐ์— ๋…ธ์ถœ์‹œํ‚ค๊ธฐ

public class Rabbit : MonoBehaviour {
    [SerializeField] // ์ธ์ŠคํŽ™ํ„ฐ์— private ๋ณ€์ˆ˜ ํ‘œ์‹œ
    private Color furColor;
}

์›๋ž˜ private ์ ‘๊ทผ์ œ์–ด์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ธ์ŠคํŽ™ํ„ฐ์— ๋…ธ์ถœ์ด ์•ˆ๋œ๋‹ค.

ํ•˜์ง€๋งŒ, ์ง๋ ฌํ™”๋ฅผ ์ด์šฉํ•ด์„œ [SerializeField] ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ private ๋ณ€์ˆ˜๋ฅผ ์ธ์ŠคํŽ™ํ„ฐ์— ๋…ธ์ถœํ•˜๋ฉด์„œ๋„ ๋‹ค๋ฅด ์ฝ”๋“œ์—์„œ๋Š” ๋…ธ์ถœํ•˜์ง€ ์•Š๋„๋ก ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

2) public ๋ณ€์ˆ˜ ๋ณดํ˜ธํ•˜๊ธฐ

public static MainManager Instance;

public ์ด์–ด์•ผ๋งŒ ํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ ๋ณดํ˜ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ static ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•œ๋‹ค.

ํ•˜์ง€๋งŒ Instance์— ์ ‘๊ทผํ•  get(), se() ํ•จ์ˆ˜๊ฐ€ ์—†๋‹ค๋ฉด MainManager.Instance ํ˜ธ์ถœ ์‹œ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.

public static MainManager Instance
{
    get {return Instance;}
    set {Instance = this;}
}

์œ ๋‹ˆํ‹ฐ์—์„œ๋Š” get, set ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ž๋™์œผ๋กœ get(), set() ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ๋‹ค.

public static MainManager Instance { get; set; } // private ์„ธํ„ฐ ์ถ”๊ฐ€

๋” ๋‚˜์•„๊ฐ€์„œ get, set ํ”„๋กœํผํ‹ฐ ์•ˆ์— return, ๋Œ€์ž… ๊ตฌ๋ฌธ์„ ์•ˆ๋„ฃ์–ด๋„ ๋˜๋Š” ์ž๋™ ๊ตฌํ˜„ ํ”„๋Ÿฌํผํ‹ฐ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

 

3) get, set ์‹œ, ์กฐ๊ฑด ๋ถ€์—ฌํ•˜๊ธฐ

private float m_ProductionSpeed = 0.5f;

public float ProductionSpeed // ์„ธ๋ฏธ์ฝœ๋ก  ์‚ญ์ œ
{
    get { return m_ProductionSpeed; } // ๊ฒŒํ„ฐ์—์„œ ์ง€์› ํ•„๋“œ ๋ฐ˜ํ™˜
    set { m_ProductionSpeed = value; } // ์„ธํ„ฐ์—์„œ ์ง€์› ํ•„๋“œ ์‚ฌ์šฉ
}

์œ„ ์ฒ˜๋Ÿผ public ํ”„๋กœํผํ‹ฐ์— ์•ก์„ธ์Šคํ•  ๋•Œ ์ง€์› ํ•„๋“œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ํ• ๋‹นํ•˜๋Š” get ๋ฐ set ํ•จ์ˆ˜๋ฅผ ์ˆ˜๋™์œผ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

set{
    if (value < 0.0f) {
    	Debug.LogError("You can't set a negative production speed!");
    }
    else{
    	m_ProductionSpeed = value; // ์›๋ž˜ ์„ธํ„ฐ๊ฐ€ ํ˜„์žฌ if/else ๋ฌธ์— ์žˆ์Œ
    }
}

์œ„ ์ฒ˜๋Ÿผ ์กฐ๊ฑด๋ฌธ์„ ์ถ”๊ฐ€ํ•ด์„œ ์ง€์ •ํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.

 

4. ์ฝ”๋“œ ํ”„๋กœํŒŒ์ผ๋ง

*์œ ๋‹ˆํ‹ฐ ํ”„๋กœํŒŒ์ผ๋Ÿฌ: ์Šคํฌ๋ฆฝํŠธ ํ˜ธ์ถœ, ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น, ๋ Œ๋”๋ง ๋“ฑ ํ”„๋กœ์ ํŠธ ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ๋ชจ๋“  ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Window > Analysis > Profiler

ํ”„๋กœํŒŒ์ผ๋Ÿฌ ์‹คํ–‰ ํ™”๋ฉด

์ƒ๋‹จ: CPU ์‚ฌ์šฉ ํ˜„ํ™ฉ์ด ๋‹ด๊ธด ์ฐจํŠธ๊ฐ€ ๋ณด์—ฌ์ง.

ํ•˜๋‹จ: ๊ฐ ํ”„๋ ˆ์ž„์— ์–ด๋–ค ์ผ์ด ๋ฐœ์ƒํ•˜๋Š”์ง€ ์‹œ๊ฐ์ ์œผ๋กœ ์š”์•ฝ๋œ ๋‚ด์šฉ์ด ๋ณด์—ฌ์ง.

 

์œ„ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๋ฉด Scripts์— ์—„์ฒญ๋‚œ ์‹œ๊ฐ„์ด ์†Œ์š”๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Œ. ์ฆ‰ Script์— ๋ฌธ์ œ๊ฐ€ ์žˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋งŽ์Œ.

์ƒ๋‹จ ๊ทธ๋ž˜ํ”„์—์„œ CPU ์‚ฌ์šฉ๋Ÿ‰์ด ๊ธ‰์ฆํ•˜๋Š” ๊ตฌ๊ฐ„์„ ํด๋ฆญํ•ด์„œ ํ•˜๋‹จ์„ ์‚ดํŽด๋ณด๋ฉด CPU: 117.26ms ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Œ

CPU ms๋Š” ํ•ด๋‹น ํ”„๋ ˆ์ž„์˜ ๋ชจ๋“  ์ž‘์—…์„ ์™„๋ฃŒํ•˜๋Š” ๋ฐ ์†Œ์š”๋œ ์ด ์‹œ๊ฐ„(๋ฐ€๋ฆฌ์ดˆ)์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

 

1,000ms / ๋ชฉํ‘œ ์ดˆ๋‹น ํ”„๋ ˆ์ž„ ์ˆ˜ = ๋ฐ€๋ฆฌ์ดˆ ์˜ˆ์‚ฐ

 

๋ชฉํ‘œ ์ดˆ๋‹น ํ”„๋ ˆ์ž„์ˆ˜๊ฐ€ 60FPS์ด๋ผ๋ฉด ๊ฐ ํ”„๋ ˆ์ž„์˜ ๋ฐ€๋ฆฌ์ดˆ ์˜ˆ์‚ฐ์€ 16์ž„. 117.26ms๋Š” ์•ฝ 7๋ฐฐ...

 

* Player Loop: ๊ฒŒ์ž„ ์ž์ฒด์—์„œ ์‹คํ–‰ ์ค‘์ธ ๋ชจ๋“  ์ž‘์—…

* EditorLoop: ์—๋””ํ„ฐ์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋ชจ๋“  ์ž‘์—….

=> ์‹ค์ œ ๊ฒŒ์ž„์€ Editor์—์„œ ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ฏ€๋กœ Player loop๋งŒ ๋ณด๋ฉด ๋จ.

 

์ฝ”๋“œ์˜ ํŠน์ • ์„น์…˜ ํ”„๋กœํŒŒ์ผ๋ง

Profiler.BeginSample("Moving"); // ํ”„๋กœํŒŒ์ผ๋ง ์‹œ์ž‘    
Move();    
Profiler.EndSample(); // ํ”„๋กœํŒŒ์ผ๋ง ์ข…๋ฃŒ

ํŠน์ • ๋ถ€๋ถ„์— Profiler.BeginSample(), Profiler.EndSample() ์„ ์‹คํ–‰ํ•˜๋ฉด ํ”„๋กœํŒŒ์ผ๋Ÿฌ์—์„œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด ๋ถ€๋ถ„์„ ๊ตฌ์ฒด์ ์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

void Move() {
    Vector3 position = transform.position;

    float distanceToCenter = Vector3.Distance(Vector3.zero, position);
    float speed = 0.5f + distanceToCenter / areaSize.magnitude;

    int steps = Random.Range(1000, 2000);
    float increment = Time.deltaTime / steps;
    for (int i = 0; i < steps; ++i) {
        position += currentVelocity * increment * speed;
    }

    transform.position = position;
    transform.position = transform.position + currentVelocity * Time.deltaTime;
}
void Move() {
    transform.position = transform.position + currentVelocity * Time.deltaTime;
}

Moveํ•จ์ˆ˜์˜ ๊ธด ์ฝ”๋“œ๋ฅผ ์ด๋™ํ–‰์œ„๋งŒ ํ•˜๋„๋ก 1์ค„๋กœ ๋ณ€๊ฒฝํ•˜์˜€๋‹ค.

 

์ฝ”๋“œ ๋ณ€๊ฒฝํ›„ ๋‹ค์‹œ ํ”„๋กœํŒŒ์ผ๋Ÿฌ๋ฅผ ๋Œ๋ ค๋ณด์ž.

์ด์ „์—๋Š” Move ์ฝ”๋“œ๋ฅผ ์™„๋ฃŒํ•˜๋Š” ๋ฐ 99.74ms๊ฐ€ ์†Œ์š”๋˜์—‡๋Š”๋ฐ, ์ตœ์ ํ™” ๋ณ€๊ฒฝ ํ›„์—๋Š” ์†Œ์š” ์‹œ๊ฐ„์ด 1.08ms ์ด๋‹ค.

์ด CPU ms๋„ ํƒ€๊ฒŸ์ธ 16ms๋ณด๋‹ค ์งง์€ 14.94ms๋กœ ๋‹จ์ถ•๋˜์—ˆ๋‹ค.

 

 

728x90