ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [게임엔진활용] 4강 미니(사과)게임 제작
    [DSU] 게임엔진활용 2026. 4. 7. 00:07
    728x90

    지난 시간까지 구현한 함수들을 활용해서, 작은 프로젝트로 사과 게임을 제작.

     

    RenderView.cs에서  Start와 파괴자에서 loadGame, freeGame 각각 호출

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public class RenderView : MonoBehaviour
    {
        // ...
     
        void Start()
        {
            // ...
     
            loadGame();
        }
     
        ~RenderView()
        {
            freeGame();
        }
    cs

     

    그리고 OnGUI에서 drawGame을 호출

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
        void OnGUI()
        {
            if (Event.current.type != EventType.Repaint)
                return;
     
            GL.Clear(truetrue, Color.black);
     
    #if false
            GUI.color = Color.white;
            GUI.DrawTexture(new Rect(00, Screen.width, Screen.height),
                mc.rt, ScaleMode.ScaleToFit);//StretchToFill
    #else
            Material m = material[materialIndex];
            m.SetColor("_BaseColor", Color.white);
            m.SetTexture("_BaseMap", mc.rt);
            m.SetFloat("size", mozicSize);
            m.SetFloat("iTime", playbackTime);
            playbackTime += Time.deltaTime;
            m.SetVector("iMouse"new Vector4(0000));
     
            Graphics.DrawTexture(mc.viewport, mc.rt, m);
    #endif
            RenderTexture texBack = RenderTexture.active;
            RenderTexture.active = texGame;// mc.rt;
            Matrix4x4 matBack = GUI.matrix;
            GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one);
     
            GL.Clear(truetrue, Color.clear);
            drawGame(Time.deltaTime);// do something ...
     
            RenderTexture.active = texBack;
            GUI.matrix = matBack;
            GUI.matrix = Matrix4x4.TRS(
                new Vector3(mc.viewport.x, mc.viewport.y, 0),
                Quaternion.identity,
                new Vector3((float)(Screen.width - mc.viewport.x * 2/ mc.devWidth,
                            (float)(Screen.height - mc.viewport.y * 2/ mc.devHeight, 1.0f));
            drawImage(texGame, 00, TOP | LEFT);//mc.rt
        }
    cs

     

    MainCamera.cs에서 mouseGame 호출

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
     
        void Update()
        {
            // ...
     
            if (Mouse.current.leftButton.wasPressedThisFrame)
            {
                Vector2 p = convertPosition(Mouse.current.position.value);
                rv.mouseGame(0, p.x, p.y);
                //drag = true;
            }
            else if (Mouse.current.leftButton.wasReleasedThisFrame)
            {
                Vector2 p = convertPosition(Mouse.current.position.value);
                rv.mouseGame(2, p.x, p.y);
                //drag = false;
            }
            else// if( drag )
            {
                Vector2 p = convertPosition(Mouse.current.position.value);
                rv.mouseGame(1, p.x, p.y);
            }
        }
        //bool drag = false;
     
        Vector2 convertPosition(Vector2 p)
        {
            p.x = devWidth * (p.x - viewport.x) / viewport.width;
            p.y = devHeight * (1f - (p.y - viewport.y) / viewport.height);
            //Debug.LogFormat($"({p.x}, {p.y})");
            return p;
        }
     
     
    cs

     

     

    사과 게임 메인 로직은 title과 inGame으로 구분.

     

    1. title

     - 배경이미지와 Play버튼 구성

     

    2. inGame

     - 랜덤(1~9) 값을 가진 사과가 총 15 x 10개가 있으며, 드래그시 선택된 사과의 숫자 합이 10이면 포함된 사과는 모두 제거함.

     - 제한된 시간 2분 동안 최대한 많은 사과를 제거하는 것이 목표이며, 사과 하나 제거시 점수 1점 획득.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
        int gameState;// 0:title, 1:inGame
     
        void loadGame()
        {
            gameState = 0;
     
            loadTitle();
        }
     
        void freeGame()
        {
            switch (gameState)
            {
                case 0: freeTitle(); break;
                case 1: freeIngame(); break;
            }
        }
     
        void drawGame(float dt)
        {
            switch (gameState)
            {
                case 0: drawTitle(dt); break;
                case 1: drawIngame(dt); break;
            }
        }
     
        public void mouseGame(int state, float x, float y)
        {
            switch (gameState)
            {
                case 0: mouseTitle(state, x, y); break;
                case 1: mouseIngame(state, x, y); break;
            }
        }
     
     
        // backgroud, button(play)
        Texture texLogo;
        struct Button
        {
            public Rect rt;
            public string s;
            public bool pressed;
            public Color cb;
            public Color ch;
            public Color cf;
            public float size;
        }
        Button btnPlay;
     
        void loadTitle()
        {
            texLogo = createImage("logo");
     
            Button b = new Button();
            b.rt = new Rect(1010400300);
            b.s = "Play";
            b.pressed = false;
            b.cb = Color.red;// 기본
            b.ch = Color.blue;// 커서
            b.cf = Color.white;
            b.size = 300;
            btnPlay = b;
     
        }
     
        void freeTitle()
        {
     
        }
     
        void drawButton(Button b)
        {
            Rect rt = b.rt;
     
            Color c = b.pressed ? b.ch : b.cb;
            setRGBA(c.r, c.g, c.b, c.a);
            fillRect(rt.x, rt.y, rt.width, rt.height);
            setRGBA(1111);
     
            setStringName("굴림");
            float stringSize = b.size;
            setStringSize(stringSize);
            Vector2 size = sizeOfString(b.s);
            if (size.x > rt.width)
                setStringSize(stringSize * rt.width / size.x);
            setStringRGBA(b.cf.r, b.cf.g, b.cf.b, b.cf.a);
            drawString(b.s, rt.x + rt.width / 2,
                                    rt.y + rt.height / 2, VCENTER | HCENTER);
        }
     
        void drawTitle(float dt)
        {
            float rx = (float)mc.devWidth / texLogo.width;
            float ry = (float)mc.devHeight / texLogo.height;
            drawImage(texLogo, 0000, texLogo.width, texLogo.height,
                TOP | LEFT, rx, ry);
     
            drawButton(btnPlay);
        }
     
        bool containPoint(float x, float y, Rect rt)
        {
            if (x < rt.x ||
                x > rt.x + rt.width ||
                y < rt.y ||
                y > rt.y + rt.height)
                return false;
            return true;
        }
        bool containRect(Rect rt0, Rect rt1)
        {
            if (rt0.x + rt0.width < rt1.x ||
                rt0.x > rt1.x + rt1.width ||
                rt0.y + rt0.height < rt1.y ||
                rt0.y > rt1.y + rt1.height)
                return false;
            return true;
        }
     
        public void mouseTitle(int state, float x, float y)
        {
            switch (state)
            {
                case 0:
                    if (btnPlay.pressed)
                    {
                        gameState = 1;
                        freeTitle();
                        loadIngame();
                    }
                    break;
                case 1:
                    btnPlay.pressed = containPoint(x, y, btnPlay.rt);
                    break;
            }
     
        }
     
        Button[] apple;
        int score;
        float playTime, _playTime;
     
        bool gameOver;
        Button[] btnResult;
        public static Color cGreen = new Color(0.1f, 0.5f, 0.0f, 1.0f);
     
        void loadIngame()
        {
            // 60 * 15 
            float x = (mc.devWidth - 60 * 15/ 2;
            float y = (mc.devHeight - 60 * 10/ 2;
     
            apple = new Button[15 * 10];
     
            for (int i = 0; i < apple.Length; i++)
            {
                Button b = new Button();
                b.s = "" + UnityEngine.Random.Range(110);
                b.rt = new Rect(x + 60 * (i % 15), y + 60 * (i / 15), 5050);
                b.cb = cGreen;//Color.green;
                b.ch = Color.yellow;
                b.cf = Color.white;
                b.pressed = false;
                b.size = 30;
                apple[i] = b;
            }
     
            score = 0;
            _playTime = 120f;
            playTime = 120f;
     
            gameOver = false;
            btnResult = new Button[2];
            string[] s = new string[2] { "다시시작""나가기" };
            for (int i = 0; i < btnResult.Length; i++)
            {
                Button b = new Button();
                b.s = s[i];
                b.rt = new Rect(500 + 120 * i, 35010050);
                b.cb = Color.blue;
                b.ch = Color.black;
                b.cf = Color.white;
                b.pressed = false;
                b.size = 30;
                btnResult[i] = b;
            }
        }
     
        void freeIngame()
        {
     
        }
     
        void drawIngame(float dt)
        {
            for (int i = 0; i < apple.Length; i++)
            {
                if (apple[i].s == "0")
                    continue;
                drawButton(apple[i]);
            }
     
            if (drag)
            {
                setRGBA(0010.5f);
                Rect rt = dragRect();
                fillRect(rt.x, rt.y, rt.width, rt.height);
                setRGBA(1111);
                //setLineWidth(1);
                //drawRect(rt.x, rt.y, rt.width, rt.height);
            }
     
            setStringName("고딕체");
            setStringSize(30);
            setStringRGBA(1001);
            drawString("Score : " + score, mc.devWidth - 1010, TOP | RIGHT);
     
            setRGBA(0101);
            float h = mc.devHeight - 100;
            float dh = h * playTime / _playTime;
            fillRect(mc.devWidth - 2050 + h - dh, 5, dh);
            drawRect(mc.devWidth - 20505, h);
            setRGBA(1111);
     
            playTime -= dt;
            if (playTime <= 0f)
            {
                gameOver = true;
            }
     
            if (gameOver)
            {
                setStringName("고딕체");
                setStringSize(100);
                setStringRGBA(1001);
                drawString("Score : " + score, 400200, TOP | LEFT);
     
                for (int i = 0; i < btnResult.Length; i++)
                    drawButton(btnResult[i]);
            }
        }
     
        bool drag = false;
        float sx, sy, ex, ey;
        Rect dragRect()
        {
            float x0, x1, y0, y1;
            if (sx < ex) { x0 = sx; x1 = ex; }
            else         { x0 = ex; x1 = sx; }
            if (sy < ey) { y0 = sy; y1 = ey; }
            else         { y0 = ey; y1 = sy; }
            return new Rect(x0, y0, x1 - x0 - 1, y1 - y0 - 1);
        }
     
        public void mouseIngame(int state, float x, float y)
        {
            if (mouseGameOver(state, x, y))
                return;
     
            if (state == 0)
            {
                sx = ex = x;
                sy = ey = y;
                drag = true;
            }
            else if (state == 2)
            {
                drag = false;
     
                int sum = 0;
                for (int i = 0; i < apple.Length; i++)
                {
                    if (apple[i].pressed)
                    {
                        int n = Int32.Parse(apple[i].s);
                        sum += n;
                    }
                }
                if (sum == 10)
                {
                    Debug.Log("제거하자");
                    for (int i = 0; i < apple.Length; i++)
                    {
                        if (apple[i].pressed)
                        {
                            apple[i].s = "0";
                            score++;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < apple.Length; i++)
                    {
                        if (apple[i].pressed)
                            apple[i].pressed = false;
                    }
                }
            }
            else if (drag)
            {
                ex = x;
                ey = y;
     
                Rect rt = dragRect();
     
                for (int i = 0; i < apple.Length; i++)
                    apple[i].pressed = false;
                for (int i = 0; i < apple.Length; i++)
                {
                    if (containRect(rt, apple[i].rt))
                    {
                        if(apple[i].s != "0")
                            apple[i].pressed = true;
                    }
                }
            }
        }
     
        bool mouseGameOver(int state, float x, float y)
        {
            if (gameOver == false)
                return false;
     
            switch (state)
            {
                case 0:
                    for (int i = 0; i < btnResult.Length; i++)
                    {
                        if (btnResult[i].pressed)
                        {
                            if (i == 0)
                            {
                                // 다시시작
                                for (int j = 0; j < apple.Length; j++)
                                {
                                    apple[j].s = "" + UnityEngine.Random.Range(110);
                                    apple[j].pressed = false;
                                }
                                score = 0;
                                playTime = _playTime;
                                gameOver = false;
     
                            }
                            else
                            {
                                // 나가기
                                gameState = 0;
                                freeIngame();
                                loadTitle();
                            }
                            break;
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < btnResult.Length; i++)
                        btnResult[i].pressed = containPoint(x, y, btnResult[i].rt);
                    break;
            }
     
            return true;
        }
     
     
    cs

     

    애플게임 (https://www.gamesaien.com/game/fruit_box_a)
    1) drawLine, fill(draw)Rect, drawImage, drawString 등 소수의 함수로 게임 제작함.
    2) 엔진에서 제공하는 기능(컴포넌트 등)이 만들어지는 과정을 설명하기 위한 용도.
    3) 최종적으로 라이브러리를 만들어 가는 과정을 통해, 게임 엔진에 대한 이해 향상하는것이 최종 목표.

     

    키보드 조작으로 Hierachy에 포함된 오브젝트를 보는 카메라 조정 가능하며, RenderView MaterialIndex값을 변경하여 각종 Shader효과를 확인 가능.

     

Designed by Tistory.