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

Detecting Scene View events


Most of the interaction with Level Creator is going to be performed through the mouse. In this section, will learn how to capture events in the Scene View.

Getting the mouse position

The Event class, part of the UnityEngine namespace, allows you to handle user inputs such as key presses or mouse actions.

Let's update the method EventHandler inside the LevelInspector class:

private void EventHandler() {
    HandleUtility.AddDefaultControl(
    GUIUtility.GetControlID(FocusType.Passive));
    
    Vector3 mousePosition = Event.current.mousePosition;
    Debug.LogFormat("MousePos: {0}", mousePosition);
}

The variable Event.current has the information about the current event that's being processed in the Scene View.

From current, we are accessing the variable mousePosition to determine in which X and Y positions, relative to the Scene View coordinate system, the cursor of the mouse is found.

Save the changes and wait for Unity to compile. Now create a new level scene, select...