Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work in Unity due to 'init' keyword in properties #113

Open
Monsoonexe opened this issue Nov 1, 2024 · 2 comments
Open

Does not work in Unity due to 'init' keyword in properties #113

Monsoonexe opened this issue Nov 1, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Monsoonexe
Copy link

Describe the bug

The readme claims that this repo can be used in Unity (the game engine, I'm assuming). However, this is not the case (tested in 2020.3). This repo uses the 'init' keyword for properties, which are explicitly not a feature supported by unity: source

Steps to reproduce the bug

Download source. Go through each folder of the 'lib' folder and copy-paste .dll into /Plugins folder in Unity. It'll compile. Now in your code, try to use any property that uses the 'init' keyword (in my case, none of the properties on DeviceData are supported. If you try to access these properties in your code, when you recompile in Unity, you'll get an error in the console about "property... is not supported by the language...". Even picking .dll from .net framework 3.5 or 4.5 has same issues.

A work-around I use is to re-write the properties as extension methods and use Reflection to call the underlying get_Name auto-property functions.

Unity 2020.3 (but the Unity 6 doc suggest this feature is still unsupported). Using Mono scripting backend 4.x.

Expected behavior

I expected the claims to be correct -- all features are supported. If some features aren't supported, please notate as such.

Screenshots

image
image

NuGet package version

Latest Source

.NET Platform

Others

Platform type

Windows

System version

No response

IDE

Visual Studio 2022

Additional context

No response

@Monsoonexe Monsoonexe added the bug Something isn't working label Nov 1, 2024
@wherewhere
Copy link
Member

wherewhere commented Nov 2, 2024

But unity said

Record support

C# 9 init and record support comes with a few caveats.

  • The type System.Runtime.CompilerServices.IsExternalInit is required for full record support as it uses init only setters, but is only available in .NET 5 and later (which Unity doesn’t support). Users can work around this issue by declaring the System.Runtime.CompilerServices.IsExternalInit type in their own projects.
  • You shouldn’t use C# records in serialized types because Unity’s serialization system doesn’t support C# records.

https://docs.unity3d.com/2021.2/Documentation/Manual/CSharpCompiler.html

Which means unity should supports init when IsExternalInit is declared.
Maybe you need to set language version to 9 or later.
Or maybe you need to compile it first.

@wherewhere wherewhere pinned this issue Nov 2, 2024
@2A5F
Copy link

2A5F commented Nov 2, 2024

add csc.rsp in assets folder

-langVersion:preview

then use https://github.com/Cysharp/CsprojModifier set lang version to 11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants