C# 无需重置变量的循环
我整个上午都在编写这段代码,但我仍然没有找到一种方法来做我想做的事情,基本上这是我的代码:C# 无需重置变量的循环,c#,user-interface,button,unity3d,C#,User Interface,Button,Unity3d,我整个上午都在编写这段代码,但我仍然没有找到一种方法来做我想做的事情,基本上这是我的代码: using UnityEngine; using System.Collections; using System.IO; public class bestellen : MonoBehaviour { string stringtoedit = " "; int voercounter = 0; void OnGUI(){ var saveddoc = File
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事件中这样做肯定不是最好的做法,因为它可以每帧调用几次 这方面的问题相当清楚: OnGUI用于呈现和处理GUI事件这意味着您的OnGUI实现可能会在每帧中被调用多次(每个事件调用一次)。有关GUI事件的更多信息,请参阅事件参考。如果MonoBehavior的enabled属性设置为false,则不会调用OnGUI() 这意味着当前状态下的代码可能会在游戏运行的每帧打开该文件数次 最好在Start或Awake事件中读入整个文件的内容,并将其存储为类的全局变量。您还需要跟踪已读取的行数,因此每次按下按钮时,您只需增加已读取的行数,然后从文件中读取下一行 一个简单的例子是
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会被调用多次 尽管如此,还是可以在这里找到一个使用您或多或少的想法的解决方案(检查代码中的注释):
多亏了Panagiotis的评论,我成功地修复了它。以下是新代码,供有兴趣的人士参考:
using UnityEngine;
使用系统集合;
使用System.IO
公共类贝斯泰伦:单一行为{
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);
}
}
}
感谢所有的帮助:)你在点击按钮时调用onGUI函数吗?只是不要重新打开文件。存储第一次打开时返回到某个字段的TextReader,或者使用ReadAllLines一次读取所有行,将字符串数组存储到某个字段并在field@Muneem不,我不会在点击按钮时运行OnGUI。我只是让OnGUI按它应该的方式运行,但是如果你看我的代码,你会发现我在按下按钮时试图更改某些值,但由于OnGUI是一个循环,它会立即重置这些值。@Panagiotis我不完全理解你存储整个文本阅读器的意思,但我会在谷歌上搜索一下,看看这是否是解决这个问题的合适方法。我希望它能工作,谢谢已经编辑:它工作了,谢谢你的帮助!你说得对。我的代码现在有点笨拙,我需要重构很多代码。谢谢你的帮助!
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);
}
}