使用ScriptableObjects制作有趣的东西

I’m Matt Schell, the Senior Screencaster on the Online Evangelism team at Unity. I make stuff in Unity and teach others how to do it via our Twitch channel and on YouTube. I’ve produced some online video training using ScriptableObjects and thought it would be cool to put them together in a blog post, along with a few related Unite talks by people much smarter than me, and so here we are!

我是Unity在线宣教团队的高级电视广播员Matt Schell。 我在Unity中制作东西,并通过Twitch频道和YouTube教别人如何做。 我已经使用ScriptableObjects进行了一些在线视频培训,并认为将它们放到博客文章中以及一些比我聪明得多的人进行的Unite相关演讲是很酷的,所以我们到了!

I had been programming in Unity for a decent amount of time before I learned about ScriptableObjects. I would not call them something that beginners urgently need to know when getting started. When I did learn about them, however, I got ScriptableObject fever. I really began to use them a lot. They offer an interesting set of tools to extend your programming toolkit in Unity and helped me to think about certain architectural problems I’d been having in my projects a little differently. Overall, I find them to be very helpful.

在了解ScriptableObjects之前,我已经在Unity中编程了相当长的时间。 我不会称它们为初学者在开始时急需了解的东西。 但是,当我确实了解它们时,我便受到了ScriptableObject的狂热。 我真的开始大量使用它们。 他们提供了一组有趣的工具来扩展Unity中的编程工具包,并帮助我以不同的方式思考我在项目中遇到的某些体系结构问题。 总体而言,我发现它们非常有帮助。

So what are they? To quote the manual “A class you can derive from if you want to create objects that don’t need to be attached to game objects.” This is a good start, I’d add to this that they allow you to create objects which are assets which can either hold custom data or execute code.

那是什么 引用手册“如果您想要创建不需要附加到游戏对象上的对象,则可以从中得到一个类。” 这是一个好的开始,我要补充一点,它们使您可以创建对象,这些对象是可以保存自定义数据或执行代码的资产。

One example would be to create a custom asset that holds all the statistics for a given enemy type. Say you want to change the maximum speed of all those enemies. You could simply edit a single value on our ScriptableObject asset, as opposed to having those values stored in the public fields of a MonoBehaviour attached to an enemy prefab or prefabs. This can help with both the centralization of data and version control. These values also persist after being changed in play mode, since we are changing a value on an asset.

一个示例是创建一种定制资产,其中包含给定敌人类型的所有统计信息。 假设您要更改所有这些敌人的最大速度。 您可以简单地在ScriptableObject资产上编辑一个值,而不是将这些值存储在附加到敌方预制件的MonoBehaviour的公共字段中。 这可以帮助集中数据和版本控制。 这些值在播放模式下更改后也会保留,因为我们正在更改资产的值。

Another way that we can use them is to create assets which hold executable code. An example of this is seen in the video series below showing the Pluggable AI System. All of the pieces of code which compose the AI behavior are stored in ScriptableObject assets, which you can then configure and combine in the Inspector, without writing additional code.

我们可以使用它们的另一种方式是创建保存可执行代码的资产。 在以下显示可插拔AI系统的视频系列中可以看到一个示例。 组成AI行为的所有代码段都存储在ScriptableObject资产中,然后您可以在Inspector中对其进行配置和组合,而无需编写其他代码。

I learned about ScriptableObjects and was inspired to begin using them by watching my colleague Richard Fine’s excellent Unite talk on the subject “Overthrowing the MonoBehaviour Tyranny In a Glorious Scriptable Object Revolution”. For a ground-up primer on what they are, how they work and some example use cases, I highly recommend you check it out. Also that title, wow.

我了解了ScriptableObjects,并通过观看我的同事Richard Fine在“光荣的可脚本化对象革命中推翻MonoBehaviour暴政”这一主题的出色团结演讲而开始使用它们。 有关其基础知识,它们如何工作以及一些用例示例的基础知识,我强烈建议您检查一下。 还有那个标题,哇。

演示地址

