循环而不重置变量
Loop without resetting vars
我整个上午都在研究这段代码,但我仍然没有找到一种方法来做我想做的事,基本上这是我的代码:
using UnityEngine;
using System.Collections;
using System.IO;
public class bestellen : MonoBehaviour {
string stringtoedit = " ";
int voercounter = 0;
void OnGUI(){
var saveddoc = File.OpenText("C:/KleindierparkAdministratie/voer/voer.txt");
var line = saveddoc.ReadLine();
GUI.Label (new Rect (10, 10, 100, 20), line);
stringtoedit = GUI.TextField(new Rect(100, 10, 200, 20), stringtoedit, 25);
var voerdoc = File.OpenText("C:/KleindierparkAdministratie/voer/" + line + ".txt");
var line2 = voerdoc.ReadLine();
int counter = 1;
while(line2 != null){
if(counter == 5){
GUI.Label(new Rect(350, 10, 100, 20), line2);
}
counter++;
line2 = voerdoc.ReadLine();
}
if (GUI.Button (new Rect (25, 50, 250, 30), "Next")) {
line = saveddoc.ReadLine();
Debug.Log (line);
}
}
}
我想做的是每次单击按钮时都让它读出文本文档的下一行,但是由于它会回忆起来
var saveddoc = File.OpenText("C:/KleindierparkAdministratie/voer/voer.txt");
var line = saveddoc.ReadLine();
每次都不起作用,而且我对如何解决这个问题一无所知,所以现在我的问题是你们中有人知道如何解决这样的问题吗?
在 OnGUI 事件本身中这样做绝对不是最好的做法,因为它可以每帧调用多次!!
Unity documentation 对此非常清楚:
OnGUI is called for rendering and handling GUI events. This means that your OnGUI implementation might be called several times per frame (one call per event). For more information on GUI events see the Event reference. If the MonoBehaviour's enabled property is set to false, OnGUI() will not be called.
这意味着您的代码在当前状态下可能每帧打开该文件数次,而您的游戏是运行。
最好在 Start 或 Awake 事件中读取整个文件的内容并将其存储为 class 的全局变量。您还需要跟踪已阅读的行数,因此每次按下按钮时,您只需增加已阅读的行数,然后从文件中读取下一行。
一个简单的例子是...
private string[] _allLines;
private int _linesRead;
void Start() {
_linesRead = 0;
var savedDoc = File.OpenText("C:/KleindierparkAdministratie/voer/voer.txt");
_allLines = savedDoc.ReadAllLines();
}
void OnGUI() {
if (/* Button pressed*/) {
// read next line and do stuff
}
}
我不知道你为什么决定那样做。最好的选择可能是在 OnGUI 之外阅读文档,因为 OnGUI 被多次调用用于框架。
然而,在这里找到一个使用你或多或少的想法的解决方案(检查代码中的注释):
using UnityEngine;
using System.Collections;
using System.IO;
public class bestellen : MonoBehaviour {
string stringtoedit = " ";
int voercounter = 0;
string line = "";
string line2 = "";
var saveddoc;
void Start()
{
saveddoc = File.OpenText("C:/KleindierparkAdministratie/voer/voer.txt");
}
void OnGUI(){
GUI.Label (new Rect (10, 10, 100, 20), line);
stringtoedit = GUI.TextField(new Rect(100, 10, 200, 20), stringtoedit, 25);
GUI.Label(new Rect(350, 10, 100, 20), line2);
if (GUI.Button (new Rect (25, 50, 250, 30), "Next")) {
line = saveddoc.ReadLine();
Debug.Log (line);
var voerdoc = File.OpenText("C:/KleindierparkAdministratie/voer/" + line + ".txt");
var lineTemp = voerdoc.ReadLine();
int counter = 1;
// in this loop you are reading the whole document just to keep the 5th line.
// Consider refactor the loop just to read the desired line
while(lineTemp != null){
if(counter == 5){
line2 = lineTemp;
}
counter++;
lineTemp = voerdoc.ReadLine();
}
// Close here voerdoc!!!!
}
}
}
感谢 Panagiotis 的评论,我设法修复了它。对于任何有兴趣在未来参考的人来说,这里是新代码:
using UnityEngine;
使用System.Collections;
使用 System.IO;
public class 最好的:MonoBehaviour {
string stringtoedit = " ";
int voercounter = 0;
TextReader saveddoc;
string line;
void Start(){
saveddoc = File.OpenText("C:/KleindierparkAdministratie/voer/voer.txt");
line = saveddoc.ReadLine();
}
void OnGUI(){
GUI.Label (new Rect (10, 10, 100, 20), line);
stringtoedit = GUI.TextField(new Rect(100, 10, 200, 20), stringtoedit, 25);
var voerdoc = File.OpenText("C:/KleindierparkAdministratie/voer/" + line + ".txt");
var line2 = voerdoc.ReadLine();
int counter = 1;
while(line2 != null){
if(counter == 5){
GUI.Label(new Rect(350, 10, 100, 20), line2);
}
counter++;
line2 = voerdoc.ReadLine();
}
if (GUI.Button (new Rect (25, 50, 250, 30), "Next")) {
line = saveddoc.ReadLine();
Debug.Log (line);
}
}
}
感谢大家的帮助:)
我整个上午都在研究这段代码,但我仍然没有找到一种方法来做我想做的事,基本上这是我的代码:
using UnityEngine;
using System.Collections;
using System.IO;
public class bestellen : MonoBehaviour {
string stringtoedit = " ";
int voercounter = 0;
void OnGUI(){
var saveddoc = File.OpenText("C:/KleindierparkAdministratie/voer/voer.txt");
var line = saveddoc.ReadLine();
GUI.Label (new Rect (10, 10, 100, 20), line);
stringtoedit = GUI.TextField(new Rect(100, 10, 200, 20), stringtoedit, 25);
var voerdoc = File.OpenText("C:/KleindierparkAdministratie/voer/" + line + ".txt");
var line2 = voerdoc.ReadLine();
int counter = 1;
while(line2 != null){
if(counter == 5){
GUI.Label(new Rect(350, 10, 100, 20), line2);
}
counter++;
line2 = voerdoc.ReadLine();
}
if (GUI.Button (new Rect (25, 50, 250, 30), "Next")) {
line = saveddoc.ReadLine();
Debug.Log (line);
}
}
}
我想做的是每次单击按钮时都让它读出文本文档的下一行,但是由于它会回忆起来
var saveddoc = File.OpenText("C:/KleindierparkAdministratie/voer/voer.txt");
var line = saveddoc.ReadLine();
每次都不起作用,而且我对如何解决这个问题一无所知,所以现在我的问题是你们中有人知道如何解决这样的问题吗?
在 OnGUI 事件本身中这样做绝对不是最好的做法,因为它可以每帧调用多次!!
Unity documentation 对此非常清楚:
OnGUI is called for rendering and handling GUI events. This means that your OnGUI implementation might be called several times per frame (one call per event). For more information on GUI events see the Event reference. If the MonoBehaviour's enabled property is set to false, OnGUI() will not be called.
这意味着您的代码在当前状态下可能每帧打开该文件数次,而您的游戏是运行。
最好在 Start 或 Awake 事件中读取整个文件的内容并将其存储为 class 的全局变量。您还需要跟踪已阅读的行数,因此每次按下按钮时,您只需增加已阅读的行数,然后从文件中读取下一行。
一个简单的例子是...
private string[] _allLines;
private int _linesRead;
void Start() {
_linesRead = 0;
var savedDoc = File.OpenText("C:/KleindierparkAdministratie/voer/voer.txt");
_allLines = savedDoc.ReadAllLines();
}
void OnGUI() {
if (/* Button pressed*/) {
// read next line and do stuff
}
}
我不知道你为什么决定那样做。最好的选择可能是在 OnGUI 之外阅读文档,因为 OnGUI 被多次调用用于框架。
然而,在这里找到一个使用你或多或少的想法的解决方案(检查代码中的注释):
using UnityEngine;
using System.Collections;
using System.IO;
public class bestellen : MonoBehaviour {
string stringtoedit = " ";
int voercounter = 0;
string line = "";
string line2 = "";
var saveddoc;
void Start()
{
saveddoc = File.OpenText("C:/KleindierparkAdministratie/voer/voer.txt");
}
void OnGUI(){
GUI.Label (new Rect (10, 10, 100, 20), line);
stringtoedit = GUI.TextField(new Rect(100, 10, 200, 20), stringtoedit, 25);
GUI.Label(new Rect(350, 10, 100, 20), line2);
if (GUI.Button (new Rect (25, 50, 250, 30), "Next")) {
line = saveddoc.ReadLine();
Debug.Log (line);
var voerdoc = File.OpenText("C:/KleindierparkAdministratie/voer/" + line + ".txt");
var lineTemp = voerdoc.ReadLine();
int counter = 1;
// in this loop you are reading the whole document just to keep the 5th line.
// Consider refactor the loop just to read the desired line
while(lineTemp != null){
if(counter == 5){
line2 = lineTemp;
}
counter++;
lineTemp = voerdoc.ReadLine();
}
// Close here voerdoc!!!!
}
}
}
感谢 Panagiotis 的评论,我设法修复了它。对于任何有兴趣在未来参考的人来说,这里是新代码:
using UnityEngine;
使用System.Collections; 使用 System.IO;
public class 最好的:MonoBehaviour {
string stringtoedit = " ";
int voercounter = 0;
TextReader saveddoc;
string line;
void Start(){
saveddoc = File.OpenText("C:/KleindierparkAdministratie/voer/voer.txt");
line = saveddoc.ReadLine();
}
void OnGUI(){
GUI.Label (new Rect (10, 10, 100, 20), line);
stringtoedit = GUI.TextField(new Rect(100, 10, 200, 20), stringtoedit, 25);
var voerdoc = File.OpenText("C:/KleindierparkAdministratie/voer/" + line + ".txt");
var line2 = voerdoc.ReadLine();
int counter = 1;
while(line2 != null){
if(counter == 5){
GUI.Label(new Rect(350, 10, 100, 20), line2);
}
counter++;
line2 = voerdoc.ReadLine();
}
if (GUI.Button (new Rect (25, 50, 250, 30), "Next")) {
line = saveddoc.ReadLine();
Debug.Log (line);
}
}
}
感谢大家的帮助:)