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
Conferences
Free Learning
Arrow right icon
Extending Unity with Editor Scripting
Extending Unity with Editor Scripting

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
Profile Icon Angelo R Tadres Bustamante
Arrow right icon
R$49.99 R$218.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.7 (7 Ratings)
eBook Sep 2015 268 pages 1st Edition
eBook
R$49.99 R$218.99
Paperback
R$272.99
Subscription
Free Trial
Renews at R$50p/m
Arrow left icon
Profile Icon Angelo R Tadres Bustamante
Arrow right icon
R$49.99 R$218.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.7 (7 Ratings)
eBook Sep 2015 268 pages 1st Edition
eBook
R$49.99 R$218.99
Paperback
R$272.99
Subscription
Free Trial
Renews at R$50p/m
eBook
R$49.99 R$218.99
Paperback
R$272.99
Subscription
Free Trial
Renews at R$50p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Extending Unity with Editor Scripting

Chapter 2. Using Gizmos in the Scene View

When you are working on a video game and you need to debug features, it's very helpful to have a visual representation of certain structures you are using in the code. For example, imagine that you have a set of waypoints to model the movement of a Non Playable Character (NPC) in your video game. If it is possible for you to see the waypoints, it will be easier for you to make tweaks and readjust the movement paths.

Thankfully, in Unity there's a class called Gizmos that allow us to add visual aids to the Scene View in an easy way.

Here, you will learn about the Gizmos class and how to use this to create a visual grid that will help level designers position the level piece prefabs with more control in the level.

In this chapter we will cover the following topics:

  • The OnDrawGizmos and OnDrawGizmosSelected methods
  • The DrawGizmo attribute
  • The Gizmos class API

Overview

In Unity, a gizmo is a visual aid rendered in Unity's Scene View to help us in the development process. Several components in Unity use gizmos to tell the developers where in the 3D world these are located.

Take a look at the following screenshot. The two icons, the movie camera and the light bulb, are gizmos that indicate the game object position of the camera and the point light components, respectively.

Overview

You can do the same with a specific game object if you click on the cube icon in their inspector pane:

Overview

Here you have three options to choose:

  • Use a label
  • Use a built-in icon
  • Use a custom icon made with any image located inside your project

You will see the following three results, respectively, in the Scene View:

Overview

Any of these gizmos will be attached to the game object and will persist in the scene and in any prefab containing this game object.

All these examples use the Unity editor to do the required setup, but there is an additional way in which gizmos can be created, allowing...

Creating gizmos through code

We explored how to add simple gizmos through the Unity editor. This section will cover how to properly implement the OnDrawGizmos and OnDrawGizmosSelected methods to achieve a similar but more flexible solution.

Note

All the scripts in this section are examples, and are not meant to be used in the Level Creator tool.

The OnDrawGizmos and OnDrawGizmosSelected methods

