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);
์—ฌ๊ธฐ์„œ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค๋„ค

๋งŒ์•ฝ ์•„๋Š”๊ฑฐ ์žˆ์œผ๋ฉด ์•Œ๋ ค์ค˜...


๊ณ ์ณ์ง€๋ฉด ์ปคํ”ผ ๊ธฐํ”„ํ‹ฐ์ฝ˜.... ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค....