๐Ÿ“š Computer Science/Design Pattern

[Unity][๋””์ž์ธ ํŒจํ„ด] Component Pattern

ibelieveinme 2023. 9. 12. 09:24
728x90

*Component Pattern: ๋กœ์ง์„ ๊ธฐ๋Šฅ๋ณ„๋กœ ์ปดํฌ๋„ŒํŠธ ํ•˜๋Š” ๊ฒƒ.

 

ํด๋ž˜์Šค๋ฅผ ๋ถ„๋ฆฌํ•˜๊ณ  ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ณ  Decoupling(๋…์ž์ ์ธ ํ๋ฆ„์„ ๋ณด์ด๋Š” ๊ฒƒ) ์‹œํ‚ค๋Š” ๊ฒƒ์€ ํ˜‘์—…ํ•˜์—ฌ ๊ฐœ๋ฐœ์„ ๋งŽ์ด ํ•ด๋ณธ ์‚ฌ๋žŒ๋“ค์—๊ฒŒ๋Š” ์ž์—ฐํžˆ ๋…น์•„ ์žˆ๋Š” ์Šต๊ด€์ด๋‹ค. ์ด๋Ÿฐ ๊ตฌ์กฐ์ ์ธ ๊ฐœ์„  ์ž‘์—…์„ ์„ ๋ฐฐ ๊ฐœ๋ฐœ์ž๋“ค์ด ์ด๋ฏธ ์ปดํฌ๋„ŒํŠธ ํŒจํ„ด์œผ๋กœ ๋ช…๋ช…ํ•ด ๋‘์—ˆ๋‹ค.

 

ํŠนํžˆ Unity ์˜ GameObject ๋Š” Component ํŒจํ„ด์ด ์ž˜ ๋ฐ˜์˜๋˜์–ด ์„ค๊ณ„๋œ ํด๋ž˜์Šค์ด๋‹ค. GetComponet Method ๋กœ ๊ฐ๊ฐ์˜ Decoupling ๋œ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ๋ถˆ๋Ÿฌ์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์œ ๋‹ˆํ‹ฐ๋Š” ์ƒˆ๋กœ์šด ํ–‰๋™์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ปดํฌ์ง€์…˜(composition: ํ•ฉ์„ฑ) ๊ตฌ์กฐ๋ฅผ ์ฑ„์šฉํ•œ๋‹ค. ์ด ์ ์€ ์ƒ์†(Inheritance)๋ฅผ ํ†ตํ•ด ํ™•์žฅ๋˜๋Š” ์˜ค๋ธŒ์ ํŠธ ์ง€ํ–ฅ ํด๋ž˜์Šค ๊ณ„์ธต๊ณผ๋Š” ์œ„์ƒ์„ ๋‹ฌ๋ฆฌํ•œ๋‹ค.

 

ex)

2๊ฐœ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  1๊ฐœ๋Š” ํšŒ์ „, 1๊ฐœ๋Š” ์ด๋™ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ํ•ด๋ณด์ž.

 

//๋””์ž์ธ ํŒจํ„ด ์ ์šฉ์•ˆํ•˜๊ณ  ํ•˜๋‚˜์˜ ์Šคํฌ๋ฆฝํŠธ์— ์ž‘์„ฑํ–ˆ์„ ๋•Œ

using System.Collections;
using UnityEngine;


enum MOVE
{
    MOVE_RIGHT,
    MOVE_LEFT
}

public class SpaghettiAct : MonoBehaviour
{
    [SerializeField] private GameObject player1;
    [SerializeField] private GameObject player2;
    private MOVE move = MOVE.MOVE_RIGHT;

    private void Start()
    {
        StartCoroutine("MixedAct");
    }

    IEnumerator MixedAct()
    {
        while (true)
        {
            player1.transform.Rotate(90.0f * Vector3.up);
            player2.transform.Rotate(90.0f * Vector3.up);

            if(player1.transform.position.x < -4)
            {
                move = MOVE.MOVE_RIGHT;
            }
            else if(player1.transform.position.x > 4)
            {
                move = MOVE.MOVE_LEFT;
            }

            if(move == MOVE.MOVE_RIGHT)
            {
                player1.transform.Translate(1.0f * Vector3.right, Space.World);
                player2.transform.Translate(-1.0f * Vector3.right, Space.World);
            }
            else
            {
                player1.transform.Translate(-1.0f * Vector3.right, Space.World);
                player2.transform.Translate(1.0f * Vector3.right, Space.World);
            }

            yield return new WaitForSeconds(0.5f);
        }
    }
}

 

//Component ํŒจํ„ด์„ ์ ์šฉํ•ด์„œ ํด๋ž˜์Šค๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ์ž‘์„ฑํ–ˆ์„ ๋•Œ

using System.Collections;
using UnityEngine;


public class RotateAct : MonoBehaviour
{
    private void Start()
    {
        StartCoroutine("Rotate");
    }

    IEnumerator Rotate()
    {
        while (true)
        {
            transform.Rotate(90.0f * Vector3.up);
            yield return new WaitForSeconds(0.3f);
        }
    }
}
using System.Collections;
using UnityEngine;


enum MOVE
{
    MOVE_RIGHT,
    MOVE_LEFT
}

public class MoveAct : MonoBehaviour
{
    private MOVE move = MOVE.MOVE_RIGHT;

    private void Start()
    {
        StartCoroutine("Move");
    }

    IEnumerator Move()
    {
        while (true)
        {
            if(transform.position.x < -4)
            {
                move = MOVE.MOVE_RIGHT;
            }
            else if(transform.position.x > 4)
            {
                move = MOVE.MOVE_LEFT;
            }

            if(move == MOVE.MOVE_RIGHT)
            {
                transform.Translate(1.0f * Vector3.right, Space.World);
            }
            else
            {
                transform.Translate(-1.0f * Vector3.right, Space.World);
            }
            yield return new WaitForSeconds(0.5f);
        }
    }
}

ํšŒ์ „ํ•˜๋Š” ๊ฐ์ฒด์™€ ์ด๋™ํ•˜๋Š” ๊ฐ์ฒด์— ๋ถ™์ผ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ถ„๋ฆฌํ•˜์˜€๋‹ค.


๊ฐ€๋…์„ฑ, ์œ ์ง€๋ณด์ˆ˜๋ฅผ ์œ„ํ•ด์„œ๋ผ๋„ Component ํŒจํ„ด์„ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค.

728x90