Files
nightclub-arcadia/NightclubArcadia/Assets/Editor/CharacterSetup.cs
T
lennartandClaude Opus 5 9a0618fd31 replace the NPC scaffold with data-driven prefabs, and retire the builders
NPCStandIn was written to be dropped on a sphere and carried its identity in
per-instance public fields. The cost of that showed up in the scene itself: of
the two spheres, one was the Bartender and the other was named "Bartender (1)"
in the hierarchy while being configured as the Chair — data changed, name never
was. Nothing outside the scene could see what existed, and two copies could
disagree.

Identity now lives in an InteractableDefinition asset and the scene only places
a prefab. The migration matched the old objects by yarn node rather than by
name, which is the only reason the chair ended up as the chair.

InteractableDefinition rather than the planned "character definition": it covers
a character and a talkable object equally, so Bartender.prefab sits under
Prefabs/Characters and Chair.prefab under Prefabs/Interactables.

Three components collapse into one. DialogueInteractable was in no scene at all
— a dead trigger-zone variant of the same idea. ClickInteractableBridge existed
only to adapt NPCStandIn without modifying it, a constraint that died with
NPCStandIn; DialogueInteractor implements IClickInteractable itself. The parts
worth keeping were lifted out rather than discarded: WorldSpaceLabel and
InteractionPrompt are now normal components on prefab children, so a label can
be seen and positioned in the Editor instead of existing only at runtime.
InputDeviceTracker moved into NightclubArcadia.Core and gained a namespace,
leaving no global-namespace runtime types.

Characters get the same pipeline as Skills and Candidates:
Assets/Characters/characters.json is the source of truth, CharacterSetup
regenerates the definition assets on script reload and removes orphans. Adding
an NPC is a JSON entry plus a prefab — no code, and no Unity for the data half.

Phase 5.4 deletes the one-shot builders rather than disabling them. UILayerSetup,
PlayerControlSetup, UILayerSetupAutoRun and YarnDemoSetup assumed the
single-scene layout, had already destroyed a level scene and turned Systems
binary once each, and after NPCStandIn went they no longer compiled. The prefabs
they used to generate are authored and committed; the wiring knowledge is in
those prefabs and in git history. The asset generators — Skill, Candidate,
Character — are untouched and still work.

EditMode 42/42, PlayMode 7/7, YarnCheck 9 files / 35 nodes. All three scenes
still text.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-08-25 21:53:06 +02:00

145 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NightclubArcadia.Interaction;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
namespace NightclubArcadia.EditorTools
{
/// <summary>
/// Regenerates InteractableDefinition assets from Assets/Characters/characters.json.
///
/// The JSON is the source of truth and the .asset files are build output; hand edits
/// to them are overwritten. Same contract as Skills and Candidates, deliberately —
/// a writer adding a character edits one JSON file and never opens Unity.
///
/// This exists because the identity of an NPC used to live in public fields on a
/// scene object, where nothing outside that scene could see it and two copies could
/// disagree. They did: the scene's second "Bartender" was the chair.
/// </summary>
public static class CharacterSetup
{
const string JsonPath = "Assets/Characters/characters.json";
const string DefinitionsFolder = "Assets/Characters/Definitions";
[Serializable]
class Dto
{
public string id;
public string displayName;
public string yarnNode;
public float interactionRange = 3f;
public bool showLabel = true;
}
[Serializable]
class DtoList
{
public Dto[] interactables;
}
[MenuItem("Tools/Nightclub Arcadia/Set Up Characters")]
public static void SetUpCharacters() => Regenerate(verbose: true);
[DidReloadScripts]
static void OnScriptsReloaded() => Regenerate(verbose: false);
static void Regenerate(bool verbose)
{
if (!File.Exists(JsonPath))
{
if (verbose)
{
Debug.LogError($"[Characters] Missing {JsonPath}");
}
return;
}
DtoList parsed;
try
{
parsed = JsonUtility.FromJson<DtoList>(File.ReadAllText(JsonPath));
}
catch (Exception e)
{
Debug.LogError($"[Characters] {JsonPath} is not valid JSON: {e.Message}");
return;
}
if (parsed?.interactables == null || parsed.interactables.Length == 0)
{
Debug.LogError($"[Characters] {JsonPath} defines no interactables.");
return;
}
var duplicates = parsed.interactables.GroupBy(i => i.id).Where(g => g.Count() > 1).ToList();
if (duplicates.Count > 0)
{
Debug.LogError("[Characters] Duplicate ids: " +
string.Join(", ", duplicates.Select(g => g.Key)));
return;
}
if (!AssetDatabase.IsValidFolder(DefinitionsFolder))
{
AssetDatabase.CreateFolder("Assets/Characters", "Definitions");
}
var live = new HashSet<string>(StringComparer.Ordinal);
var changed = new List<string>();
foreach (var dto in parsed.interactables)
{
if (string.IsNullOrWhiteSpace(dto.id))
{
Debug.LogError("[Characters] An entry has no id; skipped.");
continue;
}
live.Add(dto.id);
var path = $"{DefinitionsFolder}/{dto.id}.asset";
var asset = AssetDatabase.LoadAssetAtPath<InteractableDefinition>(path);
if (asset == null)
{
asset = ScriptableObject.CreateInstance<InteractableDefinition>();
AssetDatabase.CreateAsset(asset, path);
changed.Add(dto.id);
}
else if (asset.DisplayName != dto.displayName || asset.YarnNode != dto.yarnNode ||
!Mathf.Approximately(asset.InteractionRange, dto.interactionRange) ||
asset.ShowLabel != dto.showLabel)
{
changed.Add(dto.id);
}
asset.EditorSet(dto.id, dto.displayName, dto.yarnNode, dto.interactionRange, dto.showLabel);
EditorUtility.SetDirty(asset);
}
// An id removed from the JSON should not leave an orphan asset behind that a
// prefab could still be pointing at.
foreach (var guid in AssetDatabase.FindAssets("t:InteractableDefinition", new[] { DefinitionsFolder }))
{
var path = AssetDatabase.GUIDToAssetPath(guid);
var name = Path.GetFileNameWithoutExtension(path);
if (!live.Contains(name))
{
Debug.LogWarning($"[Characters] Removing orphan definition '{name}' — no longer in the JSON.");
AssetDatabase.DeleteAsset(path);
}
}
AssetDatabase.SaveAssets();
if (verbose || changed.Count > 0)
{
Debug.Log($"[Characters] {parsed.interactables.Length} definition(s) in {DefinitionsFolder}" +
(changed.Count > 0 ? $"; updated: {string.Join(", ", changed)}" : "; no changes"));
}
}
}
}