我正在使用Unity進行仿真並記錄一些數據。它基本上是一個車,我試圖記錄這些事情:StreamWrite和Debug.log中的不同輸出
- 節點=我的航點
- 超時=我的輸出時間,這是當前時間,這個圈
- 速度=的此時汽車的速度
- XPOS = X位置
- zPos = Z位置
此日誌記錄腳本爲我的data.txt工作的罰款,我得到這個結果。
1 1 0.2876698 -3.296932 95.41293
1 2.02 1.095865 -3.286392 94.23542
1 3.04 2.818792 -3.264626 92.27694
1 4.08 3.079535 -3.226411 89.4863
1 5.12 2.492892 -3.171062 85.86295
現在,我想補充這是給我的,我需要每圈的時間第二個腳本,以便檢查,如果我們有一個固定的時間這一點。我必須採用這種方式:圈數時間。
如果我想打印出來我得到這個結果的結果:
lap time
62.02
所以我很驚訝,因爲它是在我的第一個數據記錄器,同時可變,所以我想退房出了什麼問題,並將我的第一個記錄器的相同命令放入Debug.log中,並得到與第二個記錄器相同的結果。
所以現在我只是想知道確切的同一行如何能夠給我兩個不同的輸出和我將如何得到我的計時圈,他的工作是應該的工作方式。
這是完整的代碼。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class SaveToFile : MonoBehaviour {
public GameObject Auto;
private CarEngine Engine;
public float speed;
public float maxSpeed;
public float time;
public float motorTorque;
public float breakTorque;
public float steerAngle;
public float node;
public bool IsRunning = true;
public int WaitTimer = 1;
public float xPos;
public float zPos;
public bool cancel = false;
public bool lap = false;
public float time_out;
public float time_lap;
public int lapNum;
void Start()
{
Engine = Auto.GetComponent<CarEngine>();
StreamWriter writer = File.AppendText(@"C:\Users\kaykl\Documents\Schule\Info\Data\CarControle\lap.txt");
writer.WriteLine("lap time");
writer.Close();
}
void FixedUpdate()
{
motorTorque = Engine.maxMotorTorque;
breakTorque = Engine.maxBrakeTorque;
steerAngle = Engine.maxSteerAngle;
maxSpeed = Engine.maxSpeed;
speed = Engine.currentSpeed;
time = Time.time;
node = Engine.node;
xPos = Auto.transform.position.x;
zPos = Auto.transform.position.z;
lap = Engine.lap;
time_out = time - time_lap;
if(lap == true && time_out > 5)
{
time_lap = Time.time;
lapNum = lapNum + 1;
StreamWriter writer = File.AppendText(@"C:\Users\kaykl\Documents\Schule\Info\Data\CarControle\lap.txt");
writer.WriteLine(lapNum +'+'+time_out);
writer.Close();
}
if (IsRunning == true)
{
StartCoroutine(Wait());
}
}
public IEnumerator Wait()
{
IsRunning = false;
yield return new WaitForSeconds(WaitTimer);
Output();
IsRunning = true;
}
void Output()
{
Debug.Log(node + " " + time_out + " " + speed + " " + xPos + " " + zPos);
StreamWriter writer = File.AppendText(@"C:\Users\kaykl\Documents\Schule\Info\Data\CarControle\data.txt");
writer.WriteLine(node + " " + time_out + " " + speed + " " + xPos + " " + zPos);
writer.Close();
}
}
我只想說謝謝你們每個人在事先,這大概是我的一個愚蠢的錯誤,但我只是沒有得到它現在。
//編輯
好的,現在這是我的工作代碼。我使用了writer.flush(),然後我的Debug.log是正確的,但我的lap.txt仍然是錯誤的。 當我將StartLine()中的WriteLine改變爲output()後,它以某種方式工作。所以這是我現在工作的代碼。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class SaveToFile : MonoBehaviour {
public GameObject Auto;
private CarEngine Engine;
public float speed;
public float maxSpeed;
public float time;
public float motorTorque;
public float breakTorque;
public float steerAngle;
public float node;
public bool IsRunning = true;
public int WaitTimer = 1;
public float xPos;
public float zPos;
public bool cancel = false;
public bool lap = false;
public float time_out;
public float time_lap;
public int lapNum;
void Start()
{
Engine = Auto.GetComponent<CarEngine>();
Output();
}
void FixedUpdate()
{
motorTorque = Engine.maxMotorTorque;
breakTorque = Engine.maxBrakeTorque;
steerAngle = Engine.maxSteerAngle;
maxSpeed = Engine.maxSpeed;
speed = Engine.currentSpeed;
time = Time.time;
node = Engine.node;
xPos = Auto.transform.position.x;
zPos = Auto.transform.position.z;
lap = Engine.lap;
time_out = time - time_lap;
if(lap == true && time_out > 5)
{
time_lap = Time.time;
lapNum = lapNum + 1;
StreamWriter writer = File.AppendText(@"C:\Users\kaykl\Documents\Schule\Info\Data\CarControle\lap.txt");
writer.WriteLine(time_out);
writer.Flush();
writer.Close();
}
if (IsRunning == true)
{
StartCoroutine(Wait());
}
}
public IEnumerator Wait()
{
IsRunning = false;
yield return new WaitForSeconds(WaitTimer);
Output();
IsRunning = true;
}
void Output()
{
Debug.Log(node + " " + time_out + " " + speed + " " + xPos + " " + zPos);
StreamWriter writer = File.AppendText(@"C:\Users\kaykl\Documents\Schule\Info\Data\CarControle\data.txt");
writer.WriteLine(node + " " + time_out + " " + speed + " " + xPos + " " + zPos);
writer.Flush();
writer.Close();
}
}
誰能向我解釋如何僅僅指的方法,而不是把它寫下來解決了這一問題的?
非常感謝。
嘗試關閉()方法之前的writer.Flush()。 – jdweng
首先感謝你,現在它在控制檯中工作,但我仍然在我的lap.txt中得到了錯誤的值。我在那裏複製了我的另一個WriteLine命令,看它是否會打印出來,但仍然打印出與我原來的帖子。所以必須有別的東西。 –
好吧修正了它,甚至很難我不太確定如何;)工作代碼被添加到問題 –