Unity 5.5.2f1 to 5.6.1f1 - interpolated strings error?

橙三吉。 提交于 2020-03-14 10:19:10

问题


I've just updated my Unity version from 5.5.2f1 to 5.6.1f1. Suddenly I get the error:

Feature `interpolated strings' cannot be used because it is not part of the C# 4.0 language specification

The code below worked well before updating.

public class SensorData
{
    public int Timestamp { get; set; }
    public float Humidity { get; set; }
    public float Temp { get; set; }
    public int Light { get; set; }
    public int Button { get; set; }

    public override string ToString()
    {
        return $"{Timestamp}, {Humidity}, {Temp}, {Light}, {Button}";
    }
}

I don't have a clue if its supposed to still work.


回答1:


It seems that interpolated strings are still not working after updating. To still use my above mentioned code, I did it the old way.

Instead of:

return $"{Timestamp}, {Humidity}, {Temp}, {Light}, {Button}";

I did:

return string.Format ("{Timestamp}, {Humidity}, {Temp}, {Light}, {Button}", Timestamp, Humidity, Temp, Light, Button);



回答2:


I have had success using

string.format("{0},{1},{2},{3}, {4}", Timestamp, Humidity, Temp, Light, Button);



回答3:


In Unity 2017 it's now possible to use interpolated strings in Unity if you change the .NET version that Unity uses. Interpolated strings is a feature from C# 6 which is released in .NET 4.6.

To change to .NET 4.6 go to: Edit > Project Settings > Player, expand "Other settings" and change the "Scripting Runtime Version" to .NET 4.6

For more info look at: https://docs.unity3d.com/Manual/ScriptingRuntimeUpgrade.html

In Unity 2018, Unity should use .NET 4.x by default so you should have access to interpolated strings without having to change anything.



来源:https://stackoverflow.com/questions/44107248/unity-5-5-2f1-to-5-6-1f1-interpolated-strings-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!