CrowRx.Data.SourceGenerator
1.0.2
dotnet add package CrowRx.Data.SourceGenerator --version 1.0.2
NuGet\Install-Package CrowRx.Data.SourceGenerator -Version 1.0.2
<PackageReference Include="CrowRx.Data.SourceGenerator" Version="1.0.2" />
<PackageVersion Include="CrowRx.Data.SourceGenerator" Version="1.0.2" />
<PackageReference Include="CrowRx.Data.SourceGenerator" />
paket add CrowRx.Data.SourceGenerator --version 1.0.2
#r "nuget: CrowRx.Data.SourceGenerator, 1.0.2"
#:package CrowRx.Data.SourceGenerator@1.0.2
#addin nuget:?package=CrowRx.Data.SourceGenerator&version=1.0.2
#tool nuget:?package=CrowRx.Data.SourceGenerator&version=1.0.2
CrowRx.Data.SourceGenerator
A Roslyn Source Generator for the CrowRx.Data ecosystem. It automates the registration and initialization of data targets and sources, ensuring high-performance data routing with zero manual configuration.
Features
- Automatic Discovery: Automatically finds all public, non-abstract classes implementing CrowRx.Data.ITarget.
- Relationship Mapping: Detects ISource types from ITarget<TSource> interfaces, including support for arrays and List<T>.
- Unity Lifecycle Integration: Automatically executes initialization after assemblies are loaded in Unity using [RuntimeInitializeOnLoadMethod].
- Zero Boilerplate: Generates the necessary Managed<T>.Init() and Couple<S, T>.Init() calls at compile time.
API Reference
1. Generation Logic
The generator scans the compilation for types that satisfy the following:
- Interface Implementation: Must implement CrowRx.Data.ITarget.
- Accessibility: Must be public.
- Concrete Type: Must not be abstract, an interface, or a generic type definition.
2. Type Extraction Rules
When an ITarget<TSource> interface is found, the generator identifies the underlying source type:
- Direct Type: ITarget<MySource> registers MySource.
- Arrays: ITarget<MySource[]> unwraps to register MySource.
- Generic Collections: ITarget<List<MySource>> (or any single-argument generic) unwraps to register MySource.
3. Generated Hub (Generated.ManagedType)
A static class generated to handle system startup.
- ManagedType.Initialize(): The main entry point that initializes all discovered targets and their relationships.
- In Unity 6.0+: Automatically called via [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)].
- Manual Call: Can be called manually in non-Unity environments to trigger registration.
Examples
Implementation Example
Define your data and targets normally. No extra code is needed for registration.
using CrowRx.Data;
// Define a Source
public struct InventorySource : ISource {
public string ItemName;
}
// Define a Target
public class InventoryUI : ITarget<InventorySource> {
public bool UpdateBy(in InventorySource source) {
UnityEngine.Debug.Log($"Received item: {source.ItemName}");
return true;
}
}
Generated Code Structure
The generator will automatically output a file ManagedType.g.cs:
// <auto-generated />
using CrowRx.Data;
#if UNITY_6000_0_OR_NEWER
using UnityEngine;
#endif
namespace Generated {
internal static class ManagedType {
#if UNITY_6000_0_OR_NEWER
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
#endif
public static void Initialize() {
#region generated managed
// Initializes the target manager
Managed<Global.InventoryUI>.Init();
// Maps the source to the target
Couple<Global.InventorySource, Global.InventoryUI>.Init();
#endregion
}
}
}
Requirements
- Unity 6.0 or newer (for automatic execution).
- .NET Standard 2.0 (compatible with most Unity environments).
- CrowRx.Data: The runtime library must be present.
License
This project is licensed under the MIT License.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.3.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on CrowRx.Data.SourceGenerator:
| Package | Downloads |
|---|---|
|
CrowRx.Data
A global reactive system library for CrowRx. This package is intended for use with Unity3D 6.0 or newer only. |
GitHub repositories
This package is not used by any popular GitHub repositories.