using UnityEngine; using Bekkoloco.QuickTile; public class RailSpawner : MonoBehaviour { public QuickTilemapEditor level; // Assign at least two markers, in route order. public Transform[] anchors; // At Scale x1: bar diameter = width * 0.20 metres. [Range(0.05f, 10f)] public float width = 1f; [Range(1, 20)] public int subdivisions = 8; [ContextMenu("Spawn rail")] public void Spawn() { if (level == null || level.targetTilemap == null || anchors == null || anchors.Length < 2) { Debug.LogWarning("Assign a configured level and at least two markers.", this); return; } foreach (var anchor in anchors) { if (anchor == null) { Debug.LogWarning("Assign every marker before spawning.", this); return; } } for (int i = 1; i < anchors.Length; i++) { if ((anchors[i].position - anchors[i - 1].position).sqrMagnitude < 0.0001f) { Debug.LogWarning("Separate consecutive markers.", this); return; } } var path = QuickTilemapEditor.CreateDefaultPath( QuickTilemapEditor.PathType.Track, null); path.trackStyle = TrackStyle.Rail; path.enableTrackMesh = true; path.trackWidth = Mathf.Clamp(width, 0.05f, 10f); path.trackSubdivisions = Mathf.Clamp(subdivisions, 1, 20); for (int i = 0; i < anchors.Length; i++) { // Preserves world XYZ; free rail points do not snap to terrain. level.InsertPathWorldPoint(path, i, anchors[i].position); } int pathIndex = level.InsertAuthoredPath(path); level.RebuildPathMesh(pathIndex); } }