To get started, in a new project, create a script called GizmoExample.cs. We will use this as a guinea pig for our first gizmos experiments (don't worry, the script is not going to suffer too much!)

Write the following code in GizmoExample.cs:

using UnityEngine;
public class GizmoExample : MonoBehaviour {
        private void OnDrawGizmos() { 
        }
}

When you implement the OnDrawGizmos method, you can add gizmos that always drawn in the Scene View and also allows the possibility to be selected with a click.

In this case, the method is empty. However, if you come back to Unity and wait for the compiler...

Adding a structure to our levels

Open the Run & Jump project and look for the folder Scripts/Level. Inside this folder, you will find all the related to the video game. The Level class is responsible for making our levels work.

As you may have noticed, in Chapter 1, Getting Started with Editor Scripting, all the level piece prefabs are added to the scene and used by the level without a problem, but we don't have control over the size of the level or any way to guarantee that the level piece prefabs are going to be in the right position. Most important, the Level class is not aware about the level piece prefabs present on the level.

We are going to fix this situation making changes to the Level class, adding an array to save references to the level piece prefabs and define it size based in the total columns and rows supported by that array.

Visually, you are going to see the size of the level with the help of a grid made with gizmos.

As this chapter requires changes on the Level class...

Implementing the gizmo grid

When you open the script Level.cs. You will see the following code:

using UnityEngine;

namespace RunAndJump {
  public partial class Level : MonoBehaviour {

    [SerializeField]
    public int _totalTime = 60;
    [SerializeField]
    private float gravity = -30;
    [SerializeField]
    private AudioClip bgm;
    [SerializeField]
    private Sprite background;

    public int TotalTime {
      get { return _totalTime; }
      set { _totalTime = value; }
    }

    public float Gravity {
      get { return gravity; }
      set { gravity = value; }
    }

    public AudioClip Bgm {
      get { return bgm; }
      set { bgm = value; }
    }

    public Sprite Background {
      get { return background; }
      set { background = value; }
    }
   }
}

This script holds the variables that you saw in the inspector and the properties to access and change these values.

Note

As you may have noticed, making your variables public is not the only way to expose them in the...

Implementing the snap to grid behaviour

To create our first level in Chapter 1, Getting Started with Editor Scripting, we used a hot key to snap the level piece prefabs between them. Here, we will do the same, but instead of using the hot key, the level piece prefabs are going to snap to the grid automatically.

Here, we will assume that the Level game object position and rotation is always (0,0,0) and the scale is (1,1,1). Also, the 2D mode is selected by default.

Implementing the snap to grid behaviour

Later, we will work on how keep this configuration by default. Based on the grid we created, we need to implement a few things to achieve our goal:

  • A way to convert 3D coordinates to grid coordinates and vice versa
  • A way to know when these coordinates are outside the boundaries of the grid

Inside the Level class, add the following methods in the Level.cs script:

public Vector3 WorldToGridCoordinates(Vector3 point) {
  Vector3 gridPoint = new Vector3(
  (int)((point.x - transform.position.x) / GridSize) ,
  (int)((point.y - transform...

Overview


In Unity, a gizmo is a visual aid rendered in Unity's Scene View to help us in the development process. Several components in Unity use gizmos to tell the developers where in the 3D world these are located.

Take a look at the following screenshot. The two icons, the movie camera and the light bulb, are gizmos that indicate the game object position of the camera and the point light components, respectively.

You can do the same with a specific game object if you click on the cube icon in their inspector pane:

Here you have three options to choose:

  • Use a label

  • Use a built-in icon

  • Use a custom icon made with any image located inside your project

You will see the following three results, respectively, in the Scene View:

Any of these gizmos will be attached to the game object and will persist in the scene and in any prefab containing this game object.

All these examples use the Unity editor to do the required setup, but there is an additional way in which gizmos can be created, allowing greater...

Left arrow icon Right arrow icon

Description

One of Unity's most powerful features is the extensible editor it has. With editor scripting, it is possible to extend or create functionalities to make video game development easier. For a Unity developer, this is an important topic to know and understand because adapting Unity editor scripting to video games saves a great deal of time and resources. This book is designed to cover all the basic concepts of Unity editor scripting using a functional platformer video game that requires workflow improvement. You will commence with the basics of editor scripting, exploring its implementation with the help of an example project, a level editor, before moving on to the usage of visual cues for debugging with Gizmos in the scene view. Next, you will learn how to create custom inspectors and editor windows and implement custom GUI. Furthermore, you will discover how to change the look and feel of the editor using editor GUIStyles and editor GUISkins. You will then explore the usage of editor scripting in order to improve the development pipeline of a video game in Unity by designing ad hoc editor tools, customizing the way the editor imports assets, and getting control over the build creation process. Step by step, you will use and learn all the key concepts while creating and developing a pipeline for a simple platform video game. As a bonus, the final chapter will help you to understand how to share content in the Asset Store that shows the creation of custom tools as a possible new business. By the end of the book, you will easily be able to extend all the concepts to other projects.

Who is this book for?

This book is for anyone who has a basic knowledge of Unity programming using C# and wants to learn how to extend and create custom tools using Unity editor scripting to improve the development workflow and make video game development easier.

What you will learn

  • Use Gizmos to create visual aids for debugging
  • Extend the editor capabilities using custom inspectors, property and decorator drawers, editor windows, and handles
  • Save your video game data in a persistent way using scriptable objects
  • Improve the look and feel of your custom tools using GUIStyles and GUISkins
  • Configure and control the asset import pipeline
  • Improve the build creation pipeline
  • Distribute the custom tools in your team or publish them in the Asset Store

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 21, 2015
Length: 268 pages
Edition : 1st
Language : English
ISBN-13 : 9781785285660
Vendor :
Unity Technologies
Languages :
Concepts :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Sep 21, 2015
Length: 268 pages
Edition : 1st
Language : English
ISBN-13 : 9781785285660
Vendor :
Unity Technologies
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
R$50 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
R$500 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just R$25 each
Feature tick icon Exclusive print discounts
R$800 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just R$25 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total R$ 885.97
Extending Unity with Editor Scripting
R$272.99
Unity 5.x Cookbook
R$339.99
Unity 5  Game Optimization
R$272.99
Total R$ 885.97 Stars icon
Banner background image

Table of Contents

11 Chapters
1. Getting Started with Editor Scripting Chevron down icon Chevron up icon
2. Using Gizmos in the Scene View Chevron down icon Chevron up icon
3. Creating Custom Inspectors Chevron down icon Chevron up icon
4. Creating Editor Windows Chevron down icon Chevron up icon
5. Customizing the Scene View Chevron down icon Chevron up icon
6. Changing the Look and Feel of the Editor with GUI Styles and GUI Skins Chevron down icon Chevron up icon
7. Saving Data in a Persistent Way with Scriptable Objects Chevron down icon Chevron up icon
8. Controlling the Import Pipeline Using AssetPostprocessor Scripts Chevron down icon Chevron up icon
9. Improving the Build Pipeline Chevron down icon Chevron up icon
10. Distributing Your Tools Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.7
(7 Ratings)
5 star 71.4%
4 star 28.6%
3 star 0%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Matthew Magginnis Dec 07, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a great book to dive into the unity editor with. I've personally been working with Unity for over 5 years and have done a lot of editor coding myself and this book teaches you some very valuable ways to create editor tools for any project. The code is pretty clean, but not documented much if at all in some parts. This isn't a huge issue as the the author explains the code in the text to give you an understanding on what it's doing.If you are a beginner with editor scripts & C#, this might be a bit challenging for you as this book quickly gets into some of the more intermediate level scripting techniques that may be difficult to understand. For those that have some experience in both, this will be a pleasure to learn from as it covers some pretty moderate problems when creating tools for the unity editor.Overall this is a great book to learn from as well as use for reference to build editor tools in Unity. The example project that the author has you create through the book is a great way to see how creating editor tools can increase the workflow and development time for all your projects.
Amazon Verified review Amazon
joekelly Dec 06, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
One of the best Unity books I've purchased. I buy a lot of Packt books, and this one is full of great tips and tricks. If you have been using Unity for a while, and are looking to improve your editor scripting, you definitely can't go wrong here. Well laid out, great information and examples.
Amazon Verified review Amazon
GameDev Nov 21, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Amazing book.
Amazon Verified review Amazon
sean321 Nov 11, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I used this to learn how to build a custom level editor for a 2d tile-based game that I'm working on. The information pertaining to custom gizmos and editor windows is invaluable. Great source. Highly recommended.
Amazon Verified review Amazon
Guilherme Nov 27, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I would say It's “the cookbook of customization”. I read many books about Unity to improve my skills but I finally found an interesting book that I can learn different things as how to customize my own tools at the inspector and my scene view, exploring the gizmos to improve my organization, how to set up Git and other cool tricks. The examples are easy to understand and they are done with Unity 5.x with C# with code commented by the author.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.