【发布时间】:2015-09-04 15:13:59
【问题描述】:
此代码允许读取 .txt 文件并将其放入 GUI 标签中,然后显示为屏幕文本。到目前为止,txt 文件被放入列表中,然后 foreach 然后分配变量。但是,我的问题是两个文本文件一起出现在每个标签中,而不是单独出现,我想知道如何做到这一点?
powersupply.txt 用于电源模型,puzzles.txt 用于 cdrw 模型。任何帮助是极大的赞赏。
using UnityEngine;
using System.Collections;
using System.IO;
// To use List
using System.Collections.Generic;
public class OnClick : MonoBehaviour
{
string cdrw = "CD-RW";
string powerSupply = "Power Supply";
public string txt;
public static string completeText = "";
public GameObject cdrwModel;
public GameObject powerSupplyModel;
public StreamReader reader = null;
public FileInfo theSourceFile = null
private static List<string> _allFiles = new List<string>();
public void Start()
{
theSourceFile = new FileInfo(Application.dataPath + "/puzzles.txt");
ProcessFile(theSourceFile);
theSourceFile = new FileInfo(Application.dataPath + "/powersupply.txt");
ProcessFile(theSourceFile);
}
private void ProcessFile(FileInfo file)
{
if (file != null && file.Exists)
reader = file.OpenText();
if (reader == null)
{
Debug.Log("puzzles.txt not found or not readable");
}
else
{
while ((txt = reader.ReadLine()) != null)
{
Debug.Log("-->" + txt);
completeText += txt + "\n";
}
_allFiles.Add(completeText);
}
}
public void OnGUI()
{
if ((isClicked) && (cdrwModel))
{
GUI.Label(new Rect(15, 35, 400, 400), "Press <TAB> for more information");
if (Input.GetKey(KeyCode.Tab))
{
foreach (var file in _allFiles)
{
GUI.contentColor = Color.red;
GUI.Label(new Rect(1000, 50, 400, 400), file);
}
}
}
else if ((isClicked) && (powerSupplyModel))
{
GUI.Label(new Rect(15, 35, 400, 400), "Press <TAB> for more information");
if (Input.GetKey(KeyCode.Tab))
{
foreach (var file in _allFiles)
{
GUI.contentColor = Color.red;
GUI.Label(new Rect(1000, 50, 400, 400), file);
}
}
}
}
}
【问题讨论】:
-
您似乎正在将两个文件都读入 _allFiles 然后将整个内容读入两个标签..?
-
是的,如何将内容分成两个标签?
-
一个选项是为每个标签创建单独的字符串变量或 List
......所以你可以将一个字符串加载到一个标签..