Mobile gaming is more popular than ever, which means your game is up against stiff competition. This is why it’s more important than ever to captivate your players with seamless and immersive gameplay. This can prove to be difficult, because behind a successful mobile game lies a complex web of code, design, and creativity that brings virtual worlds to life. This is where a powerful tool that’s revolutionized game development in the Unity engine becomes relevant — scriptable objects.
In this blog post, we’ll cover the following:
- What are scriptable objects?
- Why should you use scriptable objects?
- How do scriptable objects work?
- How do you create a scriptable object?
- How can you incorporate scriptable objects into a game system?
What are scriptable objects?
In Unity, scriptable objects are an asset type that allows engineers and designers to store data and behavior separately from game objects. Scriptable objects act as containers for variables, functions, and other data that can be accessed and used within the game. They are a fundamental part of Unity prototyping and game development.
As a Unity engineer, you may be more familiar with MonoBehaviors. Unlike MonoBehaviors, which are attached to specific game objects, scriptable objects are not attached to any scene-level objects, which makes them different in many key ways. For example, this often makes them ideal for configuration data.
When compared to other Unity components like MonoBehaviors, scriptable objects are unique. While MonoBehaviors are scripts that define the behavior of specific game objects, scriptable objects focus on storing and manipulating data outside of the scene layer.
Why should you use scriptable objects?
These are a few reasons why game engineers rely on scriptable objects.
Reusability and modularity
Scriptable objects are a tool that allows developers to create independent, self-contained assets that can be easily reused across different parts of a game or even in future projects. This promotes efficient development practices, reduces duplication of code and assets, and saves valuable time and effort.
Encouraging data-driven design
Scriptable objects separate the logic and behavior of game objects from the data they operate on. This approach empowers designers and level creators to tweak and fine-tune gameplay parameters without the need for coding, alongside fostering iterative development and enabling rapid prototyping.
Simplifying asset management
Scriptable objects simplify asset management by providing a centralized location for storing and organizing data, making it easier to maintain and update game content.
Storing data in a non-scene dependent way
Scriptable objects offer the advantage of storing data in a non-scene dependent way, allowing information to persist across multiple scenes. This enhances flexibility, component communication, and interoperability, helping to provide a seamless player experience.
As an engineer, leveraging these benefits can revolutionize the way you architect games and elevate them to new heights.
How do scriptable objects work?
The lifecycle of a scriptable object begins with its creation within Unity’s editor, where engineers can define its variables, functions, and other data. Once created, scriptable objects can be accessed and modified by scripts and game objects within the game. This accessibility is made possible through Unity’s serialization system, which allows the data of scriptable objects to be saved and restored during the length of the app run.
In other words, a player can background a game and return to it without destroying the state of active scriptable objects. This is because scriptable objects are only reinitialized to their original designer-set values on the next game session, which occurs whenever the app opens from a closed state or whenever the user force quits the app.
How do you create a scriptable object?
Here are the steps to create a scriptable object:
- Create a scriptable object: Right-click in the Project window (the area where your assets are stored) and select “Create” from the context menu. Then, navigate to “Create > C# Script” and name it according to your desired scriptable object’s functionality. For example, let’s call it “MyScriptableObject”.
- Open the script: Double-click on the newly created script file (“MyScriptableObject.cs”) to open it in your preferred code editor.
- Inherit from ScriptableObject: In the script file, change the class declaration line to inherit from “ScriptableObject” instead of “MonoBehaviour”. It should look like this:
using UnityEngine;
[CreateAssetMenu(fileName = "NewScriptableObject", menuName = "Custom/Create Scriptable Object")]
public class MyScriptableObject : ScriptableObject
{
// Add your variables, functions, and data here
}
- Define variables and data: Inside the script, add the necessary variables, functions, and data that you want your scriptable object to hold and manipulate. For example, you can add public variables like strings, ints, floats, or even references to other objects in your game.
using UnityEngine;
[CreateAssetMenu(fileName = "NewScriptableObject", menuName = "Custom/Create Scriptable Object")]
public class MyScriptableObject : ScriptableObject
{
// The following variables will automatically be exposed and serialized.
public string playerName;
public int playerLevel;
// Add more variables and functions as needed
}
- Customize creation menu: Above the class declaration line, add the `[CreateAssetMenu]` attribute and specify the desired file name and menu name. This attribute tells Unity to display a custom creation option in the context menu.
- Save and close: Save the changes to your script file and close the code editor.
- Create an instance: Go back to the Unity Editor. Right-click in the Project window and navigate to the custom menu item you specified in the `[CreateAssetMenu]` attribute. In this case, it would be “Custom > Create Scriptable Object > NewScriptableObject”. Click on it, and Unity will create a new instance of your scriptable object. If you adjust the menuName, the Unity editor will use that as the menu path to the asset.
- Modify and access data: With the newly created instance selected, you can modify its data through the Inspector window. The public variables you defined in the script will be accessible and editable in the Inspector. You can assign values, such as setting the player name or player level.
- Use the scriptable object: You can now use the created scriptable object instance in your game code. You can reference it in other scripts, assign it to variables, or use it as a data source for gameplay logic.
How can you incorporate scriptable objects into a game system?
Now that you’ve created the scriptable object, the next step is to incorporate it into your game.
- Identify the game system: Determine the specific game system or feature you want to enhance or create using scriptable objects. It could be anything from an inventory system, character stats, AI behavior, level design, or even the system itself!
- Design the game system: Plan out how the scriptable object will work in your game. Determine which other scripts or components will reference or use the data stored in the scriptable object.
- Create a scriptable object: Follow the previous step-by-step guide to create a scriptable object that corresponds to the game system you identified. Make sure to define the necessary variables, functions, and data within the scriptable object to represent the behavior and characteristics of the system.
- Reference the scriptable object: In the scripts or components that need access to the data stored in the scriptable object, create a public variable of the type of the scriptable object you created. For example, if you created a scriptable object called “InventoryData”, you would declare a variable like this: public InventoryData inventoryData.
- Assign the scriptable object: In the Unity Editor, locate the game object that contains the script or component referencing the scriptable object. In the Inspector window, find the public variable you created in the previous step and assign the corresponding scriptable object instance to it. You can either create a new instance of the scriptable object or select an existing one — it’s as simple as dragging and dropping the asset.
- Access data and behavior: Within the script or component, you can now access the data and behavior/functions like any other class or MonoBehavior that you’ve defined before.
- Use the scriptable object data: Use the data from the scriptable object to drive the functionality of your game system. Depending on the system, you may need to perform calculations, trigger events, or update other game objects with this data. In this way, you can do things like inject damage modification functionality through function injection or something similar.
- Repeat for additional systems: If your game has multiple systems or features that could benefit from scriptable objects, you can repeat steps 2-7 to create and incorporate additional scriptable objects into the respective systems.
- Iterate and refine: As you develop your game and iterate on its systems, you can easily update the data within the scriptable objects without modifying other parts of your game. This flexibility allows you to refine and tweak your game systems efficiently.
Empowering mobile engineers in game development
The journey of mobile game development doesn’t end with the creation or incorporation of scriptable objects. As mobile games increase in complexity, engineers face the tough challenge of delivering great player experiences at scale.
In order to build the boldest, most compelling mobile games, Unity teams need visibility into every aspect of their player experiences. That way, whether the issue stems from a frustrating user journey, crash, app freeze, error, failing API endpoint, or more, Unity engineers can efficiently remediate it and get back to building.
Embrace provides the full technical and behavioral details of your player experiences in production, so you can build and ship with confidence. Want to increase your Play Store ranking and reduce user churn? Explore Embrace for free today.
Learn how to identify these issues and hold ad vendors accountable.
Download now