You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity点击按钮从指定文本文件获取随机单词的问题

问题描述

我有三个按钮:3、4、5,需求如下:

  • 玩家点击3、4或5时,从对应的.txt文件中分配一个随机单词给玩家。文本文件分别是3pointWordList.txt、4pointWordList.txt、5pointWordList.txt,存放路径为Assets\Word Lists。
  • 规则:若玩家此次选择3分选项,下次轮到他时只能选另外两个选项之一。
  • 要求:选中的单词需要从随机池中删除。

当前遇到的问题:无法从txt文件中获取随机单词,试过ChatGPT和StackExchange相关问题都没解决。已经在检视面板给每个按钮设置了固定的wordListNumber(比如按钮3对应wordListNumber=3)。

无效的随机化代码

int randomIndex = Random.Range(0, wordList.Count);
string selectedWord = wordList[randomIndex];
wordList.RemoveAt(randomIndex);

初始代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.UIElements;

public class TaskManager : MonoBehaviour
{
    public int wordListNumber;
    public string wordList;
    public string selectedWord;

    // Start is called before the first frame update
    void Start()
    {
        if (wordListNumber == 3)
        { 
        }
        else if (wordListNumber == 4)
        {
        }
        else if (wordListNumber == 5)
        {
        }    
    }
}

编辑后的代码

using UnityEngine;
using System.Collections.Generic;
using System.IO;
using UnityEditor.Experimental.GraphView;
using TMPro;

public class RandomWordLoader : MonoBehaviour
{
public string fileName = "3pointWordList";
public string fileName2 = "4pointWordList";
public string fileName3 = "5pointWordList";
public int pointquestion;
public TextMeshProUGUI yourWord;


private List<string> lines = new List<string>();

void Start()
{
    LoadWordsFromFile();
}

void LoadWordsFromFile()
{
    if (pointquestion == 3)
    { 
        TextAsset textAsset = Resources.Load<TextAsset>(fileName);

        if (textAsset != null)
        {
            StringReader reader = new StringReader(textAsset.text);
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                // 假设每行一个单词
                lines.Add(line.Trim());
            }

            reader.Close();
        }
        else
        {
            Debug.LogError("加载文件失败: " + fileName);
        }

    }
    else if (pointquestion == 4)
    {
        TextAsset textAsset = Resources.Load<TextAsset>(fileName2);

        if (textAsset != null)
        {
            StringReader reader = new StringReader(textAsset.text);
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                // 假设每行一个单词
                lines.Add(line.Trim());
            }

            reader.Close();
        }
        else
        {
            Debug.LogError("加载文件失败: " + fileName);
        }

    }
    else if (pointquestion == 5)
    {
        TextAsset textAsset = Resources.Load<TextAsset>(fileName3);

        if (textAsset != null)
        {
            StringReader reader = new StringReader(textAsset.text);
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                // 假设每行一个单词
                lines.Add(line.Trim());
            }

            reader.Close();
        }
        else
        {
            Debug.LogError("加载文件失败: " + fileName);
        }

    }
}
public string GetRandomWord()
{
    if (lines.Count > 0)
    {
        int randomIndex = Random.Range(0, lines.Count);
        return lines[randomIndex];


    }
    else
    {
        Debug.LogWarning("没有加载到单词。");
        return null;
    }
}

内容的提问来源于stack exchange,提问作者Jill Correy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 05:53:18