【问题标题】:Separate Text files from List into GUI Labels将文本文件从列表中分离到 GUI 标签中
【发布时间】: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 ......所以你可以将一个字符串加载到一个标签..

标签: c# unity3d


【解决方案1】:

您正在向 _allFiles 添加对同一个 completeText 字符串的 2 个引用,在添加第一个实例后所做的任何更改都将在该条目的列表中看到,因为它是对同一对象的引用。每次要添加不同的文本时,都需要创建一个新实例。您可以将 completeText 设为 ProcessFile 的局部变量。

【讨论】:

  • 感谢您的评论 - 有什么办法可以告诉我,因为我对这一切有点陌生? :)
  • 删除 public static string completeText = "";" 行并在 ProcessFile 方法中添加本地声明:"private void ProcessFile(FileInfo file) { string完整文本 = ""; .....”
  • 好的,太棒了!我这样做了,现在文本只是出现在彼此之上?
  • 我认为这是因为你所有的 Rectangles 都在同一个地方,它们需要每次循环改变 x 或 y 参数:int x = 0; foreach(_allFiles 中的 var 文件){ GUI.contentColor = Color.red; GUI.Label(new Rect(1000 + (x * 410), 50, 400, 400), 文件); x++; } - 由于矩形的宽度 + 给定 10 间距,我乘以 410。
  • 但我的意思是 - 这两个文本文件仍然出现在同一个标​​签中,而不是一个标签的一个文本文件和另一个标签的另一个文本文件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 2019-02-14
  • 2021-03-23
  • 1970-01-01
相关资源
最近更新 更多