LoginSignup
0
0

More than 5 years have passed since last update.

オブジェクトの角度をCSVに出力

Last updated at Posted at 2019-01-25

友達からいきなり、「角度をcsvに出力するプログラムってどうしたらいいの?」とラインが来たので、実装しました。

環境

Unity 2017.3.0

プログラム

I_am_hentai.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using System.IO;

public class I_am_hentai : MonoBehaviour
{

    StreamWriter sw;
    private int count = 0;

    // Use this for initialization
    void Start()
    {
        //出力ファイル名、(true=追記 false=上書き)、エンコード
        sw = new StreamWriter(@"outPut.csv", false, Encoding.GetEncoding("Shift_JIS"));
        // CSVのヘッダー部分
        string[] s1 = { "x", "y", "z" };//x,y,z軸であることを示す
        string s2 = string.Join(",", s1);
        sw.WriteLine(s2);

        StartCoroutine("WriteCSV");
    }

    IEnumerator WriteCSV(){
    var wait = new WaitForSeconds(1);
    while(true){
        if(count<10){
            Debug.Log("csv書き込み中");
            yield return wait;

            float x = transform.rotation.x;
            float y = transform.rotation.y;
            float z = transform.rotation.z;

            //データ出力
            string[] str = { (x).ToString(), (y).ToString(), (z).ToString() };
            string str2 = string.Join(",", str);
            sw.WriteLine(str2);
            count++;
        }else if(count==10){
            sw.Close();//しっかりクローズしないとだめ

           // ファイル読み込み
           StreamReader sr = new StreamReader(@"outPut.csv", Encoding.GetEncoding("Shift_JIS"));
           string line;
            while ((line = sr.ReadLine()) != null)//次の行があるなら書き込む
            {
               //Debug.Log(line);
            }
            sr.Close();//しっかりクローズしないとだめ
            Debug.Log("書き込み完了");
            break;
        }
    }
    }
}

結果

このように、10秒間のx,y,z軸の角度が出力できました。
スクリーンショット (28).png

感想

てか、友達は他の研究室なんですけど、回転値をcsvに書き出して、研究の中でどのように使うのか疑問です。。。笑

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0