Spire C# – Exhaustive Matching and Discriminated/Tagged Unions
Offline documentation and runtime API tooltips. Optional Quick Start sample. ... Installation ... Unity 2022 serialization: generic unions cannot use native Unity serialization(Unity ...
by Houtamelo
Price History +
Spire brings exhaustive matching and discriminated unions to Unity. Catch forgotten cases as your game evolves, generate missing branches in your IDE, and give each enum variant the data it needs.
Catch missing cases before you press Play
Use exhaustive matching with enums, classes, interfaces, structs, and discriminated unions. Spire checks switch statements/expressions, reports a compile-time error when cases are missing, and lists exactly which cases you forgot. No `default:` or catch-all case is required.
Added a new class implementing an interface? `Spire` immediately reports the switches that need updating. The same applies when you add an enum member or a union variant.
Follow the diagnostics straight to the affected code. Use the IDE quick fix to generate the unhandled cases, then fill in their behavior.
Give each variant its own data
Have you ever looked at an enum and wished you could attach different data to each variant? That's exactly what a discriminated union provides!
Separate logic from data, let your systems react to the same command, state, or result in their own way.
Spire's struct-based unions are lightweight, with compact, allocation-free type-safe storage (No unsafe code!).
Author unions in the Unity Inspector
Add `[Serializable]` to your union and use it in serialized fields. Spire generates the variant picker and editable fields for the selected variant, including inside collections.
Compatible with Odin Inspector/Serializer. Odin is optional.
Generate JSON serialization and deserialization
Spire generates converters for System.Text.Json and/or Newtonsoft.Json. No additional setup needed.
Use typed polymorphic data in Burst and DOTS
Bring struct-based discriminated unions into Burst jobs and DOTS. Store your polymorphic data in NativeArray<T>/NativeList<T>, switch on their variants, and deconstruct their payloads inside your job.
Included
- Runtime library, analyzers, source generator, and IDE quick fixes.
- Generated Unity serialization and Inspector integration.
- Offline documentation and runtime API tooltips.
- Optional Quick Start sample.
- Optional Unity Showcase sample with a runnable scene, Inspector-editable presets, and tests.
Installation
Standard installation via Unity's Package Manager.
For Rider support, make sure that Roslyn Analyzers are enabled: Editor > Inspection Settings > Roslyn Analyzers.
Technical details
- Validated Unity versions: 2022.3.62f3, 2023.2.22f1, and 6000.5.6f1.
- Unity 2020 & 2021: will not work (verified), earlier versions most likely won't work as well.
- IDE support: Spire uses Roslyn's standard analysis/code-fix providers, any IDE that supports that should also support Spire. Verified in JetBrains Rider and Visual Studio Community.
- Tested platforms and backends: Linux x64 and Windows 10 x64 on all 3 Unity versions.
- Tested Web builds: WebGL2/IL2CPP on all 3 versions in Chrome and Firefox.
- Unity 2022 serialization: generic unions cannot use native Unity serialization(Unity limitation).
- JSON: Spire supports System.Text.Json and Newtonsoft.Json.
- Burst/DOTS: Spire does not have code that specifically targets Burst/DOTS, our discriminated unions are naturally Burst/DOTS compatible since they are struct-based. Note that the fields of the variants must also comply with Burst's restrictions (Spire isn't magic!). Consult the included Usage and Serialization guides for payload and serialization choices.
- Unity 6000.6.5f1 limitation: an engine serialization bug affects generic unions with reference-type arguments, such as Command<string>. See issue tracker . This crash has been fixed in Unity 6.7 onwards.
- License: this Asset Store distribution of Spire uses the standard Unity Asset Store End User License Agreement.
- Third-party notice: includes compiler compatibility definitions generated by PolySharp under the MIT license. See Third Party Notices in the package for details.
- Also works on 7000.0.0a12, official support will be provided once a LTS 7000.X version is released.