As of my last update in April 2023, Unity does support C# 9 features, but the integration of C# 9 Source Generators in Unity is not straightforward and may not be fully supported. C# Source Generators, introduced in C# 9, are a powerful feature of the Roslyn compiler platform, allowing for the generation of additional C# source files during compilation.
Here are some points to consider:
-
Compatibility with Unity’s Compiler: Unity uses its own compilation process, which historically has lagged behind the latest C# features supported by the latest .NET compilers. Even though Unity supports C# 9, its compiler may not fully support all Roslyn features, including Source Generators.
-
Integration Challenges: To effectively use Source Generators in Unity, they need to integrate seamlessly with Unity’s build and compilation process. This integration can be complex, as Unity handles script compilation in a specific way that’s different from a typical .NET project.
-
Limited Documentation and Community Usage: There might be limited documentation and community examples of using C# 9 Source Generators within Unity, as this is a relatively new and advanced feature.
-
Alternative Approaches: Depending on what you aim to achieve with Source Generators, there may be alternative approaches more compatible with Unity. For example, using T4 templates, custom editor scripts, or other code generation tools that are known to work well within the Unity ecosystem.
-
Future Support: Unity is continually updating its support for C#, so it’s possible that future versions of Unity will offer better support for features like Source Generators.
If you are keen on experimenting with Source Generators in Unity, it would be a good idea to start with a small, isolated test project to assess compatibility and workflow. Also, keep an eye on Unity’s official documentation and forums for any updates or community-contributed solutions regarding the integration of C# 9 Source Generators.