Skip to main content

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.

images-medium

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

FeatureSupport
Null-coalescing assignmentSupported
Pattern-matching enhancementsSupported
Readonly membersSupported
Static local functionsSupported
Using declarationsSupported
Enhancement of interpolated verbatim stringsSupported
Disposable ref structsUnsupported
Stackalloc in nested expressionsUnsupported
Unmanaged constructed typesUnsupported
Default interface methodsUnsupported (Unity does not support)
Indices and rangesUnsupported (Unity does not support)
Asynchronous streamsUnsupported (Unity does not support)
Asynchronous disposableUnsupported (Unity does not support)

C# 9.0 Features

FeatureSupport
Lambda discard parametersSupported
Target-typed newSupported
Pattern-matching enhancementsSupported
Static anonymous functionsSupported
Lambda discard parametersSupported
Target-typed conditional expressionUnsupported
Local function attributeUnsupported
Native-sized integersUnsupported
Skip localsinitUnsupported
RecordsUnsupported
Init-only settersUnsupported (Unity does not support)
Covariant returnsUnsupported (Unity does not support)
Top-level statementsUnsupported
Function PointersUnsupported (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.