https://github.com/needle-tools/unity-type-merger
Merge multiple classes into one using SourceGenerators
https://github.com/needle-tools/unity-type-merger
Last synced: 9 months ago
JSON representation
Merge multiple classes into one using SourceGenerators
- Host: GitHub
- URL: https://github.com/needle-tools/unity-type-merger
- Owner: needle-tools
- Created: 2023-03-19T15:36:22.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-26T19:50:45.000Z (about 3 years ago)
- Last Synced: 2025-04-24T08:04:51.484Z (about 1 year ago)
- Language: C#
- Homepage:
- Size: 428 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Unity Type Merger
Merge multiple classes into one using SourceGenerators
> ⚠ This is very experimental
## How it works:
1) Mark your class or component as `partial`
2) Add one or multiple `MergeClass` attributes to your class
3) Insert a list of type names that you want to be merged into that class or component
Example Class:
```csharp
namespace MyNamespace
{
[MergeClass("MyNamespace.MyOtherType", "MyNamespace.SomeOtherBehaviour")]
[MergeClass("Partials.SomeThirdType")]
public partial class TestComponent
{
public string SomeString = "Hello World";
}
}
```
Resulting Sourcegen:
```csharp
namespace MyNamespace
{
public partial class TestComponent : MonoBehaviour, IMyInterface // ScriptableObject
{
// Begin MyOtherType
public string HelloWorld = "test123";
public void MyInterfaceMethod()
{
}
// End MyOtherType
// Begin SomeOtherBehaviour
public bool Active = true;
public void Update()
{
if (Active)
Debug.Log("IT WORKS");
}
// End SomeOtherBehaviour
// Begin SomeThirdType
public void MySpecialMethod()
{
Debug.Log("Hello from some third type");
}
// End SomeThirdType
}
}
```
Final component:

## Limitations
- Multiple base classes are not supported (e.g. for multiple partials deriving from different classes only the first one will be used)
- If the main class already has a base type and a partial also has a base type it will cause compiler errors (Sourcegen currently doesnt check that)
- If partials have member name collisions (e.g. two classes declaring a method name `MySpecialMethod` it will cause compiler errors).
- Types to merge must either be in the same assembly or the script file name must match the C# type name to merge (same as with Unity's components)
## Debugging
- See https://github.com/needle-tools/unity-analyzers-starter#debugging