๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Personal

[๊ณต๋ชจ์ „ ํ™œ๋™] Unity ์ถ”๋ฆฌ ์‹œ์Šคํ…œ ๊ตฌ์ถ• - ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ & ์„ ํƒ์ง€ ๋ถ„๊ธฐ

728x90
๋ฐ˜์‘ํ˜•

 

 

 ์„œ๋‘

์ด๋ฒˆ ๊ธ€์€ ์ €์˜ ๊ฐœ์ธ์ ์œผ๋กœ ๊ณต๋ถ€์™€ ๊ณต๋ชจ์ „ ํ™œ๋™์„ ์ •๋ฆฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ธ€์ด๋ฏ€๋กœ ์ •๋‹ต์ด ์•„๋‹ˆ๋ผ๋Š” ์ .  "์ด๋ ‡๊ฒŒ ํ–ˆ๊ตฌ๋‚˜" ์ •๋„๋กœ ์ƒ๊ฐํ•˜์—ฌ ์ฐธ๊ณ ์šฉ์œผ๋กœ๋งŒ ๋ด์ฃผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ฝ”๋”ฉ์—๋Š” ์ •๋‹ต์ด ์—†์œผ๋‹ˆ๊น์š”.

 

https://www.gigdc.or.kr/game/item.php?it_id=1688204158&ca_id=6020&findType=&findType2=&findType3=20&findWord=&page=6&sort1=&sort2= 

 

GIGDC

๊ธ€๋กœ๋ฒŒ ์ธ๋””๊ฒŒ์ž„์ œ์ž‘ ๊ฒฝ์ง„๋Œ€ํšŒ

www.gigdc.or.kr

์ฐธ๊ฐ€ํ•œ ๊ณต๋ชจ์ „ ์ •๋ณด๋Š” ์œ„ ์‚ฌ์ดํŠธ ๋งํฌ๋ฅผ ํ†ตํ•ด ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ํ˜‘์—…๊ณผ ๊ณต๋ชจ์ „ ํ™œ๋™์ด๊ธฐ์— ํ˜„์žฌ๋Š” ์ •๋ง ์ •์‹ ์ด ์—†๋„ค์š”..

์™„์„ฑ์„ ํ•  ์ˆ˜ ์žˆ์„๊นŒ? ํ•˜๋Š” ๊ฑฑ์ •์ด ๊ตฌํ˜„ํ•˜๋Š” ๋งค ์ˆœ๊ฐ„ ์ƒ๊ฐ์ด ๋‚˜์ง€๋งŒ, ์‹œ๊ฐ„์ด ํ๋ฅด๋ฉด ์ด๊ฒƒ๋„ ๊ฒฝํ—˜์ด์ž ์ถ”์–ต์ด๋ฏ€๋กœ ์ผ๋‹จ ์ตœ์„ ์„ ๋‹คํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค..

 

 

 

 

 

 

 ์ถ”๋ฆฌ ์‹œ์Šคํ…œ ๊ตฌ์ถ•

 

 

๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•˜๋Š” ์ถ”๋ฆฌ ์‹œ์Šคํ…œ ํ๋ฆ„์€ ์ด๋ ‡์Šต๋‹ˆ๋‹ค. ์ผ๋‹จ ์•„์ง ๋””์ž์ด๋„ˆ๋ถ„๋“ค ์ž‘์—…์ด ๋๋‚˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ํ˜ผ์ž์„œ ๋”๋ฏธ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด ์‹œ์Šคํ…œ์„ ๊ตฌ์ถ•ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 

 

 

๋“ค์–ด๊ฐˆ ๊ณต๊ฐ„ ํด๋ฆญ ์‹œ ์•„์ดํ…œ ๋ฆฌ์ŠคํŠธ ๋œจ๊ธฐ

 

๋ฆฌ์ŠคํŠธ ๋‚ด ํ•ญ๋ชฉ ์„ ํƒ ์‹œ ํ•ญ๋ชฉ ๋ฐ˜์˜ํ•˜๊ธฐ

 

์‚ฌ์ง„์œผ๋กœ ๋ณด๊ธฐ ์ข‹๊ฒŒ ์š”์•ฝ์„ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋ฐ˜์˜ํ•˜๊ณ  ์‹ถ์€ ๊ณต๊ฐ„ ํด๋ฆญ ์‹œ ์ฆ๊ฑฐ ๋ชฉ๋ก์ด ๋œจ๊ฒŒ ๋˜๊ณ , ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ ์ค‘ ํ•ญ๋ชฉ ์„ ํƒ ์‹œ ๊ทธ ํ•ญ๋ชฉ์ด ๊ณต๊ฐ„์— ๋ฐ˜์˜๋  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด์ฃ .

 

