unityでplayer位置を取得し、CSVファイルで書き出す
UnityでPlayerの位置情報を取得し、CSVファイルを書き出そうとしたのですが、
CSVファイルが見つからないようで以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
DirectoryNotFoundException: Could not find a part of the path "C:\Users\yamag\Documents\chichubijutsukan\Assets\moving_log\2019-11-22-23-00-20.csv". System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <567df3e0919241ba98db88bec4c6696f>:0) System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options, System.String msgPath, System.Boolean bFromProxy, System.Boolean useLongPath, System.Boolean checkHost) (at <567df3e0919241ba98db88bec4c6696f>:0) (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions,string,bool,bool,bool) System.IO.StreamWriter.CreateFile (System.String path, System.Boolean append, System.Boolean checkHost) (at <567df3e0919241ba98db88bec4c6696f>:0) System.IO.StreamWriter..ctor (System.String path, System.Boolean append, System.Text.Encoding encoding, System.Int32 bufferSize, System.Boolean checkHost) (at <567df3e0919241ba98db88bec4c6696f>:0) System.IO.StreamWriter..ctor (System.String path, System.Boolean append, System.Text.Encoding encoding, System.Int32 bufferSize) (at <567df3e0919241ba98db88bec4c6696f>:0) System.IO.StreamWriter..ctor (System.String path, System.Boolean append) (at <567df3e0919241ba98db88bec4c6696f>:0) (wrapper remoting-invoke-with-check) System.IO.StreamWriter..ctor(string,bool)
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5using System.IO; 6using System; 7 8 9public class DataLogger : MonoBehaviour 10{ 11 private StreamWriter sw; 12 private FileInfo fi; 13 14 private DateTime date; 15 // Use this for initialization 16 void Start() 17 { 18 date = DateTime.Now; 19 } 20 21 // Update is called once per frame 22 void Update() 23 { 24 string format = "yyyy-MM-dd-HH-mm-ss"; 25 StreamWriter sw; 26 FileInfo fi; 27 Debug.Log(date.ToString(format)); //ログ表示、カッコ内に好きな文字列などを入れる 28 string filename = date.ToString(format); //ファイルネームの設定、今回は日時をもとにcsvファイルを設定 29 fi = new FileInfo(Application.dataPath + "/moving_log/" + filename + ".csv"); // Aplication.dataPath で プロジェクトファイルがある絶対パスが取り込める 30 //str = this.gameObject + "," + (transform.position.x) + "," + (transform.position.y) + "," + (transform.position.z) + "," + transform.rotation.x + "," + transform.rotation.y + "," + transform.rotation.z + "," + transform.rotation.w; 31 //sw = fi.AppendText(); 32 sw = fi.AppendText(); 33 string str = this.gameObject + "," + (transform.position.x) + "," + (transform.position.y) + "," 34 + (transform.position.z) + "," + transform.rotation.x + "," + transform.rotation.y + "," 35 + transform.rotation.z + "," + transform.rotation.w; 36 sw.WriteLine(str); 37 sw.Flush(); 38 sw.Close(); 39 } 40 41} 42
試したこと
上記のように書き込んだのですが、何が原因かわかりません。改善点を教えて下さい。
あなたの回答
tips
プレビュー