Unity Playworks Plugin Compiler V2
Playworks Plugin now has support of C# 8.0 and C# 9.0 features. This option is disabled by default and could be activated in Playworks Plugin Plugin window.
When to use?
If you know, that you use C# 8.0 or C# 9.0 features in project - enabling of Unity Playworks Plugin Compiler v2 is recommended.
Popular C# 8.0 and C# 9.0 features:
- Pattern matching
if(fruit is Apple)
{
MakeApplePieFrom(fruit as Apple);
}
- Using declarations
static int WriteLinesToFile(IEnumerable<string> lines)
{
using var file = new System.IO.StreamWriter("WriteLines2.txt");
int skippedLines = 0;
foreach (string line in lines)
{
if (!line.Contains("Second"))
{
file.WriteLine(line);
}
else
{
skippedLines++;
}
}
// Notice how skippedLines is in scope here.
return skippedLines;
// file is disposed here
}
- Null-coalescing assignment
List<int> numbers = null;
int? i = null;
numbers ??= new List<int>();
numbers.Add(i ??= 17);
numbers.Add(i ??= 20);
Console.WriteLine(string.Join(" ", numbers)); // output: 17 17
Console.WriteLine(i); // output: 17
- Enhancement of interpolated verbatim strings
var myString1 = $@"...";
var myString2 = @$"...";
If you see errors on Unity Playworks Plugin Compiler v1 (default compiler), then enabling of Unity Playworks Plugin Compiler v2 could solve the issue:
- Feature is not available in C# 7.0. Please use language version 8.0 or greater.
- An expression of type MyCustomType cannot be handled by a pattern of type “not null” in C# 8.0. Please use language version 9.0 or greater.
- Tuple element name MyElementName is inferred. Please use language version 9.0 or greater to access an element by its inferred name.
- Invalid MyCustomType value: null for C# 8.0. Please use language version 9.0 or greater.
Supported Unity versions
Option is not available on Unity 2019.X - 2020.1.
C# 8.0 works on Unity 2020.2 or higher.
C# 9.0 works on Unity 2021.2 or higher.
Tools
Unity Playworks Plugin Compiler v2 uses Mono Runtime, MSBuild and Roslyn compiler directly from Unity Editor installation, so no additional tools required.
How it affects your playable?
- With Unity Playworks Plugin Compiler v2 you could use newest C# features.
- With Unity Playworks Plugin Compiler v2 build time slightly increased.
- With Unity Playworks Plugin Compiler v2 all Unity Playworks Plugin types now arent exposed to user (see Caveats section for more details).
Supported features
Playworks Plugin Plugin supports almost all C# 9.0 features, which Unity supports. Full list of tested features you could find below:
C# 8.0 Features
Feature | Support |
---|---|
Null-coalescing assignment | Supported |
Pattern-matching enhancements | Supported |
Readonly members | Supported |
Static local functions | Supported |
Using declarations | Supported |
Enhancement of interpolated verbatim strings | Supported |
Disposable ref structs | Unsupported |
Stackalloc in nested expressions | Unsupported |
Unmanaged constructed types | Unsupported |
Default interface methods | Unsupported (Unity does not support) |
Indices and ranges | Unsupported (Unity does not support) |
Asynchronous streams | Unsupported (Unity does not support) |
Asynchronous disposable | Unsupported (Unity does not support) |
C# 9.0 Features
Feature | Support |
---|---|
Lambda discard parameters | Supported |
Target-typed new | Supported |
Pattern-matching enhancements | Supported |
Static anonymous functions | Supported |
Lambda discard parameters | Supported |
Target-typed conditional expression | Unsupported |
Local function attribute | Unsupported |
Native-sized integers | Unsupported |
Skip localsinit | Unsupported |
Records | Unsupported |
Init-only setters | Unsupported (Unity does not support) |
Covariant returns | Unsupported (Unity does not support) |
Top-level statements | Unsupported |
Function Pointers | Unsupported (Unity does not support) |
Caveats
Unity Playworks Plugin specific types
In some rare cases, your code could use Playworks Plugin Plugin internal classes (due to custom package or any kind of custom changes in your project) such as ElementComponent class, so, it could not be exposed as public anymore. Such issues have workarounds, you could ask for help from our support.