One of the use cases that Richard demonstrates in this talk is a pluggable AI system with different ‘brains’ that can be plugged into AI tanks. I thought this concept was really fascinating and dug into his public source code to see how he had put it together. Once I understood the pattern I thought it would be really interesting to create a variant of his idea. I wanted to also allow the configuration of those ‘brains’ and the creation of variants and styles of behavior using a similar ‘pluggable’ approach. The result is my own Pluggable AI With Scriptable Objects Live Training Session. I won’t claim to have improved on Richard’s work or idea, but I very much enjoyed creating the system and I have used it in some personal projects with results I am happy with. Adding a nice Editor Window or Inspector to enable easier, more elegant editing and configuration would be a way to make it even more beautiful. I hope to be able to do this someday in the future (maybe) ((no promises!)) :)

Richard在本次演讲中演示的用例之一是具有不同“大脑”的可插入AI系统,可以将其插入AI储罐。 我认为这个概念真的很吸引人,并深入他的公共源代码以了解他是如何将其组合在一起的。 一旦理解了这种模式,我就认为创建他的想法的变体真的很有趣。 我还希望使用类似的“可插拔”方法配置这些“大脑”,并创建行为的变体和样式。 结果是我自己的带有脚本对象的现场培训课程的可插拔AI。 我不会声称会改进Richard的工作或想法,但是我非常喜欢创建该系统,并且将其用于一些个人项目中,结果令人满意。 添加漂亮的“编辑器窗口”或“检查器”以更轻松,更优雅地进行编辑和配置,将是使其更加美观的一种方法。 我希望将来有一天能够做到这一点(也许)((没有承诺!)):)

The Pluggable AI system is, I think, a nice example of the ‘modular code asset’ style of using ScriptableObjects, but importantly, ScriptableObjects can simply be used as custom data containers as well, to great effect. I used them for this purpose to create a similar pluggable architecture, in my Text Adventure Live Training series. Creating a Text Adventure game in Unity is, of course, massive overkill. You don’t need a powerful 3D engine to make such a game, but I found it to be a very fun programming exercise and many folks who follow our content on YouTube seemed to enjoy it as well. This project provided a nice opportunity from a teaching perspective to present some ideas about architecture and patterns, including the use of ScriptableObjects, that I think would have been difficult in a more visual style of game. In this case, I used ScriptableObjects to create custom data asset types for each ‘room’ in the game. I also used them to create each item, possible action, and action response. This meant that if I was on a team with a programmer and a game designer, for example, the programmer could create custom asset types, and then the designer could create instances of those types, for example, rooms, and populate them with descriptions, exits, and interactable items, all via editing in the Inspector. I thought this was a fun idea and a neat option to build an architecture in which all the pieces of your game could avoid becoming tightly coupled together in written code, allowing non-programmers to interact with it more readily. I wouldn’t propose to say that this is the only or even the best way to structure such a system, but certainly that it wasn’t the worst and it was fun to make, think about and work with.

我认为,可插拔AI系统是使用ScriptableObjects的“模块化代码资产”风格的一个很好的例子,但重要的是,ScriptableObjects也可以简单地用作自定义数据容器,从而产生很大的效果。 我在Text Adventure Live Training系列中将它们用于创建类似的可插拔体系结构。 当然,在Unity中创建Text Adventure游戏是极大的过大杀伤力。 您不需要强大的3D引擎来制作这样的游戏,但是我发现它是非常有趣的编程练习,并且许多关注我们YouTube内容的人也很喜欢它。 从教学的角度来看,该项目提供了一个很好的机会,可以提出一些有关架构和模式的想法,包括ScriptableObjects的使用,我认为在更直观的游戏风格中这是很难的。 在这种情况下,我使用ScriptableObjects为游戏中的每个“房间”创建自定义数据资产类型。 我还使用它们来创建每个项目,可能的操作和操作响应。 举例来说,这意味着如果我和一个程序员以及一个游戏设计师一起在一个团队中,那么程序员可以创建自定义资产类型,然后设计师可以创建这些类型的实例(例如房间),并用描述填充它们,退出以及可交互的项目,所有这些都可以通过在检查器中进行编辑来实现。 我认为这是一个有趣的主意,是构建一种架构的巧妙选择,在该架构中,游戏的所有部分都可以避免以书面代码紧密地耦合在一起,从而使非程序员可以更轻松地与其交互。 我不会建议说这是构建这样一个系统的唯一甚至最好的方法,但是可以肯定的是,这并不是最坏的情况,并且制作,思考和使用它很有趣。

The last video I wanted to share and talk briefly about is the newest. It’s a video of a talk at Unite Austin by Ryan Hipple of Schell Games (no relation!)  I recommend watching Richard Fine’s talk first, before watching Hipple’s talk since Hipple makes reference to Fine’s and builds on some of the fundamentals Fine has laid out. I find myself very inspired after watching this talk to try out some of the concepts that Hipple describes.

