【发布时间】:2020-09-04 14:18:31
【问题描述】:
我在使用统一引擎时遇到了一点问题。 我正在尝试结合一种方法来替换另一个字符串中的字符串。 如果我使用一个字符串,像往常一样设置,代码就可以工作(例如 string b = "whatever"); 但是当我尝试使用列表中的变量时,输出不会被修改(例如excludedWords [0])。 我试图在我的 excludeWords 变量的所有位置将它转换为字符串,但仍然没有成功。
有什么见解吗?
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using TMPro;
public class FilterRex : MonoBehaviour
{
List<string> excludedWords;
[SerializeField]TextAsset excludedWordsFile;
[SerializeField]TMP_InputField inputField;
// Start is called before the first frame update
void Start()
{
//this code accesses the words from the lists that need to be banned
excludedWords = new List<string>();
string[] lines = excludedWordsFile.text.Split('\n');
foreach(string line in lines)
{
excludedWords.Add(line);
}
Debug.Log(excludedWords[0]);
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Return))
{
PrepareTheWord();
}
}
public void PrepareTheWord()
{
string innerWord = inputField.text.ToLower();
string b = excludedWords[0].ToString());
inputField.text = ReplaceTheWord(b, innerWord);
}
public string ReplaceTheWord(string _replace, string _from)
{
string output = _from.Replace(_replace, "");
return output;
}
}
【问题讨论】:
-
而不是使用
Input.GetKeyDown,我宁愿使用例如onValueChanged
标签: c# string unity3d filter replace