Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Extending Unity with Editor Scripting

You're reading from  Extending Unity with Editor Scripting

Product type Book
Published in Sep 2015
Publisher
ISBN-13 9781785281853
Pages 268 pages
Edition 1st Edition
Languages

Table of Contents (18) Chapters

Extending Unity with Editor Scripting
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Getting Started with Editor Scripting 2. Using Gizmos in the Scene View 3. Creating Custom Inspectors 4. Creating Editor Windows 5. Customizing the Scene View 6. Changing the Look and Feel of the Editor with GUI Styles and GUI Skins 7. Saving Data in a Persistent Way with Scriptable Objects 8. Controlling the Import Pipeline Using AssetPostprocessor Scripts 9. Improving the Build Pipeline 10. Distributing Your Tools Index

Chapter 7. Saving Data in a Persistent Way with Scriptable Objects

When you are in the final stage of the implementation of features for your video game, the next step is usually polishing and tweaking, investing most of the time to finding those special values that make your video game awesome and unique.

It is annoying when you find those values in your video game and suddenly you realize you were in Play mode. At this point, you have two options; you can either write down all these values, sometimes they are a lot, or start over.

In Unity, there is a special type of class called Scriptable Object, which is mainly used as a data container. One of its characteristics, if is used correctly, is the ability to save the changes you make to the scene during the Play mode.

You will learn how to create and use Scriptable Objects in Unity and use them to contain gameplay parameters from a level in Run & Jump.

The main topics that will be covered in this chapter are:

  • Creating the Scriptable Object...

Overview


A Scriptable Object is a Unity special object type that doesn't need to be attached to a game object on a scene to exist, because it can be saved as an asset in the project. This class is used as a base for most of the special editor classes that we saw in the previous chapters, such as the Editor and EditorWindow class. However, the principal use for that in this chapter is to going to be saving data in a persistent way.

In some scenarios, this has benefits over using XML, JSON, or plain text files because Unity will handle all the serializing and parsing for you without the necessity of a custom parser or third-party tool.

When you create tools for game designers, you may want to allow them to experiment with values that affect how the video game behaves.

In this chapter, you will learn how to use Scriptable Objects to store data and make it persist in the Play mode.

Defining the chapter goals

In this chapter, we will use Scriptable Objects to allow game designers to tweak level settings...

Preparing the environment


Before we start playing with Scriptable Objects, we are going to play with the gravity of a custom level an see what happens.

Updatable gravity in levels

Right now, if you want to adjust the gravity of a level you must to make the changes before pressing the Play button in order to see results.

The Level class has a method called SetGravity, which is responsible for taking the value of the gravity property and applying it to the Physics 2D settings in Unity. You don't need to take care about how this is implemented. The only thing we must to do is to integrate this in the custom inspector we created for the Level class, so each time the gravity value changes, the SetGravity method will take care of the rest.

Let's update the LevelInspector class to achieve this. At the end of the DrawLevelDataGUI method, add the following line of code:

_myTarget.SetGravity();

This method checks the value of the gravity and updates this if necessary. Pretty simple!

Playing with gravity

Using...

Implementing a Scriptable Object


In this section, we will create a ScriptableObject class and then reallocate the gravity, bgm, and background variables there.

Creating the data class

The ScriptableObject class is part of the UnityEngine namespace. You derive from this class if you want to create objects that don't need to be attached to a game object, but more often, if you are looking for objects meant to save data.

Let's create a script called LevelSettings.cs inside the folder Scripts/Levels and add the following code:

using UnityEngine;
using System;

namespace RunAndJump {

  [Serializable]
  public class LevelSettings : ScriptableObject {

    public float gravity = -30;
    public AudioClip bgm;
    public Sprite background;
  }
}

The first step was to make the class extend from the ScriptableObject class. Then, we added the public variables to represent each variable we want to save in this class.

We added the namespace System to use the attribute Serializable. Using this attribute, we...

Integrating the Scriptable Object with the level


In this section, we will integrate the LevelSetting class with our levels and then test how Scriptable Objects allow us to modify values in the play mode.

Updating the Level and the LevelInspector class

The first change to be made is to update the Level class. So, instead of using the current variables for the gravity, bgm, and background, start using a LevelSetting class as a reference.

Let's make a small update in Level.cs file. Add the following lines to it:

[SerializeField]
  private LevelSettings _settings;

  public LevelSettings Settings {
    get { return _settings; }
    set { _settings = value; }
  }

We need to take care of the render of this new property.

We will update the DrawLevelDataGUI method in the LevelInspector class:

private void DrawLevelDataGUI () {
      EditorGUILayout.LabelField ("Data", _titleStyle);
      EditorGUILayout.BeginVertical ("box");
      EditorGUILayout.PropertyField (_serializedTotalTime);
      _myTarget.Settings...

Summary


Scriptable Objects are not the most used feature of Unity but are useful it is good to keep them in the solution sets approaches for our video game.

They are used as assets, which are only meant to store data, but can also be used to help serialize objects and can be instantiated in our scenes. In some scenarios, they are also an alternative to XML, JSON, or plain text files to define configuration parameters.

Based on what we did in this chapter, with a Scriptable Object approach, it is now possible to keep changes you make to settings values while your game is running in play mode, easily swap between different sets of settings values, and allowing the separation of logic and data.

In the next chapter, we are going to work improving the asset import pipeline.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Extending Unity with Editor Scripting
Published in: Sep 2015 Publisher: ISBN-13: 9781785281853
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}