This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ShrinkTutorial
|
||||
{
|
||||
[CreateAssetMenu(fileName = "ShrinkTutorialDatabase", menuName = "ShrinkTutorial/Tutorial Database")]
|
||||
public class ShrinkTutorialDatabase : ScriptableObject
|
||||
{
|
||||
[SerializeField] private List<ShrinkTutorialData> tutorials = new();
|
||||
|
||||
private Dictionary<string, ShrinkTutorialData> _cache;
|
||||
|
||||
public IReadOnlyList<ShrinkTutorialData> Tutorials => tutorials;
|
||||
|
||||
public bool TryGetTutorial(string tutorialId, out ShrinkTutorialData tutorial)
|
||||
{
|
||||
EnsureCache();
|
||||
return _cache.TryGetValue(tutorialId, out tutorial);
|
||||
}
|
||||
|
||||
public List<string> Validate()
|
||||
{
|
||||
var issues = new List<string>();
|
||||
var ids = new HashSet<string>();
|
||||
|
||||
foreach (var tutorial in tutorials)
|
||||
{
|
||||
if (!tutorial)
|
||||
{
|
||||
issues.Add("存在空的教程资源引用。");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(tutorial.tutorialId))
|
||||
{
|
||||
issues.Add($"教程资源 {tutorial.name} 缺少 tutorialId。");
|
||||
}
|
||||
else if (!ids.Add(tutorial.tutorialId))
|
||||
{
|
||||
issues.Add($"tutorialId 重复: {tutorial.tutorialId}");
|
||||
}
|
||||
|
||||
if (tutorial.steps == null || tutorial.steps.Count == 0)
|
||||
{
|
||||
issues.Add($"教程 {tutorial.tutorialId} 没有任何步骤。");
|
||||
continue;
|
||||
}
|
||||
|
||||
for (var i = 0; i < tutorial.steps.Count; i++)
|
||||
{
|
||||
var step = tutorial.steps[i];
|
||||
if (step == null)
|
||||
{
|
||||
issues.Add($"教程 {tutorial.tutorialId} 第 {i + 1} 步为空。");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (step.targetMode == ShrinkTutorialTargetMode.Path && string.IsNullOrWhiteSpace(step.targetPath))
|
||||
issues.Add($"教程 {tutorial.tutorialId} 第 {i + 1} 步缺少 targetPath。");
|
||||
|
||||
if (step.targetMode == ShrinkTutorialTargetMode.AnchorId && string.IsNullOrWhiteSpace(step.anchorId))
|
||||
issues.Add($"教程 {tutorial.tutorialId} 第 {i + 1} 步缺少 anchorId。");
|
||||
|
||||
if (step.completeCondition == ShrinkTutorialCompleteCondition.CustomEvent &&
|
||||
string.IsNullOrWhiteSpace(step.customEventName))
|
||||
issues.Add($"教程 {tutorial.tutorialId} 第 {i + 1} 步使用 CustomEvent 但未填写 customEventName。");
|
||||
}
|
||||
}
|
||||
|
||||
return issues;
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
_cache = null;
|
||||
}
|
||||
|
||||
private void EnsureCache()
|
||||
{
|
||||
if (_cache != null)
|
||||
return;
|
||||
|
||||
_cache = new Dictionary<string, ShrinkTutorialData>();
|
||||
foreach (var tutorial in tutorials)
|
||||
{
|
||||
if (!tutorial || string.IsNullOrWhiteSpace(tutorial.tutorialId))
|
||||
continue;
|
||||
|
||||
_cache[tutorial.tutorialId] = tutorial;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user