first commit
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class DialogueUI : MonoBehaviour
|
||||
{
|
||||
public static DialogueUI Instance { get; private set; }
|
||||
|
||||
public GameObject root;
|
||||
public Text nameText;
|
||||
public Text bodyText;
|
||||
public RectTransform responseContainer;
|
||||
public Button responseButtonTemplate;
|
||||
|
||||
public bool IsOpen => root != null && root.activeSelf;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
Close();
|
||||
}
|
||||
|
||||
public void Open(NPCDialogue npc, DialogueData dialogueData)
|
||||
{
|
||||
if (root == null || dialogueData == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
root.SetActive(true);
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
|
||||
if (nameText != null)
|
||||
{
|
||||
nameText.text = dialogueData.npcName;
|
||||
}
|
||||
|
||||
if (bodyText != null)
|
||||
{
|
||||
bodyText.text = dialogueData.greeting;
|
||||
}
|
||||
|
||||
ClearResponses();
|
||||
|
||||
foreach (DialogueResponse response in dialogueData.responses)
|
||||
{
|
||||
Button button = Instantiate(responseButtonTemplate, responseContainer, false);
|
||||
button.name = $"Response - {response.text}";
|
||||
button.gameObject.SetActive(true);
|
||||
|
||||
Text text = button.GetComponentInChildren<Text>();
|
||||
if (text != null)
|
||||
{
|
||||
text.text = response.text;
|
||||
}
|
||||
|
||||
QuestData questToStart = response.questToStart;
|
||||
button.onClick.AddListener(() =>
|
||||
{
|
||||
if (questToStart != null && QuestManager.Instance != null)
|
||||
{
|
||||
QuestManager.Instance.StartQuest(questToStart);
|
||||
}
|
||||
|
||||
Close();
|
||||
});
|
||||
}
|
||||
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(responseContainer);
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
if (root != null)
|
||||
{
|
||||
root.SetActive(false);
|
||||
}
|
||||
|
||||
ClearResponses();
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
|
||||
private void ClearResponses()
|
||||
{
|
||||
if (responseContainer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = responseContainer.childCount - 1; i >= 0; i--)
|
||||
{
|
||||
Transform child = responseContainer.GetChild(i);
|
||||
if (responseButtonTemplate != null && child == responseButtonTemplate.transform)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user