57 lines
1.6 KiB
C#
57 lines
1.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AibisDream.Test
|
|
{
|
|
public class OptionTest : MonoBehaviour
|
|
{
|
|
private GameObject panel;
|
|
private GameObject prefab;
|
|
|
|
void Start()
|
|
{
|
|
// 初始化时注册Command
|
|
// 第一个参数为[command name=xxx]的参数
|
|
// 第二个参数是注册的函数,在Yarn走到选择项时触发
|
|
DialogController.Instance.RegisterOptionCommand("show_buttons", ShowButtons);
|
|
|
|
// 不重要
|
|
panel = transform.GetChild(0).gameObject;
|
|
prefab = Resources.Load<GameObject>("Perfabs/UI/Choice Button");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册的命令
|
|
/// </summary>
|
|
/// <param name="options">触发时提供的选择项</param>
|
|
public void ShowButtons(YarnOption[] options)
|
|
{
|
|
foreach (YarnOption option in options)
|
|
{
|
|
var instance = Instantiate(prefab);
|
|
instance.transform.SetParent(panel.transform);
|
|
instance.GetComponentInChildren<TextMeshProUGUI>().text = option.Text;
|
|
|
|
instance.GetComponent<Button>().onClick.AddListener(() => {
|
|
|
|
// 执行SelectOption(), 对话就会从对应的选项往下继续执行
|
|
option.SelectOption();
|
|
|
|
|
|
panel.SetActive(false);
|
|
});
|
|
}
|
|
}
|
|
|
|
public void OnDestroy()
|
|
{
|
|
// 注意在合适的时机移除命令
|
|
DialogController.Instance.RemoveOptionCommand("show_buttons");
|
|
}
|
|
}
|
|
}
|
|
|