Book Image

Extending Unity with Editor Scripting

Book Image

Extending Unity with Editor Scripting

Overview of this book

Table of Contents (18 chapters)
Extending Unity with Editor Scripting
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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...