quot;Origin: {svc.LastOrigin} Player: {svc.PlayerId}\n{(svc.LastError != null ? "Last error: " + svc.LastError : "")}"; _list.Clear(); foreach (var k in new List<TuningKey>(values.Keys)) { var row = new VisualElement { style = { flexDirection = FlexDirection.Row, marginTop = 2 } }; var src = values.SourceOf(k.Key); row.Add(new Label(quot;{k.Key} [{src}]") { style = { color = src == TuningLayer.LocalOverride ? Color.yellow : Color.white, width = 230 } }); var field = new TextField { value = values.Raw(k.Key) }; field.style.flexGrow = 1; var key = k.Key; field.RegisterCallback<FocusOutEvent>(_ => { values.SetOverride(key, field.value); Rebuild(); }); row.Add(field); _list.Add(row); } } } ``` - **F8** toggles it (Input System; see [[Input System]] if your project still uses the old Input Manager). - Each row shows `key [Source]`, with local overrides in yellow, and a text field. Type a value and click away: it becomes a `LocalOverride`, clamped and type-checked like any other value. Invalid text is ignored. - **Refresh** calls `RefreshNow()` (rate-limited to once per 5 s by `RefreshPolicy`). **Clear overrides** drops back to remote, cached or default values. - The header shows the last `ConfigOrigin`, the player ID (handy for checking rollout buckets) and the last error. - In release builds the `#if !(UNITY_EDITOR || DEVELOPMENT_BUILD)` branch disables the component, so players never see it. The code is still compiled in; strip the whole GameObject from release scenes if you'd rather not ship it. [[In-Game Debug Console and Cheat Tools]] covers that pattern in depth. Setup: 1. **GameObject > UI Toolkit > UI Document**. Assign a **Panel Settings** asset (create one with **Create > UI Toolkit > Panel Settings Asset** if you don't have one). 2. Add **Tuning Debug Panel** to the same GameObject. 3. Press Play and press F8. *(Untested: layout and readability at different resolutions. Overrides are not saved; they vanish on restart, which is deliberate.)* ## 5.2 Tests Put the pure scripts (`TuningValues.cs`, `RefreshPolicy.cs`, `Rollout.cs`) in an assembly your test assembly can reference, as shown in [[Unity Test Framework]], or keep everything in the default `Assembly-CSharp` and use an Edit Mode test assembly that references it. Then add: **`TuningTests.cs`** ```csharp using System.Collections.Generic; using NUnit.Framework; public class TuningTests { static TuningValues Make() { var v = new TuningValues(); v.Register(new TuningKey("spawn_interval", TuningKey.Kind.Float, "2.5", 0.5, 10)); v.Register(new TuningKey("rollout", TuningKey.Kind.Int, "0", 0, 100)); v.Register(new TuningKey("event", TuningKey.Kind.Bool, "false")); return v; } [Test] public void DefaultsApplyBeforeAnyFetch() { var v = Make(); Assert.AreEqual(2.5f, v.GetFloat("spawn_interval")); Assert.AreEqual(TuningLayer.BuildDefault, v.SourceOf("spawn_interval")); } [Test] public void RemoteBeatsCachedBeatsDefault() { var v = Make(); v.ApplyLayer(TuningLayer.Cached, new Dictionary<string, string> { ["spawn_interval"] = "3" }); Assert.AreEqual(3f, v.GetFloat("spawn_interval")); v.ApplyLayer(TuningLayer.Remote, new Dictionary<string, string> { ["spawn_interval"] = "4" }); Assert.AreEqual(4f, v.GetFloat("spawn_interval")); Assert.AreEqual(TuningLayer.Remote, v.SourceOf("spawn_interval")); } [Test] public void OverrideWinsAndClears() { var v = Make(); v.ApplyLayer(TuningLayer.Remote, new Dictionary<string, string> { ["spawn_interval"] = "4" }); v.SetOverride("spawn_interval", "1"); Assert.AreEqual(1f, v.GetFloat("spawn_interval")); v.ClearOverrides(); Assert.AreEqual(4f, v.GetFloat("spawn_interval")); } [Test] public void OutOfRangeIsClamped() { var v = Make(); v.ApplyLayer(TuningLayer.Remote, new Dictionary<string, string> { ["spawn_interval"] = "0.01", ["rollout"] = "250" }); Assert.AreEqual(0.5f, v.GetFloat("spawn_interval")); Assert.AreEqual(100, v.GetInt("rollout")); } [Test] public void GarbageAndUnknownKeysRejectedNotApplied() { var v = Make(); v.ApplyLayer(TuningLayer.Remote, new Dictionary<string, string> { ["spawn_interval"] = "lots", ["typo_key"] = "1", ["event"] = "True" }); Assert.AreEqual(2.5f, v.GetFloat("spawn_interval")); Assert.AreEqual(2, v.Rejected.Count); Assert.IsTrue(v.GetBool("event")); } [Test] public void NaNRejected() { var v = Make(); v.ApplyLayer(TuningLayer.Remote, new Dictionary<string, string> { ["spawn_interval"] = "NaN" }); Assert.AreEqual(2.5f, v.GetFloat("spawn_interval")); } [Test] public void ChangedOnlyListsRealChanges() { var v = Make(); List<string> got = null; v.Changed += k => got = new List<string>(k); v.ApplyLayer(TuningLayer.Remote, new Dictionary<string, string> { ["spawn_interval"] = "2.5", ["rollout"] = "10" }); Assert.AreEqual(1, got.Count); Assert.AreEqual("rollout", got[0]); got = null; v.ApplyLayer(TuningLayer.Remote, new Dictionary<string, string> { ["spawn_interval"] = "2.5", ["rollout"] = "10" }); Assert.IsTrue(got == null); } [Test] public void RemovedRemoteKeyFallsBackToCachedThenDefault() { var v = Make(); v.ApplyLayer(TuningLayer.Remote, new Dictionary<string, string> { ["rollout"] = "10" }); v.ApplyLayer(TuningLayer.Remote, new Dictionary<string, string>()); Assert.AreEqual(0, v.GetInt("rollout")); } [Test] public void RolloutIsStableAndProportional() { Assert.AreEqual(Rollout.Bucket("player-1", "hud"), Rollout.Bucket("player-1", "hud")); var on = 0; for (var i = 0; i < 10000; i++) if (Rollout.IsEnabled("p" + i, "hud", 25)) on++; Assert.IsTrue(on > 2300 && on < 2700); Assert.IsTrue(Rollout.IsEnabled("x", "hud", 100)); Assert.IsTrue(!Rollout.IsEnabled("x", "hud", 0)); } [Test] public void RolloutRaisingPercentKeepsEarlierPlayers() { for (var i = 0; i < 1000; i++) if (Rollout.IsEnabled("p" + i, "hud", 10)) Assert.IsTrue(Rollout.IsEnabled("p" + i, "hud", 30)); } [Test] public void RefreshPolicyStartupOnceResumeAfterInterval() { var p = new RefreshPolicy(300, 1800); Assert.IsTrue(p.ShouldFetch(0, RefreshPolicy.Reason.Startup)); p.Started(0); p.Succeeded(); Assert.IsTrue(!p.ShouldFetch(10, RefreshPolicy.Reason.Startup)); Assert.IsTrue(!p.ShouldFetch(100, RefreshPolicy.Reason.Resume)); Assert.IsTrue(p.ShouldFetch(301, RefreshPolicy.Reason.Resume)); Assert.IsTrue(!p.ShouldFetch(1000, RefreshPolicy.Reason.Timer)); Assert.IsTrue(p.ShouldFetch(1801, RefreshPolicy.Reason.Timer)); } [Test] public void RefreshPolicyBacksOffAndBlocksWhileInFlight() { var p = new RefreshPolicy(); p.Started(0); Assert.IsTrue(!p.ShouldFetch(1, RefreshPolicy.Reason.Manual)); p.Failed(); Assert.IsTrue(!p.ShouldFetch(20, RefreshPolicy.Reason.Timer)); Assert.IsTrue(p.ShouldFetch(31, RefreshPolicy.Reason.Timer)); p.Started(31); p.Failed(); Assert.IsTrue(!p.ShouldFetch(80, RefreshPolicy.Reason.Timer)); Assert.IsTrue(p.ShouldFetch(92, RefreshPolicy.Reason.Timer)); } } ``` What they prove: | Test | Behaviour | |---|---| | DefaultsApplyBeforeAnyFetch | The game works with zero network | | RemoteBeatsCachedBeatsDefault | Layer order | | OverrideWinsAndClears | The debug panel can't get stuck | | OutOfRangeIsClamped | Dashboard typos are contained | | GarbageAndUnknownKeysRejectedNotApplied | Wrong types and unknown keys are reported, not applied; `True` from Newtonsoft is accepted | | NaNRejected | `NaN` never reaches gameplay | | ChangedOnlyListsRealChanges | Identical refreshes don't wake listeners | | RemovedRemoteKeyFallsBackToCachedThenDefault | Deleting a key means "back to normal" | | RolloutIsStableAndProportional | Same bucket every time; about 25 % of 10,000 IDs at 25 % | | RolloutRaisingPercentKeepsEarlierPlayers | Raising a rollout never removes anyone | | RefreshPolicyStartupOnceResumeAfterInterval | No fetch storms on resume | | RefreshPolicyBacksOffAndBlocksWhileInFlight | Backoff after failures, one fetch at a time | All 12 pass. They were run outside Unity (on .NET 10 with a small NUnit stand-in) and compiled against Unity's own NUnit; they haven't been run in the Unity Test Runner. ## 5.3 Pitfalls 1. **Waiting for the fetch before showing the menu.** Players on bad connections stare at a spinner, and the store review team sees it too. Start on defaults and the cache. 2. **Culture bugs.** `float.Parse("1.5")` on a German system returns 15. Parse and format everything with `CultureInfo.InvariantCulture`, as `TuningKey` does. 3. **Reading values every frame by key.** String lookups each frame cost time and let values change mid-calculation. Copy on `Changed`. 4. **Forgetting to unsubscribe.** Every listener needs a matching `-=` in `OnDisable`. 5. **Renaming keys.** Old builds keep asking for the old name. Add new keys; retire old ones only when no supported build uses them. 6. **Testing in production.** Use a development environment, then a small Game Override. 7. **Secrets in config.** Everything you send is readable by players. 8. **Trusting the client.** A modded client can ignore Remote Config entirely. Anything competitive needs server-side checks. 9. **Unbounded values.** Every number gets a range in the schema. 10. **Too many keys.** Each key is a promise to keep supporting it. Tune multipliers, not every individual number. 11. **No change log.** Write down each production change with a reason. 12. **Rollout buckets from `GetHashCode`.** Use a fixed hash like FNV-1a. ## 5.4 Where to go next - Feed the tuned values into the curves from [[Game Balance and Dynamic Difficulty]]. - Measure the effect with [[Playtest Telemetry and Heatmaps]] before rolling out widely. - Revisit the Observer and Service Locator chapters of [[Game Programming Patterns (Review)]]; this series uses both. - [[Level Up! (Review)]] on iteration: tune, watch, tune again. Back to [[Remote Config and Live Tuning]].