我整个上午一直在研究这段代码,但我还没找到办法去做我想做的事情,基本上这是我的代码:
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();
每一次它都没有工作,我对如何解决这个问题一无所知,所以现在我的问题是你们中的任何人都知道如何解决这个问题吗?
答案 0 :(得分:2)
在OnGUI事件本身中执行此操作绝对不是最好的事情,因为它可以每帧调用几次 !!
Unity documentation非常明确:
调用OnGUI来渲染和处理GUI事件。 这意味着您的OnGUI实现可能每帧调用几次(每个事件一次调用)。有关GUI事件的更多信息,请参阅事件参考。如果MonoBehaviour的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
}
}
答案 1 :(得分:0)
我不知道你为什么决定这样做。最好的选择可能是读取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!!!!
}
}
}
答案 2 :(得分:0)
感谢Panagiotis的评论,我设法解决了这个问题。对于任何有兴趣以后参考的人,这里是新代码:
using UnityEngine;
使用System.Collections; 使用System.IO;
public class bestellen: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);
}
}
}
感谢所有帮助人员:)