Newer
Older
namespace de.jmu.ge.viavr.dialogs.runtime {
[RequireComponent(typeof(ArticyFlowPlayer))]
public class Dialog: MonoBehaviour, IArticyFlowPlayerCallbacks {
public string dialogId;
public string avatarName;
[FormerlySerializedAs("avatarNameText")] [SerializeField] private TMP_Text[] avatarNameTexts;
[SerializeField] private TMP_Text contentText;
[SerializeField] private Button[] buttons;
[SerializeField] private GameObject dialogPanel;
private ArticyFlowPlayer articyFlowPlayer;
private void Awake() {
articyFlowPlayer = GetComponent<ArticyFlowPlayer>();
}
private void Start() {
articyFlowPlayer.StartOn = ArticyDatabase.GetObject(id);
foreach(var tmpText in avatarNameTexts) {
tmpText.text = avatarName;
}
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Jump into first dialog fragment if dialog itself has no text
// if(ArticyDatabase.GetObject(id) is IObjectWithText current && string.IsNullOrWhiteSpace(current.Text)) {
// Debug.Log("skip");
// if(current is IObjectWithTarget currentWithTarget) {
// Debug.Log("adsfasdfasdf");
// // articyFlowPlayer.Play(ArticyDatabase.GetObject(currentWithTarget.Target.Id));
// }
// }
}
public void OnFlowPlayerPaused(IFlowObject currentNode) {
if(currentNode != null) {
if(currentNode is IObjectWithText objWithText) {
contentText.text = objWithText.Text;
}
if(currentNode is IObjectWithSpeaker objSpeaker) {
var speakerDisplayName = objSpeaker.Speaker as IObjectWithDisplayName;
var actor = speakerDisplayName?.DisplayName ?? string.Empty;
foreach(var tmpText in avatarNameTexts) {
tmpText.text = actor;
}
}
}
else {
dialogPanel.SetActive(false);
}
}
public void OnBranchesUpdated(IList<Branch> currentBranches) {
for(var i = 0; i < buttons.Length; i++) {
var correspondingBranchExists = currentBranches.Count > i;
buttons[i].gameObject.SetActive(correspondingBranchExists);
buttons[i].onClick.RemoveAllListeners();
if(!correspondingBranchExists) continue;
var branchText = string.Empty;
if(currentBranches[i].Target is IObjectWithMenuText target) {
branchText = target.MenuText;
}
if(string.IsNullOrWhiteSpace(branchText)) {
var hasNext = currentBranches[i].Target != null;
branchText = hasNext ? "Continue" : "End dialog";
}
buttons[i].GetComponentInChildren<TMP_Text>().text = branchText;
var i1 = i;
buttons[i].onClick.AddListener(() => articyFlowPlayer.Play(currentBranches[i1]));
}