์ถ”๋ฆฌ๊ฒŒ์ž„ - ๋‹น์‹ ์˜ ์•ˆ๋…•์„ ์œ„ํ•˜์—ฌ

 

์ด์™€ ์ œ์ผ ๋น„์Šทํ•œ ์ถ”๋ฆฌ ์‹œ์Šคํ…œ์œผ๋กœ "๋‹น์‹ ์˜ ์•ˆ๋…•์„ ์œ„ํ•˜์—ฌ" ๊ฒŒ์ž„์„ ์ฐธ๊ณ ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

1. Canvas๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

2. ๋ฐ˜์˜ํ•  ๊ณต๊ฐ„ Panel ๊ณผ ๋ฆฌ์ŠคํŠธ ํ•ญ๋ชฉ์„ ๋‹ด์„ Panel์„ ์ฆ‰, 2๊ฐœ์˜ Panel์„ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

 

 

๋งŒ๋“ค์–ด์ง€๋ฉด ์ด๋Ÿฐ ๊ตฌ์กฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ „ Guess Panel์— Grid Layout Group์„ ๋„ฃ์–ด์ฃผ์–ด ๋ณด๊ธฐ ์ข‹๊ฒŒ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

 

์ด์ œ ์›ํ•˜๋Š” ์ž‘๋™์„ ํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์งœ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋‘ ๊ฐœ์˜ ํด๋ž˜์Šค๊ฐ€ ํ•„์š”ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ๋‚ด ํ•ญ๋ชฉ๊ณผ ๋ฐ˜์˜ ๊ณต๊ฐ„์— ์–ด๋–ป๊ฒŒ ๋ณ€ํ™”๋ฅผ ์ค„ ๊ฒƒ์ธ๊ฐ€์— ๋Œ€ํ•œ ํด๋ž˜์Šค ํ•˜๋‚˜์™€ 
๋ฆฌ์ŠคํŠธ ๋‚ด ํ•ญ๋ชฉ์„ ํด๋ฆญ ์ด๋ฒคํŠธ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค ํ•˜๋‚˜๊ฐ€ ํ•„์š”ํ•˜๊ฒ ๋„ค์š”.

Selects ํด๋ž˜์Šค:
- ์ฆ๊ฑฐ ๋ฆฌ์ŠคํŠธ ๋‚ด์—์„œ ํ•ญ๋ชฉ์„ ์„ ํƒํ•˜์—ฌ ์ƒ‰์ƒ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ธฐ๋Šฅ
- ๋ฒ„ํŠผ์˜ ์ƒ‰์ƒ๊ณผ ํ…์ŠคํŠธ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ธฐ๋Šฅ

SlotClickHandler ํด๋ž˜์Šค:
- ์Šฌ๋กฏ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ
- ํด๋ฆญํ•œ ์Šฌ๋กฏ์— ๋Œ€ํ•œ ์ƒ‰์ƒ๊ณผ ํ…์ŠคํŠธ ๋ณ€๊ฒฝ
- ๋ชจ๋“  ์Šฌ๋กฏ์ด ๋นจ๊ฐ„์ƒ‰์ธ์ง€ ํ™•์ธํ•˜๊ณ  ์ฒ˜๋ฆฌ ๋กœ์ง์„ ์ˆ˜ํ–‰

 

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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
 
public class Selects : MonoBehaviour
{
 
    // ์ฆ๊ฑฐ ๋ฆฌ์ŠคํŠธ ๋‚ด์—์„œ ํ•ญ๋ชฉ์„ ์„ ํƒ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    public GameObject[] slots;
   
    // ์ฆ๊ฑฐ ๋ฆฌ์ŠคํŠธ ๋‚ด ํ•ญ๋ชฉ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค.
    public void ChangeSlotColor(int slotIndex, Color newColor)
    {
        GameObject slot = slots[slotIndex];
        Image slotImage = slot.GetComponent<Image>();
        slotImage.color = newColor;
    }
 
    // ๋ฆฌ์ŠคํŠธ ๋‚ด ์•„์ดํ…œ(์ฆ๊ฑฐ) ์„ ํƒ ์‹œ ์œกํ•˜์›์น™ ๊ณต๊ฐ„์— ๊ทธ ์ฆ๊ฑฐ๊ฐ€ ๋ฐ˜์˜๋œ๋‹ค.
    public void ChangeButtonColorAndText(Color newColor, string newText)
    {
        Button button = GetComponentInChildren<Button>();
        Text buttonText = button.GetComponentInChildren<Text>();
        button.image.color = newColor;
        buttonText.text = newText;
    }
 
}
cs

 

