![]() This can help with both the centralization of data and version control. 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. Say you want to change the maximum speed of all those enemies. One example would be to create a custom asset that holds all the statistics for a given enemy type. 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. 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. When I did learn about them, however, I got ScriptableObject fever. I would not call them something that beginners urgently need to know when getting started. ![]() I had been programming in Unity for a decent amount of time before I learned about ScriptableObjects. 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! I make stuff in Unity and teach others how to do it via our Twitch channel and on YouTube. The scenario gets even more complicated if additional classes such as enemy AI need to access the player’s health, but we’ll consider only the UI class for now.I'm Matt Schell, the Senior Screencaster on the Online Evangelism team at Unity. Since the PlayerUI class directly depends on Player, those classes are closely coupled. The ‘traditional’ way to do this is to reference the Player directly via a variable on the PlayerUI script and read the variable directly. Other portions of the code, such as the UI, need to read that health value in order to display the health value. In my game, I have a Player class with an integer member variable to represent the player’s health. The designer must find the correct object instance in the hierarchy or prefab in the project view and modify values, which can be cumbersome. While it’s possible to expose variables to designers inside the Inspector through use of regular MonoBehaviour components, it can be clunky because you must attach them to a GameObject which needs to be instantiated in the scene either in-editor, cluttering up the hierarchy, or at runtime, which requires extra code to instantiate the object. On top of that, the Unity Editor’s interface is geared towards designers. In order to make code easy to change, as is especially common in game development, it’s a good idea to keep classes as loosely coupled as possible. Two portions of code are called decoupled when a small modification to one requires no or very minimal changes to the other. Let’s look at a few ways we can use this powerful tool to overhaul our projects.Ī good measure of how easy it is to change a piece of code without far-reaching ill effects is if there are few other parts of code that rely on it. The result will be a new object in the Project view in whichever folder is active. You can tweak the location of the option using the menuName and order variables, and the default name of the resulting asset file using the fileName variable. The menu is available by right-clicking in the Project view or using the Assets->Create menu on the toolbar. Creating a new type that derives from ScriptableObject works the same way as for any C# class – colloquially, when we inherit this class, we will say that we’re “creating our own types of scriptable object”. Unity assets are anything found inside the Project pane – audio clips and scripts fall under this category too. The primary usage for scriptable objects is to store data inside a Unity asset as an alternative to reading data from a file at runtime or containing the data inside a component attached to a GameObject. ![]() This article was available to $5+ Patreon supporters two weeks early. Let’s explore this powerful class and its features. However, there is another core class built into Unity which inherits directly from UnityEngine.Object but does not need to be attached to a GameObject to function: ScriptableObject. It derives ultimately from UnityEngine.Object through a couple of other classes and represents an object which must be attached to a GameObject to function properly – they are user-created Components. The MonoBehaviour class is probably the class you most commonly inherit from in Unity.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |