gpt4 book ai didi

c# - 如何一步步使用函数

转载 作者:太空宇宙 更新时间:2023-11-03 19:39:03 26 4
gpt4 key购买 nike

抱歉这个奇怪的标题,我只是不知道如何命名这个问题。

所以我有这样一个函数say()

void say(string printedText) {
gameText.text = printedText;
}

而且我需要多次使用它。像这样:

say("test text 1");
say("test text 2");
say("test text 3");
...

我需要通过单击空格键来更改文本。当然我需要使用这样的东西:

if(Input.GetKeyDown(KeyCode.Space)) {
...
}

但我无法理解如何逐步显示文本。因此,例如,如果我单击空格键一次,我应该会看到“测试文本 1”。下一次点击应该会显示“测试文本 2”等。

如何实现?提前致谢。

最佳答案

根据您的需要,您可以在 List<string> 中存储不同的文本或者甚至 Queue<string>

列表示例

// Add your texts in the editor or by calling texts.Add(someNewString)
public List<string> texts = new List<string>();

private int index = 0;

if(Input.GetKeyDown(KeyCode.Space))
{
// have a safety check if the counter is still within
// valid index values
if(texts.Count > index) say(texts[index]);
// increase index by 1
index++;
}

数组示例

List<string>基本相同但是你不能“即时”添加或删除元素(至少不是那么简单)

public string[] texts;

private int index = 0;

if(Input.GetKeyDown(KeyCode.Space))
{
// have a safety check if the counter is still within
// valid index values
if(texts.Length > index) say(texts[index]);
// increase index by 1
index++;
}

队列示例

public Queue<string> texts = new Queue<string>();

将新文本添加到队列的末尾

texts.Enqueue(someNewString);

然后

if(Input.GetKeyDown(KeyCode.Space)) 
{
// retrieves the first entry in the queue and at the same time
// removes it from the queue
if(texts.Count > 0) say(texts.Dequeue());
}

简单计数器

如果它真的只是关于具有不同的 int 值那么是的,只需使用一个字段

private int index;

if(Input.GetKeyDown(KeyCode.Space))
{
// uses string interpolation to replace {0} by the value of index
say($"test text {0}", index);
// increase index by one
index++;
}

关于c# - 如何一步步使用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56832172/

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