如何逐步使用功能

时间:2019-07-01 08:16:37

标签: c# unity3d

对不起,标题奇怪,我只是不知道如何命名这个问题。

所以我有这样的功能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”等。

我怎么能意识到?预先感谢。

3 个答案:

答案 0 :(得分:3)

根据您的需要,您可以在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++;
}

答案 1 :(得分:2)

定义这样的类字段:

function test() {

    var queryString = "select * from Product where PEnable = 'true' and PType = '1'"
    const pool = new sql.ConnectionPool(config);
    var conn = pool;
    let jim = '';
    return conn.connect().then(function () {
        var req = new sql.Request(conn);

       return req.query(queryString).then(function (result) {

           return result['recordset'][0]['Name'];

            conn.close();

            console.log('result >' , result['recordset'][0]['Name']);

        }).catch(function (err) {
            console.log('Unable to add result >', err);
            conn.close();
        });

    }).catch(function (err) {
        console.log('Unable to connect to SQL >', err);
    });
    return jim;
}

现在每次碰到空格:

int count = 0;

答案 2 :(得分:0)

此代码:

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

仅适用于Unity,在Visual Studio中,您必须为要执行此操作的任何对象创建一个Event,例如,如果您想每次按空格键时都调用void,则必须执行此操作很简单:(下图)

在属性窗口中,按螺栓图标,然后双击要创建的事件(样本):TextChanged,LocationChnaged,MouseMove等...

我将在 TextBox对象

上使用 KeyDown

image

现在在您的代码中应该会生成该空缺

image

在这个空白中,我编写了代码,看起来就是这样:

(将int n = 1置于空位之前)

private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Space)
            {
                //int n = 1; must be defined
                textBox1.Text = "test text " + n; 
                n++;
            }
        }

现在,每当您按下或保持按下空格键时,文本框将填充“测试文本”,并且每次将其值再增加1。

相关问题