Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Extending Unity with Editor Scripting

You're reading from   Extending Unity with Editor Scripting Put Unity to use for your video games by creating your own custom tools with editor scripting

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher
ISBN-13 9781785281853
Length 268 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Angelo R Tadres Bustamante Angelo R Tadres Bustamante
Author Profile Icon Angelo R Tadres Bustamante
Angelo R Tadres Bustamante
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Getting Started with Editor Scripting FREE CHAPTER 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

Introducing Run & Jump

Run & Jump is a 2D platformer video game created for this book to serve as a base for our editor scripting experiments. In this section, we will talk about the video game and what kind of things we want to achieve.

Keep in mind that it is not important to understand in detail how Run & Jump is implemented. It's enough just to understand the workflows associated with the content creation of this video game.

Playing the video game

In this video game, the player takes control of Timmy, a guy who likes to collect coins and invests his time searching for hidden treasures. On his journey, he needs to avoid obstacles and enemies to reach the finale of each level and win. You can see how the video game looks in the following screenshots:

Playing the video game

To play the video game, you will have to clone or download the project from https://github.com/angelotadres/RunAndJump in GitHub.

When you are ready, open the project in Unity. You will see the following folder structure in the Project browser:

Playing the video game

To test the game, open the scene Title inside the Scenes folder and then press the Play button in the Unity toolbar:

Playing the video game

To control Timmy, use the left and right arrows on your keyboard. Pressing the space bar makes him jump, and pressing it again while he is in the air makes him perform a double jump.

Currently, there are three levels implemented for this video game to test its functionality. In the next section, you will learn how to add more levels.

Creating a new level

In this video game, each level is a Unity scene inside the folder Levels. When you start playing Run & Jump and then select a specific level, the video game call the LevelHandler scene and this starts the script LevelHandlerScene.cs.

This scene has all the GUI necessary for the level, and the script is responsible for the game's status detection (playing, paused, and so on), when the player wins or loses, and loading of the specific level scene using the method Application.LoadLevelAdditive.

Note

Unlike LoadLevel, LoadLevelAdditive does not destroy objects in the current scene. Objects from the new scene are added over the current one.

Each level scene is composed of several prefabs. We will refer to these in the rest of the book as level pieces prefabs.

Navigate to Prefabs | LevelPieces to check the available level piece prefabs. The following table contains a description of each one:

Level Piece

Description

Creating a new level

Timmy (Player.prefab):

You control this character in the game. Timmy's abilities are run, jump and double jump. There's nothing to envy about the Italian plumber.

Creating a new level

Angry Blob (EnemyAngryBlob.prefab):

This character moves over platforms from one side to the other with an angry face. You don't like him and he doesn't like you so don't touch him or you will lose a life!

Creating a new level

Coins (InteractiveCoin.prefab):

It is not a real platform video game without something to collect. Coins are one of the collectibles, and when you pick one, your score increases by 100 points.

Creating a new level

Treasure (InteractiveTreasure.prefab):

Usually, this collectable is well hidden in order to motivate the player to explore the level. When you pick one, your score increases by 1,000 points.

Creating a new level

Sign (InteractiveSign.prefab):

This will display a message on the screen when the player is around the sign board. The sign is used to give the player hints or miscellaneous information about the current level.

Creating a new level

Spikes (HazardSpike.prefab):

These sharp spikes are placed in locations that make it harder to reach your objective. Don't touch them or you will lose a life!

Creating a new level

Dirt (SolidDirt.prefab):

This is used as a building block for the level.

Creating a new level

Grass (SolidGrass.prefab):

Like Dirt, this too is used as a building block for the level. The only difference is this it's green on the top.

Creating a new level

Goal flag (InteractiveGoalFlag.prefab):

The main objective of the video game is to reach the Goal flag at the end of each level. A well-designed level will have a lot of hazards and enemies between you and the goal flag.

To get a better understating of what is involved in creating levels, let's create a new one. The goal is to copy the following level (or at least try to do so):

Creating a new level

For this, you need to perform the following steps:

  1. Create a new scene and remove the default camera.
  2. Add a new Game Object to the scene and attach the level.cs script located in Scripts | Level. This script contains the base to make our level work.
  3. Navigate to Prefabs | LevelPieces and clone the prefabs in the scene until you complete creating the level. All the prefabs must be nested inside the game object you created earlier.
  4. When you are done, click again on the root game object. If you check the Inspector window, you will see the following:
    Creating a new level

    Here, you will be able to adjust the properties of the level, such as the maximum time taken to beat the level and get the score bonus, Gravity, Bgm (background music), and Background. You can play with these values: for the Bgm, you can grab an audio clip from the folder Audio/Bgm; and for the background, you can grab a sprite from Art/Bg.

  5. As soon you finish, save the scene inside the folder Levels with the name MyLevel_level.

Tip

To align the prefabs among themselves, select the Transform tool and press and hold the V key to activate the Vertex-Snapping mode.

Run & Jump comes with a custom tool that allows you to set up the order and the name of the levels and also add these to the Scenes in Build list automatically. We must use this in order of make our level usable by the video game (one of the requirements is to include the suffix _level in the name of the scene).

In the Unity editor menu, navigate to Tools | Level Packager | Show Levels Package:

Creating a new level

This will display the following in the Inspector window:

Creating a new level

Currently, there are only three levels listed, so click on the + icon to create a new item in the list. Now, add the scene you created in right column and add the string My Level in the left column. This will add your level as the fourth one.

Save the changes by clicking on the Commit Levels button.

To check the scene you created, open the scene Title inside the Scenes folder, and then click on the Play button to run the video game:

Creating a new level

Now you know the necessary amount of effort it takes to create a level for this game; so let's make this level creation process the first thing to improve.

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 $19.99/month. Cancel anytime
Banner background image