Unity 和 Google 的 Blockly 库集成

Gar*_*ara 5 unity-game-engine blockly

对于我的本科期末项目,我想开发一个教育游戏来教授基本编程,所以我想为他们提供一些简单的拖放可视化编程编辑器,就像在这段代码中一样,但我不知道如何做到这一点,我是新来的团结起来,我在谷歌上做了很多搜索,但我没有得到它(我很迷失)。所以请任何人都可以帮助我,并给我一个线索,以便我可以在此基础上进行构建。感谢您的帮助,这是我的游戏设计说明的一个示例(我想通过拖放来移动玩家,向右移动,向上移动,向前移动......)。我回家我的想法和问题很清楚

Pao*_*ngo 4

几个月前,我开发了一个与您的项目非常相似的项目。我最近从这个项目中推断出一个库并将其发布在 github 上。该库被调用blockly-gamepad 并允许您块地创建 a 的结构并使用或 等game方法与其交互。我相信这将大大简化和 之间的交互,如果你对文档感兴趣你可以找到游戏的现场。play()pause()

blocklyunitydemo


这是演示的 gif。

在此输入图像描述

怎么运行的

与正常使用 blockly 相比,这是一种不同且简化的方法。

首先,您必须定义块(请参阅 文档中如何定义它们)
您不必定义any code generator,所有与代码生成有关的事情都由库执行。

在此输入图像描述


每个块生成一个请求

// the request
{ method: 'TURN', args: ['RIGHT'] }
Run Code Online (Sandbox Code Playgroud)


当执行一个块时,相应的请求将传递到您的游戏

class Game{
    manageRequests(request){
        // requests are passed here
        if(request.method == 'TURN')
            // animate your sprite
            turn(request.args)
    }
}
Run Code Online (Sandbox Code Playgroud)


您可以使用Promise来管理异步动画

class Game{
    async manageRequests(request){
        if(request.method == 'TURN')
            await turn(request.args)
    }
}
Run Code Online (Sandbox Code Playgroud)


块和游戏之间的链接由游戏手柄管理。

let gamepad = new Blockly.Gamepad(),
    game = new Game()

// requests will be passed here
gamepad.setGame(game, game.manageRequest)
Run Code Online (Sandbox Code Playgroud)


游戏手柄提供了一些方法来管理块的执行,从而管理请求的生成

// load the code from the blocks in the workspace
gamepad.load()
// reset the code loaded previously
gamepad.reset()

// the blocks are executed one after the other
gamepad.play() 
// play in reverse
gamepad.play(true)
// the blocks execution is paused
gamepad.pause()
// toggle play
gamepad.togglePlay()

// load the next request 
gamepad.forward()
// load the prior request
gamepad.backward()

// use a block as a breakpoint and play until it is reached
gamepad.debug(id)
Run Code Online (Sandbox Code Playgroud)

您可以在此处阅读完整的文档。
我希望我对这个项目有所帮助并祝你好运!


编辑:我更新了库的名称,现在它被称为blockly-gamepad