iT邦幫忙

0

Unity funtion call queue

  • 分享至 

  • twitterImage

在Unity遊戲中,使用者可以自行拖曳幾個方塊來決定動作的順序,舉例來說,方塊有四個:
Run(speed);
Jump(height);
Stop();
Wait(seconds);

使用者就可以從web端透過javascript自行定義要
Run>Wait(3)>Jump>Stop>: 跑3秒,跳起來,然後停止動作
還是要
Jump>Wait(2)>Run>Stop: 跳起來,等2秒後往前跑,然後馬上停下來

請問要怎麼實作這個自訂的行為?
如果是javascript的話,我還可以用eval來偷吃步把function name字串當成真的function來執行(儘管eval是少用為妙),但C#沒有類似eval的function,所以我卡關了XD

應該主要是卡在wait這邊,我希望等待後才做後續的動作,要如何才能把後續的動作排程進去?

目前寫的C#:

    // 設定延時
    // web中這樣呼叫使用: gameInstance.SendMessage("people", "Wait", "毫秒數");
    public void Wait(string milliseconds)
    {
        isWait = true;
        float seconds = float.Parse(milliseconds) / 1000;
        StartCoroutine("Delay", seconds);
    }
    IEnumerable Delay(float seconds)
    {
        if (isWait)
        {
            yield break;
        }

        
        yield return new WaitForSecondsRealtime(seconds);

        // code to excute after delay
        isWait = false;
        // and then how to do next gamInstance.SendMessage("people", "command", "parameter")?
        // 若直接呼叫,等待就沒意義,必須要能排進某個queue,還要可以做為function執行才行?
    }

javascript:

// 使用者自訂了執行順序,會產生這樣的 code 來呼叫 C# 的 function 控制 Unity 遊戲
setInterval(function () {
    // 這邊做了一些判斷還有其他事情,才會發生以下自訂行為
    if (somethingNeedToDoIsTrue()) {
        window.gameInstance.SendMessage('people', 'Run', '80');
        window.gameInstance.SendMessage('people', 'Wait', '3000');
        window.gameInstance.SendMessage('people', 'Jump', '30');
        window.gameInstance.SendMessage('people', 'Stop');
    }
}, 1);

我自己都問得有點混亂了,如果有什麼需要補充的,請跟我說我會補上來,謝謝。

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
JamesDoge
iT邦高手 1 級 ‧ 2023-01-30 08:24:25

在Unity遊戲中,使用者可以自行拖曳幾個方塊來決定動作的順序,舉例來說,方塊有四個:
Run(speed);
Jump(height);
Stop();
Wait(seconds);

使用者就可以從web端透過javascript自行定義要
Run>Wait(3)>Jump>Stop>: 跑3秒,跳起來,然後停止動作
還是要
Jump>Wait(2)>Run>Stop: 跳起來,等2秒後往前跑,然後馬上停下來

請問要怎麼實作這個自訂的行為?

C#

// Define a queue to store commands from javascript
private Queue<Command> commandQueue = new Queue<Command>();

public class Command
{
    public string action;
    public string parameter;
}

// Add a new command to the queue
public void AddCommand(string action, string parameter)
{
    commandQueue.Enqueue(new Command() { action = action, parameter = parameter });
}

// Use a coroutine to process the command queue
private IEnumerator ProcessCommandQueue()
{
    while (commandQueue.Count > 0)
    {
        Command command = commandQueue.Dequeue();
        switch (command.action)
        {
            case "Wait":
                float seconds = float.Parse(command.parameter) / 1000;
                yield return new WaitForSecondsRealtime(seconds);
                break;
            case "Run":
                Run(command.parameter);
                break;
            case "Jump":
                Jump(command.parameter);
                break;
            case "Stop":
                Stop();
                break;
        }
    }
}

// Start processing the command queue when necessary
private void Update()
{
    if (commandQueue.Count > 0)
    {
        StartCoroutine(ProcessCommandQueue());
    }
}

Javascript

setInterval(function () {
    if (somethingNeedToDoIsTrue()) {
        window.gameInstance.SendMessage('people', 'AddCommand', 'Run', '80');
        window.gameInstance.SendMessage('people', 'AddCommand', 'Wait', '3000');
        window.gameInstance.SendMessage('people', 'AddCommand', 'Jump', '30');
        window.gameInstance.SendMessage('people', 'AddCommand', 'Stop', '');
    }
}, 1);

如果是javascript的話,我還可以用eval來偷吃步把function name字串當成真的function來執行(儘管eval是少用為妙),但C#沒有類似eval的function,所以我卡關了XD

應該主要是卡在wait這邊,我希望等待後才做後續的動作,要如何才能把後續的動作排程進去?

可以使用Queue來解決這個問題,將需要執行的動作存進Queue中,每當一個動作完成,再從Queue中取出下一個動作執行,直到Queue為空為止。
以C#實現的話,可以使用Queue<Action>存放每個動作,當一個動作完成時,再從Queue中取出下一個動作執行。

我要發表回答

立即登入回答