์œ„์—์„œ Prefab์œผ๋กœ ๋งŒ๋“  Slot ๋“ค์„ slots ๋ฐฐ์—ด์—์„œ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

ChangeSlotColor()๋Š” ๋ฆฌ์ŠคํŠธ ๋‚ด ์„ ํƒํ•œ ํ•ญ๋ชฉ์„ ๋ณ€๊ฒฝํ•˜๊ฒ ๋‹ค. ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”์„œ๋“œ๋Š” ์„ ํƒํ•œ ํ•ญ๋ชฉ์„ ๋‚˜ํƒ€๋‚ด๋Š”  SlotIndex์™€ ๋ณ€๊ฒฝํ•  ์ƒ‰์ƒ์ธ newColor ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„์™€ ๋ณ€๊ฒฝํ•ด ์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

ChangeButtonColorAndText()๋Š” ์„ ํƒํ•œ ํ•ญ๋ชฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ๋ฐ˜์˜ํ•˜๊ณ  ์‹ถ์€ ๊ณต๊ฐ„(Selected Panel)์— ์ •๋ณด๋ฅผ ๋ฐ˜์˜ํ•˜๊ฒ ๋‹ค. ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

Selected Panel - Selects.cs
Guess Panel - Selects.cs

 

๋‘ ๊ฐœ์˜ Panel์— ๊ฐ๊ฐ Selects ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ฃ์–ด์ค€ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค. 

 

 

 

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
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
 
 
public class SlotClickHandler : MonoBehaviour, IPointerClickHandler
{
  
    // Panel ๋ฐ Button์— ๋Œ€ํ•œ ์ฐธ์กฐ
    private Selects panelSelection;
    public Selects otherPanelSelection;
 
 
    public int objectNumber;
    // objectNumber์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ƒ‰์ƒ๊ณผ ํ…์ŠคํŠธ๋ฅผ ์„ค์ •ํ•  ๋ฐฐ์—ด
    public string texts;
 
 
    private void Start()
    {
        panelSelection = GetComponentInParent<Selects>();
 
    }
 
    public void OnPointerClick(PointerEventData eventData)
    {
       
        int N = panelSelection.slots.Length;
 
     
        bool allSlotsRed = true;
 
        // ๋ชจ๋“  Slot์ด Red์ธ์ง€ ํ™•์ธ
        for (int i = 0; i < N; i++)
        {
            if (!panelSelection.slots[i].GetComponent<Image>().color.Equals(Color.red))
            {
                allSlotsRed = false;
                break;
            }
        }
 
 
        // ๋ชจ๋“  Slot์ด Red์ผ ๋•Œ์™€ ์•„์ง Red๊ฐ€ ์•„๋‹Œ Slot์ด ์žˆ๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ๋ถ„๊ธฐ
        if (allSlotsRed)
        {
            // ๋ชจ๋“  Slot์ด Red์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌํ•  ๋กœ์ง
            print("์ „ํˆฌ ์”ฌ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค..");
        }
        else
        {
            // ์•„์ง Red๊ฐ€ ์•„๋‹Œ Slot์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ฒ˜๋ฆฌํ•  ๋กœ์ง
            print("์•„์ง ๋ชจ๋“  ์ฆ๊ฑฐ๋ฅผ ํ™•๋ณดํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค..");
        }
 
 
        // ํ˜„์žฌ ํด๋ฆญํ•œ Slot์— ๋Œ€ํ•œ ์ƒ‰์ƒ๊ณผ ํ…์ŠคํŠธ -> ํ•ญ๋ชฉ ๋ฐ˜์˜
        switch (objectNumber)
        {
            case 0:
                panelSelection.ChangeSlotColor(objectNumber, Color.red);
                otherPanelSelection.ChangeButtonColorAndText(Color.red, texts);
                break;
            case 1:
                panelSelection.ChangeSlotColor(objectNumber, Color.red);
                otherPanelSelection.ChangeButtonColorAndText(Color.red, texts);
                break;
            case 2:
                panelSelection.ChangeSlotColor(objectNumber, Color.red);
                otherPanelSelection.ChangeButtonColorAndText(Color.red, texts);
                break;
            case 3:
                panelSelection.ChangeSlotColor(objectNumber, Color.red);
                otherPanelSelection.ChangeButtonColorAndText(Color.red, texts);
                break;
 
        }
    }
}
cs

 

