using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPC : MonoBehaviour
{
public int npcID;
public string npcName;
public DialogueManager dialogueManager;
public bool playerInRange;
void Start()
{
// DialogueManager๋ฅผ ์ฐพ์์ ๋ณ์์ ์ ์ฅ
dialogueManager = GameObject.FindObjectOfType<DialogueManager>();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
// ํ๋ ์ด์ด๊ฐ ์์ญ์ ๋ค์ด์ค๋ฉด playerInRange๋ฅผ true๋ก ์ค์ ํฉ๋๋ค.
playerInRange = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
// ํ๋ ์ด์ด๊ฐ ์์ญ์์ ๋ฒ์ด๋๋ฉด playerInRange๋ฅผ false๋ก ์ค์ ํฉ๋๋ค.
playerInRange = false;
}
}
private void Update()
{
// G ํค๋ฅผ ๋๋ฅด๊ณ , ํ๋ ์ด์ด๊ฐ ์์ญ์ ์์ผ๋ฉฐ, ๋ํ๊ฐ ํ์ฌ ์งํ ์ค์ด์ง ์์ ๋๋ง ๋ํ๋ฅผ ์์ํฉ๋๋ค.
if (Input.GetKeyDown(KeyCode.G) && playerInRange && dialogueManager.dialogueQueue.Count == 0)
{
dialogueManager.ShowDialogue(npcID);
}
// G ํค๋ฅผ ๋๋ฅด๋ฉด ๋ํ๋ฅผ ๊ณ์ ํฉ๋๋ค.
else if (Input.GetKeyDown(KeyCode.G))
{
dialogueManager.ContinueDialogue();
}
}
}
_________________________________________________________
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using TMPro;
[System.Serializable]
public class NPCDialogue
{
public int npcID;
public Dictionary<string, List<List<string>>> dialogue;
}
[System.Serializable]
public class NPCDialogueList
{
public List<NPCDialogue> npcDialogues;
}
public class DialogueManager : MonoBehaviour
{
public AffinityManager affinityManager;
public NPCDialogueList npcDialogueList;
public TextMeshProUGUI dialogueText;
public TextAsset dialogueJson;
void Start()
{
LoadDialogueData();
}
void LoadDialogueData()
{
if (dialogueJson != null)
{
string dataAsJson = dialogueJson.text;
if (dataAsJson != null)
{
Debug.Log("๋์ฌ ๋ฐ์ดํฐ ํธ์ถ ์ฑ๊ณต!");
Debug.Log(dataAsJson);
npcDialogueList = JsonUtility.FromJson<NPCDialogueList>(dataAsJson);
if (npcDialogueList != null)
{
Debug.Log("๋์ฌ ๋ฆฌ์คํธ ํธ์ถ ์ฑ๊ณต!");
}
}
}
else
{
Debug.Log("dialogueJson์ด ํ ๋น๋์ง ์์์ต๋๋ค.");
}
}
public Queue<string> dialogueQueue = new Queue<string>();
public void ShowDialogue(int npcID)
{
NPCDialogue npcDialogue = npcDialogueList.npcDialogues.Find(dialogue => dialogue.npcID == npcID);
Debug.Log(npcID);
string affinityLevel = affinityManager.GetAffinityLevel(npcID);
Debug.Log(affinityLevel);
if (npcDialogue != null)
{
Debug.Log("npcDialogue๋ ์กด์ฌํฉ๋๋ค");
}
if (npcDialogue != null && npcDialogue.dialogue.ContainsKey(affinityLevel))
{
List<List<string>> dialogueSets = npcDialogue.dialogue[affinityLevel];
// ๋ํ ์ธํธ ์ค์์ ๋๋คํ๊ฒ ํ๋๋ฅผ ์ ํํฉ๋๋ค.
int randomIndex = Random.Range(0, dialogueSets.Count);
List<string> selectedDialogueSet = dialogueSets[randomIndex];
foreach (string dialogue in selectedDialogueSet)
{
dialogueQueue.Enqueue(dialogue);
}
// ์ฒซ ๋ฒ์งธ ๋ํ๋ฅผ ๋ณด์ฌ์ค๋๋ค.
if (dialogueQueue.Count > 0)
{
dialogueText.text = dialogueQueue.Dequeue();
}
}
else
{
Debug.Log("์๋ฌ ์กด์ฌ");
}
}
public void ContinueDialogue()
{
// ๋ํ ํ์ ๋ ์ด์ ๋ํ๊ฐ ์์ ๋๊น์ง ๋ํ๋ฅผ ๊ณ์ํฉ๋๋ค.
if (dialogueQueue.Count > 0)
{
dialogueText.text = dialogueQueue.Dequeue();
}
else
{
// ๋ํ๊ฐ ๋๋๋ฉด ํ
์คํธ๋ฅผ ๋น์๋๋ค.
dialogueText.text = "(๋ํ ๋)";
}
}
}
____________________________________________________________
{
"npcDialogues": [
{
"npcID": 1,
"dialogue": {
"์ฒ์๋ณด๋ ์ฌ์ด": [
[ "์๋
ํ์ธ์, ์ฒ์ ๋ต๊ฒ ์ต๋๋ค.", "์ ๋ NPC์
๋๋ค." ],
[ "์๋
ํ์ธ์.", "๋ ์จ๊ฐ ์ข๋ค์." ]
],
"์๋จน์๋จน": [
[ "๋ค์ ๋ง๋์ ๋ฐ๊ฐ์์.", "๋ ๋ง๋์ ์ข๋ค์." ],
[ "์ค๋๋ง์
๋๋ค.", "์ต๊ทผ์ ์ด๋ป๊ฒ ์ง๋ด์
จ๋์?" ]
],
"์น๊ทผํจ": [
[ "์น๊ทผํ ๋ถ์๊ธฐ๊ฐ ์ข๋ค์.", "์์ฃผ ๋ง๋์ผ๊ฒ ์ด์." ],
[ "์ด์ ๊ฝค ์นํด์ง ๊ฒ ๊ฐ์์.", "๋ค์์ ๋ ๋ต๊ธธ ๋ฐ๋๋๋ค." ]
],
"์น๊ตฌ": [
[ "์น๊ตฌ๋ผ๊ณ ๋ถ๋ฌ๋ ๋ ๊น์?", "ํจ๊ปํ๋ ์๊ฐ์ด ์ฆ๊ฒ๋ค์." ],
[ "์ ํฌ ์ด์ ์น๊ตฌ์ฃ ?", "์น๊ตฌ๋ผ๋ ๊ธฐ๋ถ์ด ์ข์์." ]
],
"์ข์ ์น๊ตฌ": [
[ "๋น์ ๊ณผ๋ ์ ๋ง ์ข์ ์น๊ตฌ๊ฐ ๋ ์ ์์ ๊ฒ ๊ฐ์์.", "์ ํฌ๋ ์ด์ ์ข์ ์น๊ตฌ์
๋๋ค." ],
[ "์ข์ ์น๊ตฌ์ ํจ๊ปํ๋ ์๊ฐ์ ํญ์ ๊ธฐ์ฉ๋๋ค.", "๋น์ ์ ์ ์๊ฒ ์ค์ํ ์น๊ตฌ๊ฐ ๋์์ด์." ]
],
"๋ฒ ์คํธ ํ๋ ๋": [
[ "๋น์ ์ ์ ์ ๋ฒ ์คํธ ํ๋ ๋์
๋๋ค.", "์ ํฌ๋ ์ด์ ๋ฒ ์คํธ ํ๋ ๋์
๋๋ค." ],
[ "๋ฒ ์คํธ ํ๋ ๋์ ๋ณด๋ด๋ ์๊ฐ์ ์์คํด์.", "๋น์ ๊ณผ ํจ๊ปํ๋ ์๊ฐ์ ํญ์ ํ๋ณตํฉ๋๋ค." ]
],
"์ธ์ ์น๊ตฌ": [
[ "๋น์ ์ ์ ์ ์ธ์ ์น๊ตฌ์
๋๋ค.", "์ธ์ ์น๊ตฌ๋ ์ด๋ฐ ๊ฑฐ๊ฒ ์ฃ ?" ],
[ "๋น์ ์์ด๋ ์์๋ ํ ์ ์์ด์.", "์ธ์ ์น๊ตฌ๋ฅผ ๋ง๋ ๊ฒ์ ๊ฐ์ฌ๋๋ ค์." ]
]
}
}
]
}
_________________________________________________________
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class AffinityData
{
public int npcID;
public int affinity;
}
public class AffinityManager : MonoBehaviour
{
public List<AffinityData> affinityDataList = new List<AffinityData>();
public int GetAffinity(int npcID)
{
AffinityData affinityData = affinityDataList.Find(data => data.npcID == npcID);
if (affinityData != null)
{
return affinityData.affinity;
}
else
{
return 0;
}
}
public void AddAffinity(int npcID, int amount)
{
AffinityData affinityData = affinityDataList.Find(data => data.npcID == npcID);
if (affinityData != null)
{
affinityData.affinity = Mathf.Clamp(affinityData.affinity + amount, 0, 100);
}
else
{
AffinityData newData = new AffinityData { npcID = npcID, affinity = Mathf.Clamp(amount, 0, 100) };
affinityDataList.Add(newData);
}
}
public string GetAffinityLevel(int npcID)
{
int affinity = GetAffinity(npcID);
if (affinity == 0)
{
return "์ฒ์๋ณด๋ ์ฌ์ด";
}
else if (affinity >= 1 && affinity <= 10)
{
return "์๋จน์๋จน";
}
else if (affinity >= 11 && affinity <= 30)
{
return "์น๊ทผํจ";
}
else if (affinity >= 31 && affinity <= 50)
{
return "์น๊ตฌ";
}
else if (affinity >= 51 && affinity <= 80)
{
return "์ข์ ์น๊ตฌ";
}
else if (affinity >= 81 && affinity <= 90)
{
return "๋ฒ ์คํธ ํ๋ ๋";
}
else
{
return "์ธ์ ์น๊ตฌ";
}
}
}
ํธ๊ฐ๋์ ๋ฐ๋ฅธ ๋ค์ํ ๋์๋ฅผ JSON์์ ๊ฐ์ ธ์์ ๋๋ค์ผ๋ก ๋ฝ๋ ์ฝ๋์ธ๋ฐ
NullReferenceException: Object reference not set to an instance of an object
DialogueManager.ShowDialogue (System.Int32 npcID) (at Assets/Scripts/NPC ๊ด๋ จ/์์ ๋ ๋ํ ์์คํ
/DialogueManager.cs:71)
NPC.Update () (at Assets/Scripts/NPC ๊ด๋ จ/์์ ๋ ๋ํ ์์คํ
/NPC.cs:41)
์๋ฌ๋ DialogueManager ์คํฌ๋ฆฝํธ
string affinityLevel = affinityManager.GetAffinityLevel(npcID);
์ฌ๊ธฐ์ ์๋ฌ๊ฐ ๋๋ค๋ค
๋ง์ฝ ์๋๊ฑฐ ์์ผ๋ฉด ์๋ ค์ค...
๊ณ ์ณ์ง๋ฉด ์ปคํผ ๊ธฐํํฐ์ฝ.... ๋๋ฆฌ๊ฒ ์ต๋๋ค....
public AffinityManager affinityManager; ์ด๊ฑฐ ํ ๋น์๋๊ฑฐ ์๋?
์ธ์คํํฐ์ affinityManager ๋ค์ด๊ฐ ์ค๋ธ์ ํธ๋ฅผ ํ ๋นํ๋๋ฐ๋ ๊ณ์ ์ ๋..
์คํฌ๋ฆฝํธ๊ฐ ์ฌ๋ฌ๊ฐ ๋ค์ด๊ฐ๊ฑด ์๋์ง? ์ค๋ณต๋๊ฑฐ ์๋์ง ํ์ธ ใฑใฑ
affinityManager์ด null์ธ ๊ฒ ์ธ์๋ ์ ๋ ์ค๋ฆ ๋๋ฒ๊น ํ์ ๋ null์ด ์๋์ง ํ์ธํจ?
if (affinityManager == null) { Debug.Log("affinityManager๊ฐ null์ ๋๋ค."); } else if (affinityManager != null) { Debug.Log("affinityManager๊ฐ null์ด ์๋๋๋ค."); }์ด๋ ๊ฒ ํ์ ๋ "affinityManager๊ฐ null์ด ์๋๋๋ค"๊ฐ ๋จ๊ณ Debug.Log(affinityLevel); ๋ฅผ ํ๋ฉด ํ์ฌ ์น๋ฐ๋๋ ์๋จ๋๋ผ์ด์ํ๋ค...ใ ใ ใ
ํ ๊ทธ๋ผ ์๋ฌด๋ฆฌ ์๊ฐํด๋ ์ ์ค์ด ์๋๋ผ ๋ค๋ฅธ๊ณณ์ ์์ด์ผํ๋๊ฑด๋ฐ.. affinityManager์ ๋ค๋ฅธ ๊ฐ์ฒด๊ฐ ์ต์ง๋ก ์บ์คํ ๋์ด ๋ค์ด๊ฐ๋ค๋ ์๋ฆ ๋ผ์น๋ ๋ฌธ์ ๊ฐ ์๋ ์ด์
์๋... ํ์ด์ด๋ผํค์์ ํธ๊ฐ๋ ๋งค๋์ ๋ผ ์จ๋๊ณ ์คํฌ๋ฆฝํธ๋ ๋ฃ์ ๋ค์ ID ์จ์ฃผ๊ณ ํธ๊ฐ๋ ๊ฐ ๋ฃ์ด์ค์ affinityLevel์ ๋๋ฒ๊น ํ๋ฉด ์๋จ๋๋ฐ ์ ๊ธฐ๋ง... ์ต๊น์ธ๊ฐ ใ ใ
๋๋ฒ๊ฑฐ ๋๋ ค๋ณด์ฌ? ์คํํํ๋ฉด์ ์ค์ฝํ ๋ด ๊ฐ๋ค ํ์ธํด๊ฐ๋ฉฐ ํด๊ฒฐํด์ผํ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ์๋ฐ?
์ดํผ๋ํฐ ๋งค๋์ ธ ์ฌํ๋ค๋ ธ๋ ธ ใ ใ ใ
ใ ใ ...
start ์คํ ์์ ์๊ด์์ด ์์ฑ๋๊ฑฐ์?
๊ทธ๋ฆฌ๊ณ ์ด๋ด๋ debug.log๋ก ์ผ์ผํ ์ฒดํฌํ์ง๋ง๊ณ ide์์ ์ค๋จ์ ์ฐ์ด์ ๊ฐ ์ด๋ค์ง ํ์ธํ๋๊ฒ ์ ์ผ ๋น ๋ฆ
npcDialogueList ์์ npcDialogues๋ฆฌ์คํธ๊ฐ ํ ๋น๋๊ฑฐ ๋ง์? npcDialogues์ด๊ฑฐ NULL์ฒดํฌํ๋ฉด ์ด์ผ๋๋?
https://gall.dcinside.com/mgallery/board/view/?id=game_dev&no=127640&page=1