我不断收到异常功能“插值字符串”,因为它不属于C#4.0语言吗?

时间:2018-08-15 00:48:19

标签: c# unity3d

首先,我有一个例外,那就是Visual Studio中的错误。 因此,我点击了出现错误的行,并选择修复它以将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);
        }
    }
}

错误在Visual Studio的这一行上:

string toWrite = $"{selected.parent}:{selected.localPosition}:{selected.localRotation}:{selected.localScale}";
                WriteDataToFile(toWrite);

现在错误已从Visual Studio中消失,我可以毫无问题地进行构建,但是在统一编辑器中,控制台中仍然存在异常,然后单击“清除”按钮不能将其删除:

Exception in editor

1 个答案:

答案 0 :(得分:6)

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

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