[Memo-Unity] 25. Delegate LOAD(SO)

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: ,


Delegate ,LOAD(SO)

Delegate

Delegate








๋ธ๋ฆฌ๊ฒŒ์ดํŠธ(Delegate)

  • Delegate ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ฐ์ฒด๋กœ, ๋ฉ”์„œ๋“œ์˜ ์‹œ๊ทธ๋‹ˆ์ฒ˜(์ž…๋ ฅ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋ฐ˜ํ™˜ ํƒ€์ž…)์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ํฌํ•จ
  • Func ๋ฐ˜ํ™˜ ํƒ€์ž…์ด void๊ฐ€ ์•„๋‹Œ 0~n๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ํ•จ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ œ๋„ค๋ฆญ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ
  • Action ๋ฐ˜ํ™˜ ํƒ€์ž…์ด void์ธ ๋ฉ”์†Œ๋“œ๋ฅผ ์œ„ํ•ด ํŠน๋ณ„ํžˆ ์„ค๊ณ„๋œ ์ œ๋„ค๋ฆญ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ
  • public Action action , public event Action action
  • event ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด์ง€ ์•Š๊ณ  Action์„ ์„ ์–ธํ•  ๊ฒฝ์šฐ ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ Action์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  • button.onClick.AddListener, inputField.onValueChanged(์ธํ’‹ํ•„๋“œ์— ๊ฐ’์ด ๋ณ€๊ฒฐ๋  ๋•Œ ๋งˆ๋‹ค) ๋„ Delegate

image

public delegate void TestDelegate();
public TestDelegate characterUpdate;

//Action
public Action action;

namespace System
{
    public delegate void Action();
}

//Func
public Func<int, float, bool> fun;

bool funcfunc(int a , float b) 
{
    return true;
}



๋ธ๋ฆฌ๊ฒŒ์ดํŠธ x public์œผ๋กœ cs.method๋กœ ์‚ฌ์šฉ
image


// Player
public class Player : MonoBehaviour
{
    public void GetExp()
    {
        Debug.Log("Get Exp");
    }
}

// QuestManager
public class QuestManager : MonoBehaviour
{
    public void UpdateQuest()
    {
        Debug.Log("Update Quest");
    }
}


// UIManager
public class UIManager : MonoBehaviour
{
    public void UpdateUI()
    {
        Debug.Log("Update UI");
    }
}

// GameManager
    public Player player;
    public QuestManager qm;
    public UIManager ui;

    void StageClear()
    {
        player.GetExp();
        qm.UpdateQuest();
        ui.UpdateUI();
    }



Delegate Action
image

// Player
public class Player : MonoBehaviour
{
    private void Start()
    {
        GameManager.Instance.action += GetExp;
    }
    public void GetExp()
    {
        Debug.Log("Get Exp");
    }
}

// QuestManager
public class QuestManager : MonoBehaviour
{
    private void Start()
    {
        GameManager.Instance.action += UpdateQuest;
    }
    public void UpdateQuest()
    {
        Debug.Log("Update Quest");
    }
}


// UIManager
public class UIManager : MonoBehaviour
{
    private void Start()
    {
        GameManager.Instance.action += UpdateUI;
    }
    public void UpdateUI()
    {
        Debug.Log("Update UI");
    }
}

// GameManager
    public event Action action;

    void StageClear()
    {
        action?.Invoke();
    }



**Delegate_Action**

// Player
public class Player : MonoBehaviour
{
    private void Start()
    {
        GameManager.Instance.actionint += GetExpint;
    }
    public void GetExpint(int a)
    {
        Debug.Log("action"+a);
    }
}

// GameManager
void StageClear()
{
    actionint?.Invoke(1);
}





SO(Sctiptable Object)

LOAD(SO)
Resources.Load
image

// Player
private CharacterSO data;

private void Awake()
{
    data = Resources.Load<CharacterSO>("SO/DefaultCharacterData");
}


// CharacterManager (so ๋ณ€๊ฒฝ)
private void Awake()
{
    data = Resources.Load<CharacterSO>("SO/DefaultCharacterData");
}

private void Start()
{
    characterUpdate += ChangeCharacter;
    characterUpdate += ChangeExp;
    characterUpdate += ChangeName;
}
public void ChangeCharacter()
{//๋ฐ์ดํ„ฐ ์ฝ์–ด์˜ด
    Debug.Log(data.hp);
}

public void ChangeExp()
{
    Debug.Log(data.exp);
}

public void ChangeName()
{
    Debug.Log(data.name);
}

// GameManager(๋ฐ์ดํ„ฐ)
private CharacterSO data;

private void Awake()
{
    data = Resources.Load<CharacterSO>("SO/DefaultCharacterData");
}

private void Start()
{
    data.hp = 50;
    data.exp = 150;
    data.name = "Name";

    CharacterUpdate();
}

void CharacterUpdate()
{
    //๋ฐ์ดํ„ฐ ๋ณ€ํ™”
    CharacterManager.Instance.characterUpdate?.Invoke();
}







12.26์ถ”๊ฐ€

๋ธ๋ฆฌ๊ฒŒ์ดํŠธ, ์ด๋ฒคํŠธ

๊ฐœ๋ฐœ์ž๊ฐ€ ์ •์˜ํ•œ ํŠน์ •์กฐ๊ฑด์ด๋‚˜ ํ–‰๋™์— ๋ฐ˜์‘ํ•˜๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ์ด๋ฒคํŠธ

  • ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ
    c# ์—์„œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ํƒ€์ž…
    ๋ฉ”์†Œ๋“œ์˜ ์ฐธ์กฐ๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๊ณ , ๋‹ค๋ฅธ ๋ฉ”์†Œ๋“œ๋กœ ์ „๋‹ฌ, ํ˜ธ์ถœ์„ ๋™์ ์œผ๋กœ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Œ.
  • ์žฅ์ 
    ์œ ์—ฐ์„ฑ, ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ, ๋ถ„๋ฆฌ
  • ๋‹จ์ 
    ๋ณต์žก์„ฑ, ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ์œ„ํ—˜


  • ์ด๋ฒคํŠธ
    ์ด๋ฒคํŠธ๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•จ
    ํŠน์ • ์ƒํ™ฉ์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ด๋ฒคํŠธ๋ฅผ ๊ตฌ๋…ํ•˜๋Š” ๋ชจ๋“  ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋จ.
  • ์žฅ์ 
    ์บก์Šํ™”, ์˜์‚ฌ์†Œํ†ต ๊ฐ•ํ™”
  • ๋‹จ์ 
    ์ดํ•ด๋„ ์š”๊ตฌ, ์˜ค๋ฒ„ํ—ค๋“œ







์žก๋‹ด




[Unity] Delegate TOP


๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