74 lines
2.0 KiB
C#
74 lines
2.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Yarn.Unity;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class YarnOption
|
|
{
|
|
private int _idx;
|
|
|
|
private readonly DialogueOption _dialogueOption;
|
|
|
|
private Action<int> _onOptionSelected;
|
|
|
|
/// <summary>
|
|
/// 选项中的文字
|
|
/// </summary>
|
|
public string Text => _dialogueOption.Line.TextWithoutCharacterName.Text;
|
|
|
|
public bool IsAvailable => _dialogueOption.IsAvailable;
|
|
|
|
public int Index => _idx;
|
|
|
|
public YarnOption(int idx, DialogueOption dialogueOption, Action<int> onOptionSelected)
|
|
{
|
|
_dialogueOption = dialogueOption;
|
|
_onOptionSelected = onOptionSelected;
|
|
_idx = idx;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 选择该选项
|
|
/// </summary>
|
|
public void SelectOption()
|
|
{
|
|
if (!IsAvailable)
|
|
{
|
|
Debug.LogWarning($"选项 {Text} 不可用");
|
|
return;
|
|
}
|
|
|
|
if (_onOptionSelected == null)
|
|
{
|
|
Debug.LogWarning($"选项 {Text} 已经执行过了");
|
|
return;
|
|
}
|
|
|
|
// 执行选项
|
|
_onOptionSelected.Invoke(_dialogueOption.DialogueOptionID);
|
|
_onOptionSelected = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成选项
|
|
/// </summary>
|
|
/// <param name="options">原始选项</param>
|
|
/// <param name="onOptionSelected">选择</param>
|
|
/// <returns>包装选项</returns>
|
|
public static YarnOption[] GeneOptions(DialogueOption[] options, Action<int> onOptionSelected)
|
|
{
|
|
var yarnOptions = new YarnOption[options.Length];
|
|
for (var i = 0; i < options.Length; i++)
|
|
{
|
|
yarnOptions[i] = new YarnOption(i, options[i], onOptionSelected);
|
|
}
|
|
|
|
return yarnOptions;
|
|
}
|
|
}
|
|
}
|
|
|