我想要分享和简短讨论的最后一个视频是最新的。 这是Schell Games的Ryan Hipple在Unite Austin上的演讲视频(没有关系!)我建议先观看Richard Fine的演讲,然后再观看Hipple的演讲,因为Hipple引用了Fine的基础,并借鉴了Fine制定的一些基础知识。 在观看了本次演讲并尝试了Hipple描述的一些概念后,我发现自己很受启发。

演示地址

One concept that I found very interesting was the idea of putting variables, like for example a single float value, into ScriptableObjects. We can then create assets out of these ScriptableObjects. The image below shows an example of this. The Player references a ScriptableObject asset which contains a single float variable, for example, containing the Player’s health. The other systems in the game reference this asset and get the current value from it, as opposed to referencing the ‘PlayerHealth’ component directly via code. This allows for the objects to all be coupled together in the Inspector instead of via hard-coded references. I was so taken with this idea that I asked Richard Fine if he saw some hidden down-side to it and he said that besides the fact that you’d have to make sure you reset each value via script manually (probably upon exiting play mode), it seemed reasonable.

我发现非常有趣的一个概念是将变量(例如单个float值)放入ScriptableObjects的想法。 然后,我们可以根据这些ScriptableObjects创建资产。 下图显示了此示例。 Player引用了一个ScriptableObject资产,该资产包含单个float变量,例如,包含Player的运行状况。 游戏中的其他系统引用此资产并从中获取当前值,而不是直接通过代码引用“ PlayerHealth”组件。 这允许将对象全部在检查器中耦合在一起,而不是通过硬编码的引用耦合在一起。 我被这个想法深深吸引,我问理查德·芬恩(Richard Fine)是否看到了一些隐藏的缺点,他说,除了必须确保手动通过脚本重置每个值(可能是退出播放模式)之外, ),这似乎很合理。

Personally, I really like the aesthetic of having lots of little objects you plug together in the Inspector. It seems, to me, less failure prone. I feel like it will also cause me to write less bad spaghetti code, which hard links all kinds of systems together in grotesque ways (something I am sadly prone to do). Of course I recognize that these kind of architectural decisions are highly aesthetic and almost religious in people’s desire to believe that theirs is the best, so of course, your mileage may vary. I for one have already been moving in this direction in my personal game project and so found this to be quite a cool idea. Maybe I’ll report back after I’ve tried it, or incorporate it into some upcoming material in my role as an Online Evangelist.

就个人而言,我真的很喜欢将许多小物件插入Inspector的美感。 在我看来,失败的可能性较小。 我觉得这也将使我编写更少的意大利面条代码,从而以怪诞的方式将各种系统硬链接在一起(我很容易这样做)。 当然,我认识到,这类建筑决策具有很高的美学价值,并且出于人们对自己的最佳决策的渴望,因此几乎是虔诚的,因此,当然,您的努力可能会有所不同。 我已经在我的个人游戏项目中朝这个方向发展,因此发现这是一个很不错的主意。 尝试过后,也许我会再报告一次,或者以我作为在线传播者的身份将其合并到一些即将发布的资料中。

Generally speaking, if you’re at the stage where you’re moving from creating small demos and prototypes into something larger destined for release, I think it’s a good idea to understand what ScriptableObjects are and how they can improve your workflow and architecture. I know that using them myself has allowed me to move away from doing a lot of gross, hacky-feeling things in my own projects and move in a more modular, elegant direction. Or so I think! What are your experiences with ScriptableObjects? Do you love them? Hate them? Please post a comment below, I’d be curious to hear your thoughts.

一般而言,如果您正处于从创建小型演示和原型转变为将要发布的较大版本的阶段,那么我认为了解ScriptableObject是什么以及它们如何改善工作流程和体系结构是个好主意。 我知道我自己使用它们使我摆脱了在我自己的项目中做很多粗鲁的,怪异的事情,而朝着更加模块化,优雅的方向前进。 大概我想! 您对ScriptableObjects有什么经验? 你爱他们吗? 讨厌他们? 请在下面发表评论,我很好奇您的想法。

翻译自: https://blogs.unity3d.com/2017/11/20/making-cool-stuff-with-scriptableobjects/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值