파일 경로명을 인자값으로 받아서 CSV데이터를 읽어 파싱한 후  List<Vector3>로 반환한다.

반환값이 T가 아닌 고정되어있어 범용성이 떨어지는 메서드이다. 나중에 Refactoring이 필요해보인다. 

 

Stage Way Point는 Vector3를 저장하는 CSV 외부데이터이다.

 

  public static List<Vector3> LoadStageWayPointFromCSV(string fileName)
  {
      // Resources 폴더에서 파일 로드
      TextAsset csvFile = Resources.Load<TextAsset>(fileName);

      if (csvFile == null)
      {
          Debug.LogError($"File {fileName} not found in Resources folder.");
          return null;
      }

      // 데이터를 한 줄씩 분리
      string[] lines = csvFile.text.Split('\n');
      List<Vector3> wayPointList = new List<Vector3>();

      //첫번쨰 줄은 헤더이기에 제외 
      for (int i = 1; i < lines.Length; i++)
      {

          string line = lines[i].Trim(); // 공백 제거
          if (string.IsNullOrEmpty(line)) continue; // 빈 줄 무시

          string[] values = line.Split(',');

          if (int.TryParse(values[0], out int x) &&
              int.TryParse(values[1], out int y) &&
              int.TryParse(values[2], out int z))
          {
              wayPointList.Add(new Vector3Int(x, y, z));
          }
      }

      return wayPointList;
  }

+ Recent posts