Files
encounter-system/Runtime/EncounterDialogOptionSet.cs
2026-04-19 12:25:49 +02:00

49 lines
1.7 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace Jovian.EncounterSystem {
/// <summary>
/// Reusable asset containing the dialog options shown for an encounter. Stored as an asset so a
/// single set can be shared across encounters when the same choices apply. When <see cref="id"/>
/// changes the asset file auto-renames to match (editor-only).
/// </summary>
[CreateAssetMenu(fileName = "EncounterDialogOptionSet", menuName = "Jovian/Encounter System/Dialog Option Set", order = 2)]
public class EncounterDialogOptionSet : ScriptableObject {
/// <summary>Designer-facing identifier for this option set. The asset file renames to match.</summary>
public string id;
/// <summary>Ordered list of options presented to the player.</summary>
public List<EncounterDialogOption> options;
#if UNITY_EDITOR
private void OnValidate() {
if(string.IsNullOrEmpty(id)) {
return;
}
// Deferred — AssetDatabase calls are unsafe from OnValidate.
UnityEditor.EditorApplication.delayCall += RenameToMatchId;
}
private void RenameToMatchId() {
if(this == null || string.IsNullOrEmpty(id)) {
return;
}
var path = UnityEditor.AssetDatabase.GetAssetPath(this);
if(string.IsNullOrEmpty(path)) {
return;
}
if(name == id) {
return;
}
var error = UnityEditor.AssetDatabase.RenameAsset(path, id);
if(!string.IsNullOrEmpty(error)) {
Debug.LogWarning($"[EncounterDialogOptionSet] Could not rename '{path}' to '{id}': {error}", this);
}
}
#endif
}
}