gpt4 book ai didi

c# - 事件功能未按预期工作

转载 作者:太空狗 更新时间:2023-10-29 20:19:20 25 4
gpt4 key购买 nike

首先我将解释发生了什么,然后是我期望发生的事情,最后是它背后的代码

所以当我按下回车键时,文本的颜色是绿色

我期望发生的是颜色变红

这是基于我是否在字段中输入“Bad”

//Please note I have edited uni9mportant code out

//Event Listener
inputField.onEndEdit.AddListener (delegate {
VerifyWords();
});

//Clss that handles the dictionary
public abstract class WordDictionary: MonoBehaviour{
public static Dictionary<string,bool> _wordDictionary = new Dictionary<string,bool> ();

private void Start(){
_wordDictionary.Add ("Bad",true);
}
}

//Function that handles the word verification
private void VerifyWords(){
if (openChat == false) { //If we done have open chat
bool hasBadWords = false; //Reset boolean
string[] stringSplit = inputField.text.Split (' '); //Split text string

for (int i = 0; i < stringSplit.Length; i++) { // Go through each word in the string array
if (WordDictionary._wordDictionary.ContainsKey (stringSplit[i])) { //If the word is in the dictionary
hasBadWords = true; //Then there is a bad word
}
}

if (hasBadWords == true) { //If a bad word was found
inputField.textComponent.color = Color.red; //Then the text should be red
} else {
inputField.textComponent.color = Color.green; //The text should be green
}
}
}

编辑我用注释编辑了代码,以便让我的想法更直观一些

最佳答案

问题是该类被标记为抽象。抽象类无法实例化,因此 Unity 无法在无法实例化的类上调用 Start。最简单的解决方法是简单地从类定义中删除 abstract:

public class WordDictionary: MonoBehaviour{
public static Dictionary<string,bool> _wordDictionary = new Dictionary<string,bool> ();

private void Start(){
_wordDictionary.Add ("Bad",true);
}
}

但是你现在有一个新问题。 WordDictionary 有一个由非静态方法初始化的静态成员。这意味着每次您创建一个新的 WordDictionary 时,都会调用 Start() 并将所有单词再次添加到字典中(或者至少它会尝试,在这种情况下你会得到一个重复的键异常,以避免你也可以编写 _wordDictionary["Bad"] = true 替换现有的键(如果存在) .

这里更好的选择是使用静态构造函数。这将确保字典只初始化一次:

public class WordDictionary: MonoBehaviour{
public static Dictionary<string,bool> _wordDictionary = new Dictionary<string,bool> ();

static WordDictionary() {
_wordDictionary.Add ("Bad",true);
}

private void Start(){
}
}

现在您可以使用 WordDictionary 而不必担心每次实例化该类时字典都会增长。但在这一点上,将 WordDictionary 变成 MonoBehavior 确实没有用,因为它实际上只是一堆单词的容器。所以你的类(class)现在变成了:

public class WordDictionary: {
private static Dictionary<string,bool> _wordDictionary = new Dictionary<string,bool> ();

public static Dictionary<string, bool> Words {
get { return _wordDictionary; }
}

static WordDictionary() {
_wordDictionary.Add ("Bad",true);
}
}

我在这里添加了一个属性,因为您实际上应该使用属性,并且具有下划线名称(在我的代码世界中)意味着它是一个私有(private)字段。你可以扩展你的字典来做其他事情:

public class WordDictionary: {
private static List<string> _wordList = new List<string> ();

static WordDictionary() {
_wordList.Add ("Bad");
}

public static Contains(string word) {
return _wordList.Contains(word);
}

public static ContainsAny(IEnumerable<string> words) {
return words.Any(w => Contains(w));
}
}

我看不出有什么理由在这里使用 Dictionary,如果它包含这个词那么它就是“bad”,如果它不包含这个词那么它就是“good” .因此,更改为列表会使事情变得更简单。如果你隐藏“字典”在后台的工作方式,只暴露“包含”和“包含任何”方法,你有两个好处,使用变得更简单,你可以在不改变接口(interface)和下游的情况下改变底层“引擎”代码。

现在你的着色函数变得简单多了:

private void VerifyWords() {
if (openChat)
return;

var stringSplit = inputField.text.Split(' ');

if (WordDictionary.ContainsAny(stringSplit))
inputField.textComponent.color = Color.red;
else
inputField.textComponent.color = Color.green;
}

关于c# - 事件功能未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48818677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com