panelSelection์€ GuessPanel ์ฐธ์กฐ ๋ณ€์ˆ˜ / otherPanelSelection์€ SelectedPanel ์ฐธ์กฐ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. 

๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์„œ๋กœ ๋‹ค๋ฅธ ํŒจ๋„ ๊ฐ„์— ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ๋Š๋‚Œ์„ ์ค๋‹ˆ๋‹ค.

 

objectNumer ๋ณ€์ˆ˜๋Š” Prefab์œผ๋กœ ์„ ์–ธํ•œ Slot๋“ค์˜ ์˜ค๋ธŒ์ ํŠธ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. 

Switch()๋ฌธ์—์„œ ์–ด๋–ค ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์„ ํƒํ–ˆ๋Š”์ง€ ์‹๋ณ„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

SlotClickHander ํด๋ž˜์Šค๋Š” IPointerClickHandler์„ ์ƒ์†๋ฐ›์•„ ๋งˆ์šฐ์Šค ํด๋ฆญ / ํ„ฐ์น˜ ๋“ฑ์„ ๊ฐ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

https://docs.unity3d.com/kr/560/Manual/SupportedEvents.html

 

์ง€์›๋˜๋Š” ์ด๋ฒคํŠธ - Unity ๋งค๋‰ด์–ผ

์ด๋ฒคํŠธ ์‹œ์Šคํ…œ์€ ๋‹ค์ˆ˜์˜ ์ด๋ฒคํŠธ๋ฅผ ์ง€์›ํ•˜๋ฉฐ ์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์„ฑํ•œ ์ž…๋ ฅ ๋ชจ๋“ˆ์„ ํ†ตํ•ด ํ•œ์ธต ๋” ํšจ์œจ์ ์œผ๋กœ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

docs.unity3d.com

 

 

 

OtherPanelSelection ๋ณ€์ˆ˜๋Š” Public์œผ๋กœ ์„ ์–ธํ•ด ์ฃผ์—ˆ๊ธฐ์— ์ง์ ‘ Panel์„ ์—ฐ๊ฒฐํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Object Number๋ฅผ ์ง€์ •ํ•ด ์Šฌ๋กฏ๋“ค์˜ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•˜๊ณ , Texts์—๋Š” ๋ฐ˜์˜ํ•  ๊ณต๊ฐ„์— ์–ด๋–ค ํ…์ŠคํŠธ๋ฅผ ์ง€์ •ํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 

 

 

 


 

๊ตฌํ˜„ ์˜์ƒ

 

์ €๋Š” ์ถ”๊ฐ€๋กœ ๋ฒ„ํŠผ ํ•˜๋‚˜๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์›๋ž˜ ์ƒ‰๊น”๊ณผ Text๋กœ ๋„˜์–ด๊ฐ€๋Š” ์ฝ”๋“œ๋ฅผ ๋”ฐ๋กœ ๊ตฌํ˜„ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

๋“ค์–ด๊ฐˆ ๊ณต๊ฐ„์„ ํด๋ฆญํ•˜๋ฉด ์•„์ดํ…œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋œจ๊ณ , ๋ฆฌ์ŠคํŠธ ๋‚ด ํ•ญ๋ชฉ์ด ์ž˜ ๋ฐ˜์˜๋˜๊ณ  ์žˆ๋Š” ๋ชจ์Šต์ž…๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ, ๋ชจ๋“  ํ•ญ๋ชฉ ์„ ํƒ ์‹œ ๋‹ค์Œ ๋กœ์ง์ด ์กฐ๊ธˆ ๋ถ€์‹คํ•ด๋ณด์ด๋„ค์š”... ์ œ๊ฐ€ ์›ํ•˜๋Š” ๊ฑด ๋ชจ๋“  ํ•ญ๋ชฉ์ด ์„ ํƒ๋˜์ž๋งˆ์ž "์ „ํˆฌ ์”ฌ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค." ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด์—ˆ์ง€๋งŒ, ์˜์ƒ์—์„œ๋Š” ๋ชจ๋“  ํ•ญ๋ชฉ์ด ์„ ํƒ๋˜๊ณ  ๋‚˜์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ํด๋ฆญํ•ด์•ผ๋งŒ ๋กœ๊ทธ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ์ฐํžŒ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  ์ด ๋ถ€๋ถ„์€ ์ข€ ๋” ๊ณ ๋ฏผ์„ ํ•ด๋ด์•ผ๊ฒ ๋„ค์š”.. ใ…  

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•