Need help converting c#6 Code to unity version.

I have some code that I need to convert to work with unity. Please help. Here is the code.

This is the main problem: This does not work in the .net 2.0 framework

private static readonly Lazy<IEnumerable<ISetupTemplate>> SetupTemplates =
            new Lazy<IEnumerable<ISetupTemplate>>(CreateTemplates);

The CreateTemplates function returns the following.

private static IEnumerable<ISetupTemplate> CreateTemplates()

Then later I should be able to iterate through all the templates like this

foreach (var template in SetupTemplates.Value)

The templates all have the : IStaticCodeBook interface.

How do I convert this code to work in Unity?
Thanks

Are you new to Unity? Do you need this to be production ready? Cos 2017.1 has support for C#6 and .NET 4.6 (although its marked as experimental; I personally haven’t had any issues with it but YMMV. Also, they said builds will be bigger since they haven’t got their code stripping done for it yet.)

Also, are you certain that this code won’t work in Unity? Cos although normal Unity is .NET 2.0, it has the equivalent feature set of .NET 3.5

2 Likes

The new thing there is “readonly”, but you can fake it a different way - by defining a getter but not a setter. It needs more setup so the simple solution to just make the line valid is removing the keyword. But yes, simply using 2017.1 gives you C# 6.0 and .NET 4.6, making life a lot easier.

You can already use readonly in unity

He needs to post more code so we can tell what the problem really is then.

I am using unity 2017.2.0b3

My program is being written for android so I don’t really want larger size if I can help it.
Here is a post to the entire project I am trying to port over.
The part I am having problem with is in EncodeSetup.cs