66 lines
2.2 KiB
C#
66 lines
2.2 KiB
C#
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Globalization;
|
||
|
|
using System.IO;
|
||
|
|
using System.Linq;
|
||
|
|
using CsvHelper;
|
||
|
|
using CsvHelper.Configuration;
|
||
|
|
using Sirenix.OdinInspector;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
namespace Ichni.Story
|
||
|
|
{
|
||
|
|
public class StorylineSheetReader : SerializedMonoBehaviour
|
||
|
|
{
|
||
|
|
public TextAsset csvFile; // 直接拖拽到 Inspector
|
||
|
|
public List<StoryBlock> allNodes;
|
||
|
|
|
||
|
|
void Awake() {
|
||
|
|
if (csvFile == null) {
|
||
|
|
Debug.LogError("请在 Inspector 中指定 CSV TextAsset 文件。");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
allNodes = new List<StoryBlock>();
|
||
|
|
|
||
|
|
using (var reader = new StringReader(csvFile.text)) {
|
||
|
|
var config = new CsvConfiguration(CultureInfo.InvariantCulture) {
|
||
|
|
HasHeaderRecord = false,
|
||
|
|
IgnoreBlankLines = true
|
||
|
|
};
|
||
|
|
|
||
|
|
using (var csv = new CsvReader(reader, config)) {
|
||
|
|
int rowIndex = 0;
|
||
|
|
while (csv.Read()) {
|
||
|
|
for (int col = 0; csv.TryGetField<string>(col, out var cell); col++) {
|
||
|
|
if (!string.IsNullOrWhiteSpace(cell)) {
|
||
|
|
allNodes.Add(new StoryBlock() {
|
||
|
|
blockName = cell.Trim(),
|
||
|
|
rowIndex = rowIndex,
|
||
|
|
timeColumn = col
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
rowIndex++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
BuildDependencies();
|
||
|
|
}
|
||
|
|
|
||
|
|
void BuildDependencies() {
|
||
|
|
var groups = allNodes.GroupBy(n => n.rowIndex);
|
||
|
|
foreach (var group in groups) {
|
||
|
|
var list = group.OrderBy(n => n.timeColumn).ToList();
|
||
|
|
for (int i = 1; i < list.Count; i++) {
|
||
|
|
var prev = list[i - 1];
|
||
|
|
var curr = list[i];
|
||
|
|
prev.nextBlock = curr; // 设置前一个单元格的下一个单元格
|
||
|
|
curr.requiredCount++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|