我一直得到异常功能'插值字符串'不能使用,因为它不是C#4.0语言的一部分?

Ben*_*mil 2 c# unity-game-engine

首先,我在视觉工作室中遇到了错误.所以我点击了包含错误的行并选择修复它以将visual studio目标更改为csharp 6

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class PrefabReplace : EditorWindow
{
    [SerializeField] private GameObject prefab;

    [MenuItem("Tools/Prefab Replace")]
    static void CreateReplaceWithPrefab()
    {
        EditorWindow.GetWindow<PrefabReplace>();
    }

    private void OnGUI()
    {
        prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), false);

        if (GUILayout.Button("Replace"))
        {
            var selection = Selection.gameObjects.ToList();

            if (prefab != null && selection.Count > 0)
            {
                for (var i = selection.Count - 1; i >= 0; --i)
                {
                    var selected = selection[i];
                    var prefabType = PrefabUtility.GetPrefabType(prefab);
                    GameObject newObject;

                    if (prefabType == PrefabType.Prefab)
                    {
                        newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
                    }
                    else
                    {
                        newObject = Instantiate(prefab);
                        newObject.name = prefab.name;
                    }

                    if (newObject == null)
                    {
                        Debug.LogError("Error instantiating prefab");
                        break;
                    }

                    Undo.RegisterCreatedObjectUndo(newObject, "Replace With Prefabs");
                    newObject.transform.parent = selected.transform.parent;
                    newObject.transform.localPosition = selected.transform.localPosition;
                    newObject.transform.localRotation = selected.transform.localRotation;
                    newObject.transform.localScale = selected.transform.localScale;
                    newObject.transform.SetSiblingIndex(selected.transform.GetSiblingIndex());
                    Undo.DestroyObjectImmediate(selected);
                }
            }
        }

        if (GUILayout.Button("Copy settings"))
        {
            var selection = Selection.gameObjects.ToList();

            for (int i = selection.Count - 1; i >= 0; --i)
            {
                var selected = selection[i].transform;
                string toWrite = $"{selected.parent}:{selected.localPosition}:{selected.localRotation}:{selected.localScale}";
                WriteDataToFile(toWrite);
            }
        }

        GUI.enabled = false;
        EditorGUILayout.LabelField("Selection count: " + Selection.objects.Length);
    }

    private void OnInspectorUpdate()
    {
        Repaint();
    }

    private void WriteDataToFile(string line)
    {
        string path = "Assets/Resources/test.txt";
        string[] text = new string[4];
        if (File.Exists(path))
        {
            text = File.ReadAllLines(path);
            if (!text.Contains(line))
                File.AppendAllText(path, line);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

错误发生在visual studio的这一行:

string toWrite = $"{selected.parent}:{selected.localPosition}:{selected.localRotation}:{selected.localScale}";
                WriteDataToFile(toWrite);
Run Code Online (Sandbox Code Playgroud)

现在错误已从视觉工作室消失,我可以毫无问题地构建它,但在统一编辑器中,控制台中仍然存在异常并单击"清除"按钮而不删除它:

编辑中的例外情况

Pro*_*mer 6

您必须在编辑器中启用.NET 4.x才能使用插值字符串.特征.

转到编辑 - > 项目设置 - >播放器 - > 其他设置 - > 配置 - > 脚本运行时版本 - > .NET 4.x等效.