2016-08-14 439 views
0

我正在製作塔防遊戲。在第一個版本中,我使用簡單的文本格式來設置關卡。這是非常基本的,並且每次添加新的關卡屬性時都必須對其進行解析。我已經發現序列化和寫入加載並保存到/到二進制文件。對於我有一個序列化的類,看起來像這樣:Unity編輯器腳本:錯誤CS1525:意想不到的符號'<internal>'

[Serializable] 
public class LevelData { 
public string filename; 
public List<List<Tile>> grid; 
public List<Vector2> waypoints; 
public int width; 
public int length; 

public Vector2 startPos; 
public Vector2 finishPos; 

public List<TowerInfo> towers; 
public List<WaveData> waveChain; 
} 

現在我想給編輯腳本添加到我的LevelManager分析給舊的文本文件格式,並將其轉換爲二進制格式。這是我的編輯腳本:

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.IO; 
using System.Runtime.Serialization.Formatters.Binary; 
using UnityEditor; 
using UnityEngine; 

[CustomEditor(typeof(LevelManager))] 
[CanEditMultipleObjects] 
public class LevelManagerEditor : Editor { 
public string levelFilename; 

public override void OnInspectorGUI() { 
    DrawDefaultInspector(); 
    if(GUILayout.Button("Load level " + levelFilename + " and serialize")) { 
​  LevelData levelData; 
     ParseFromFile(out levelData); 
     Debug.Log("Parsing " + levelFilename + " successful!"); 
     SaveLevel(levelData); 
     Debug.Log("Saved serialized level"); 
    } 
} 
private void ParseFromFile(out LevelData levelData) { 
    levelData = new LevelData(); 
    levelData.filename = levelFilename; 
    System.IO.StreamReader file = new System.IO.StreamReader(levelFilename + ".txt"); 

    string line; 
    int y = 0; 
    while ((line = file.ReadLine()) != null) { 
     if (line[0] == '#') break; 
     List<Tile> row = new List<Tile>(); 
     for (int x = 0; x < line.Length; x++) { 
      switch(line[x]) { 
       case '.': 
        row.Add(Tile.NORMAL_BRICK); 
        break; 
       case 'P': 
        row.Add(Tile.PATH_BRICK); 
        break; 
       case 'S': 
        row.Add(Tile.START_BLOCK); 
        levelData.startPos = new Vector2(x, y); 
        break; 
       case 'F': 
        row.Add(Tile.FINISH_BLOCK); 
        levelData.finishPos = new Vector2(x, y); 
        break; 
       default:break; 
      } 
     } 
     levelData.grid.Add(row); 
     levelData.width = row.Count; 
     y++; 
    } 
    levelData.length = y; 
    while((line = file.ReadLine()) != null) { 
     levelData.waypoints.Add(ParseWaypoints(line)); 
    } 
    file.Close(); 
} 
private Vector2 ParseWaypoints(string line) { 
    string[] tokens = line.Split(' '); 
    return new Vector2(int.Parse(tokens[0]), int.Parse(tokens[1])); 
} 
private void SaveLevel(LevelData levelData) { 
    string pathToLevel = Application.persistentDataPath + "/" + levelData.filename + ".dat"; 
    FileStream file = File.Create(pathToLevel); 
    BinaryFormatter bf = new BinaryFormatter(); 
    bf.Serialize(file, levelData); 
} 
} 

這是編譯器錯誤: 資產/塔防資產/腳本/工具/ LevelManagerEditor.cs(17,5):錯誤CS1525:意外的符號`<內部>」

當我加倍點擊它指向我裏面OnInspectorGUI的LevelData levelData;聲明()

我不明白的錯誤。請幫忙。謝謝:)

回答

1

我想通了。我有一個不可打印的字符在這一行。我不得不刪除該行並重寫它。

+0

絕對!在該行上的所有常用空格之前,它似乎是[U + 200B,'零寬空格]'(https://en.wikipedia.org/wiki/Zero-width_space)的一次出現。我不知道你是怎麼介紹這件事的。您使用的是哪種編輯器或開發環境(Visual Studio?)?也許一些晦澀的組合(你不小心使用)會產生這個角色?或者,也許你從一個「豐富」的文本文件複製了一些代碼,如文字處理文檔或網頁? –

+0

我正在使用visual studio 2015.不,我沒有複製粘貼代碼片段,雖然我可能有Alt + numpad的東西沒有注意到,奇怪的大聲笑! 第一次,我將levelData設置爲我的編輯器類的成員變量。當我編譯它給了我同樣的錯誤,但在這一行。然後我想,也許某種程度上我不允許在編輯器腳本中使用用戶定義的類作爲成員變量,所以我通過Alt + DownArrow移動了該行。它告訴我在新位置出現同樣的錯誤,所以我認爲這與我的LevelData類有關。真的讓我困惑 –

相關問題