C#初心者です。
前回の質問と似ていますが、別のソースにてエクセルの書き込み次に問題が起こっています。
下記のように、一つ空白のマスがあって次のものが書き込みされています。
0
(空白)
0
(空白)
これを
0
0
と空白を無くして書き込みたいです。
問題のソース
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using UnityEditor; 6using System.Collections; 7using System.IO; 8using System; 9using System.Text; 10 11 12public class countright: MonoBehaviour { 13 14 private Text CountTextR; 15 private int CountR; 16 private int OldCountR; 17 18 // Use this for initialization 19 void Start () { 20 CountTextR = GetComponentInChildren<Text>(); 21 OldCountR = 0; 22 CountR = 0; 23 24 /* StreamWriter swR = new StreamWriter(@"saveDataR7.csv", true, Encoding.GetEncoding("Shift_JIS")); 25 string[] sR1 = { "右カウント" }; 26 string sR2 = string.Join(",", sR1); 27 swR.WriteLine(sR2);*/ 28 } 29 30 int endR = 0; 31 // Update is called once per frame 32 void Update () { 33 if (Input.GetKeyDown(KeyCode.RightArrow)) 34 { 35 endR = 1; 36 CountR++; 37 //Debug.Log("右矢印"); 38 if (OldCountR != CountR) 39 { 40 CountTextR.text = CountR.ToString("0"); 41 } 42 OldCountR = CountR; 43 } 44 45 StreamWriter swR = new StreamWriter(@"saveDataR8.csv", true, Encoding.GetEncoding("Shift_JIS")); 46 47 string[] strR = {""+(CountTextR.text) }; 48 string strR2 = string.Join(",", strR); 49 swR.WriteLine(strR2); 50 51 swR.Close(); 52 53 if (endR == 1) 54 { 55 EditorApplication.Exit(0); 56 //Application.Quit(); 57 } 58 } 59}
自分が間違えを探すのが下手なのもありますが、下記のはほぼ同じソースで空白なしで書き込まれます。
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using UnityEditor; 6using System.Collections; 7using System.IO; 8using System; 9using System.Text; 10 11public class countleft : MonoBehaviour 12{ 13 14 private Text CountTextL; 15 private int CountL; 16 private int OldCountL; 17 18 19 // Use this for initialization 20 void Start() 21 { 22 CountTextL = GetComponentInChildren<Text>(); 23 OldCountL = 0; 24 CountL = 0; 25 26 /* StreamWriter swL = new StreamWriter(@"saveDataL6.csv", true, Encoding.GetEncoding("Shift_JIS")); 27 string[] sL1 = { "左カウント" }; 28 string sL2 = string.Join(",", sL1); 29 swL.WriteLine(sL2);*/ 30 31 } 32 33 34 35 int endL = 0; 36 // Update is called once per frame 37 void Update() 38 { 39 if (Input.GetKeyDown(KeyCode.LeftArrow)) 40 { 41 endL = 1; 42 CountL++; 43 //Debug.Log("左矢印"); 44 if (OldCountL != CountL) 45 { 46 CountTextL.text = CountL.ToString("0"); 47 } 48 OldCountL = CountL; 49 // Application.Quit(); 50 51 } 52 StreamWriter swL = new StreamWriter(@"saveDataL9.csv", true, Encoding.GetEncoding("Shift_JIS")); 53 54 string[] strL = {""+(CountTextL.text) }; 55 string strL2 = string.Join(",", strL); 56 swL.WriteLine(strL2); 57 58 swL.Close(); 59 60 if (endL == 1) 61 { 62 EditorApplication.Exit(0); 63 //Application.Quit(); 64 } 65 } 66}
質問の答えでは無いのでこちらに書きますが、CountR++の結果を保存しているので、右矢印を押した数だけ「1,2,3,4...」とカウントアップするのが正しい動作じゃないでしょうか?
>Windさん if (Input.GetKeyDown(KeyCode.RightArrow))の外で書き出ししてるんで、右矢印キーを押す前から書き出しは実行されるはずです。その状態なら一意な値が出るのはおかしくはないです。
>Fuku78さん CountTextR.textの初期値(インスペクタ上での値)は何ですか? そちらが「(改行)0」になっていたりしませんか?
インスペクタ上で、「0(改行)」になっていました。改行を消したところ空白が消えて、エクセルに綺麗に書き込まれました。ありがとうございます。
>sakura_hanaさん 右矢印が押されなければ0が保存され続けるのですね。失礼しました。