![]() ![]() Multiple objects can reference a single Scriptable Object through dragging and dropping in the Inspector. Now, this is pretty awesome, but it’s only part of the story. The Inspector recognizes them as referenceable objects. ![]() What does that mean? Scriptable Objects automatically work with all the editor systems. One of the main advantages of Scriptable Objects is that they are first-class citizens in the Unity editor. I had a hard time titling this section, but bear with me for a minute, and you’ll see where I’m going with this. Next, let’s explore the power of Scriptable Object as references. Plus, the stats StartingHealth, BaseSpeed, and BaseAttack are readable, which are the values most likely to change. The Scriptable Object is almost human-readable. For comparison, here’s a simple prefab: %YAML 1.1 What’s more, when conflicts occur, they’re easier to solve because Scriptable Object files are more readable. As a result, the only merge conflicts that arise come from two designers tweaking the same value. When the stats live inside a Scriptable Object, designers can change them without touching the prefab file at all. What if we could split these tasks into separate assets, eliminating merge conflicts? That’s where Scriptable Objects come in. In this case, someone will have to redo work. If multiple developers make simultaneous changes to the same prefab, it’s unlikely that those changes will merge cleanly. At the same time, artists may iterate on the visuals while gameplay developers attach new components. It’s common to run into a situation where a designer must balance stat values across several creatures in a game. When it comes to version control, merging complex assets is a pain. Scriptable Objects, Prefabs and Version Control So if you change these base values while your game is running, you’ll lose your carefully balanced stats. Not to mention, those modifications persist in and out of play mode. ![]() By the way, this is a crucial step because any modifications to Scriptable Objects will affect all the objects referencing it. Then, in the Start method, copy the base values from the MonsterType into this instance of a monster. Now, modify the Monster class to hold a reference to the Type Object. Public class MonsterType : ScriptableObject Create a new Type Object that derives from Scriptable Object with our base values. Let’s quickly build something to provide some context. This is a good approach, but when we throw a Scriptable Object into the mix, we gain several advantages for minimal cost. As I mentioned before, generally, in Unity, you would attach this component to a prefab and modify the stats on each variant to define new monsters. Here, our monster has three stats: Health, Speed, and Attack. Admittedly it’s a simple example, but it’s enough for demonstration purposes. using UnityEngine īehold the classic monster. We’ll create a bunch of monsters and experience the Scriptable Object advantage first-hand. You’ll still use prefab variants, but Scriptable Objects provide further benefits on top of the variant system. So if Unity already solved the problem, what’s the point of this article? Well, I’m here to offer a different approach. Then we make new types through data rather than through code. Rather than creating a Type to represent variants of a class, we create a single Type that holds all the variable data. Great! Turning code into data is the aim of the Type Object pattern. So, Unity has already helped us turn our code into data in the form of prefab files. Then for every new type, you’d create either a new prefab or prefab variant. Instead, you’d define some building component and attach that to a prefab. In Unity, it’s unlikely you’d take this approach at all. Using subclasses to define types is both a hassle and inflexible. Plus, you have to recompile your code every time you add a new building or change the cost of an existing building. Now you have a bunch of subclasses with nothing but a few variables changed here and there. Then, you’d subclass Building for each new type and adjust the building cost, sell price, income generated, etc. In classical OOP style, you maybe be tempted to start with a base Building class. Imagine you had to define a bunch of different buildings for your city builder game. There’s plenty of literature on the subject, but I’ll briefly explain the origins of this pattern. This article will demonstrate how to implement the Type Object pattern in Unity using Scriptable Objects. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |