简体   繁体   English

Unity3d 可视化脚本框架如何在幕后工作?

[英]How does a Unity3d visual scripting framework work behind the scene?

I'd like to know what happens when I use a visual scripting tool in unity3d.我想知道当我在 unity3d 中使用可视化脚本工具时会发生什么。

Let's say I want to make an FSM:假设我想制作一个 FSM:

The first approach is when I don't use such a tool.第一种方法是当我不使用这样的工具时。 In this case, I should do everything inside the source code(C#), meaning creating all the necessary classes and wiring up the whole state machine manually.在这种情况下,我应该在源代码 (C#) 中执行所有操作,这意味着创建所有必要的类并手动连接整个状态机。

The second approach is when I use a tool like Bolt or NodeCanvas .第二种方法是当我使用像BoltNodeCanvas这样的工具时。 I still need to write the State classes, but this time, wiring them up is done visually through a node-based graph editor.我仍然需要编写 State 类,但这一次,通过基于节点的图形编辑器以可视方式将它们连接起来。

The question is, how these tools convert that graph into something that Unity can use?问题是,这些工具如何将该图转换成 Unity 可以使用的东西? Do they generate C# code based on the graph using a templating engine like T4 Templating Engine ?他们是否使用T4 模板引擎等模板引擎基于图形生成 C# 代码? Or do they do something else?或者他们会做其他事情吗?

They can either generate some C# code or save the graph definitions to a file, add a runtime execution engine to your game and interpret those while the game is running.他们可以生成一些 C# 代码或将图形定义保存到文件中,向您的游戏添加运行时执行引擎并在游戏运行时解释这些引擎。 They might also take a hybrid approach or do some performance optimizations .他们也可能采用混合方法或进行一些性能优化 So either way is possible and you should consult the tools' design docs to make sure.所以任何一种方式都是可能的,你应该查阅工具的设计文档来确定。

As for Bolt , this post on the developer's official blog, implies that current stable version doesn't generate any C# code.至于Bolt ,开发者官方博客上的这篇文章暗示当前的稳定版本不会生成任何 C# 代码。 But apparently the team is actively working on a new version that supports code generation.但显然该团队正在积极开发支持代码生成的新版本。

We are currently actively working on Bolt 2, a major new version that includes massive overhauls and new features such as C# generation, classes, vertical flow, tweening, generics, a fresh new look, and a lot more.我们目前正在积极开发 Bolt 2,这是一个主要的新版本,包括大规模的检修和新功能,例如 C# 生成、类、垂直流、补间、泛型、全新的外观等等。

As for NodeCanvas , I'm not sure if it's generating any C# code, but judging by the package contents on the assets store, looks like it's working in a similar way to Bolt.至于NodeCanvas ,我不确定它是否生成任何 C# 代码,但从资产商店中的包内容来看,它的工作方式与 Bolt 类似。

Unity has editor/playmode/runtime mode. Unity 有编辑器/播放模式/运行时模式。 In editor mode drawing your design is saved in file.在编辑器模式绘图中,您的设计保存在文件中。 Some tools use custom formats binaries,json or else.一些工具使用自定义格式二进制文件、json 或其他。 LogicForge for example,use Scriptable object to be fully compatible with Unity dependency and serialziation managment.例如 LogicForge,使用 Scriptable 对象完全兼容 Unity 依赖和序列化管理。 Graph can be designed in editor and/or playmode.图形可以在编辑器和/或播放模式中设计。 Logic forge supports both, cause in playmode you can see what your design is doing right away. Logic Forge 两者都支持,因为在播放模式下您可以立即看到您的设计在做什么。 Node can represent atomic function or complex component.节点可以表示原子功能或复杂组件。 It is good if visual tool gave you opportunity to design own nodes, also visually and create nodes automatically of any MonoBehaviour script dropped in design.如果可视化工具让您有机会设计自己的节点,也可以可视化并自动创建设计中丢弃的任何 MonoBehaviour 脚本的节点,那就太好了。 Node can be dynamic or static, already coded.节点可以是动态的或静态的,已经编码。 Dynamic nodes use Reflection to create functions playmode/runtime.动态节点使用反射来创建函数播放模式/运行时。 They are optimized with cached and IL created Delegates.它们使用缓存和 IL 创建的委托进行了优化。 Code can be generated when you make change to graph, but you need to wait long Unity recompile, or can be generated on request.更改图形时可以生成代码,但Unity重新编译需要等待很长时间,也可以按要求生成。 Tools can have option to compact the design creating code and more complex node from design.工具可以选择压缩设计创建代码和更复杂的设计节点。
Graph asset can be attached to GameObject and load with the scene, or can be loaded as resource runtime and then implement the design.They read serialized data and create components on the game object. Graph asset 可以附加到 GameObject 并随场景加载,或者可以作为资源运行时加载然后实现设计。它们读取序列化数据并在游戏对象上创建组件。

Some tools have FSM incorporated so you can switch between different graph designs/logics by condition.一些工具集成了 FSM,因此您可以根据条件在不同的图形设计/逻辑之间切换。 Logic Forge has general event based FSM and animation playables blender(you don't need to use Mecanim) Logic Forge 具有基于通用事件的 FSM 和动画播放器 blender(您不需要使用 Mecanim)

You have to understand lexer and parser to understand it fully.您必须了解lexer and parser才能完全理解它。

When you write something in non-graphical way, computer turns that piece of code or codes in machine readable format, as computer can't even distinguish between a and b .当您以非图形方式编写内容时,计算机会将这段代码或代码转换为机器可读格式,因为计算机甚至无法区分ab It converts it into binary or hexa (which is later even turned into binary ).它将它转换成binaryhexa进制(后来甚至变成binary )。

Now the question still remains How visual scripting works?现在问题仍然存在可视化脚本是如何工作的?

Ok, the developer has their own lexer and parser, they iterate through those visual components and create code from that, then those are interpreted or compiled, depending on language.好的,开发人员有自己的词法分析器和解析器,他们遍历这些可视组件并从中创建代码,然后根据语言对这些组件进行解释或编译。

As you want to know about Unity , it is based on C# , it will be compiled.正如您想了解的Unity ,它基于C# ,它将被编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM