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

Integrating the Palette with the Level Creator tool


In this section, we will create an event that will be triggered every time you select a piece in the Palette and captured by the Level inspector. This feature will be used in the next chapter.

Creating an event

An event in C# is a way for a class to provide notifications when something happens to an object.

In this case, we will add an event when the user selects one of the pieces from the Palette. To achieve this, we we will add the following lines of code to the PalleteWindow.cs script:

public delegate void itemSelectedDelegate (PaletteItem item,Texture2D preview);
public static event itemSelectedDelegate ItemSelectedEvent;

The delegate type defines the signature for the method that handles the event. In this case, itemSelectedDelegate receives a PaletteItem and a Texture2D with the preview.

Tip

As a good practice, it is always recommended to check if the even is, or is not, null.

Now, it is time to invoke the event. We will do this inside the...