[Memo-Unity] 25. Delegate LOAD(SO)
์นดํ ๊ณ ๋ฆฌ: MeMo Unity
ํ๊ทธ: C#, MeMo Unity
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
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๋ก ์ฌ์ฉ
// 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
// 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
// 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
๋๊ธ๋จ๊ธฐ๊ธฐ