A code generation tool for Unity.
Tested Version: Unity - 2022.3
Roslyn Version: 3.8
- Import the dll into a folder under Assets.
- In the Inspector:
- Select Platforms → Disable Any Platform
- Under Include Platforms, disable Editor and Standalone (i.e., disable all platform-related tools).
- Create a label named
RoslynAnalyzerin Asset Labels (case-sensitive).
- You can limit the analyzer's scope using assembly definitions to target specific parts of your code. Unity applies analyzers to:
- All assemblies in the project's Assets folder, or
- Assemblies in subfolders where no parent folder contains an assembly definition file.
- If an analyzer is placed in a folder with an assembly definition (or its subfolders), it only applies to assemblies generated from that definition and any assemblies referencing it. (Adapted from Unity Documentation)
Purpose: Use the [Register("xxx")] attribute to register subclasses into a shared dictionary within a parent class.
public class RegisterAttribute : Attribute
{
// The name of the generated Helper class
public string RegisterTypeName;
public RegisterAttribute(string registerTypeName)
{
RegisterTypeName = registerTypeName;
}
}namespace CodeGen_Register
{
// Constants; only the variable name will be read
public static class Magic
{
public const string BaseSample1 = nameof(BaseSample1);
}
// Register accepts two types of values:
// 1. Constants (uses the variable name)
public class BaseSample1 { }
[Register(Magic.BaseSample1)]
public class SampleA : BaseSample1 { }
[Register(Magic.BaseSample1)]
public class SampleB : BaseSample1 { }
// Generated Code:
/*
using System.Collections.Generic;
namespace CodeGen_Register {
public static partial class BaseSample1Helper {
public static Dictionary<string, BaseSample1> Type = new Dictionary<string, BaseSample1>() {
{ "SampleA", new SampleA() },
{ "SampleB", new SampleB() }
};
}
}
*/
// 2. String literals (uses the literal value)
public class BaseSample2 { }
[Register("BaseSample2")]
public class SampleA2 : BaseSample2 { }
[Register("BaseSample2")]
public class SampleB2 : BaseSample2 { }
// Generated Code:
/*
using System.Collections.Generic;
namespace CodeGen_Register {
public static partial class BaseSample2Helper {
public static Dictionary<string, BaseSample2> Type = new Dictionary<string, BaseSample2>() {
{ "SampleA2", new SampleA2() },
{ "SampleB2", new SampleB2() }
};
}
}
*/
public class Main
{
void Main()
{
// If generated successfully, you can use them directly here
var list1 = BaseSample1Helper.Type;
var list2 = BaseSample2Helper.Type;
}
}
}
Import EventBus of Arch.Extension. It is high performance! Arch.Extension
public partial class MyInstanceReceiver {
public MysInstanceReceiver(){ Hook(); } // To start listening.
[Event(order...)]
public void OnShootEvent(ref ShootEvent @event){
// Handle
}
... Other event receiving methods
}
public static class SomeEventHandler{
[Event(order: ...)]
public static void OnShootFireBullets(ref ShootEvent event){ // ref, none, in supported and all types as a event. Only one param!
// Do stuff
}
...
}
var shootEvent = ...;
EventBus.Send(ref shootEvent); // Broadcasts the event to all annotated static methods wherever they are.