101

Share

GAME DEVELOPMENT

Event Trigger in Unity Part 1


The Event Trigger is a component of Unity. It receives events from the Event System and calls registered functions for each event.

Agnesh Pipaliya

June 30, 2022

What Are EventTrigger Components?

Event trigger Receives events from the EventSystem and calls registered functions for each event. The EventTrigger can be used to specify functions you wish to be called for each EventSystem event trigger. You can assign multiple events in event trigger, it can call orders how they call.

There are two ways to intercept events: You could extend EventTrigger in the script, and override the functions for the events you are interested in intercepting.

There Are Many Types Of Event In Unity They Are Listed Below.

1.1 PointerEnter

1.2 PointerDown

1.3 PointerUp

1.4 PointerExit

1.5 PointerClick

1.6 Drag

1.7 Drop

1.8 Scroll

1.9 BeginDrag

1.10 EndDrag

1.11 Initialized Potential Drag

1.12 Deselect

1.13 Select

1.14 Submit

1.1 PointerEnter

This Event is called when the mouse pointer is over the Game Object.

Demonstration

Add Image, Right-click in property window > select UI > select Image

 

 

Select Image > Add component > select Event Trigger from property window.

 

 

Now select “Add new event type”.

 

 

Now select PointerEnter event.

 

 

It will look like the below image.

 

 

Now create a script and attach to a game object it will look like the below image.

 

 

Copy below script into “TextInput” Function.

Now select an image and click on the “+” icon and add an event.

 

 

Now drag the “Text Input script” of the Hierarchy window to the pointer event.

 

 

Select onPointerClickEventTrigger() function from PointerEnter Event.

 

 

Now Run the project. When your pointer is above image the function is called.

 

 

1.2 PointerDown

This Event is called when the mouse pointer is down on the Game Object.

Demonstration

Add Image, Right click in property window > select UI > select Image.

 

 

Select Image > Add component > select Event Trigger from property window.

 

 

Now select “Add new event type”.

 

 

Now select PointerDown Enter event.

 

 

It will look like below image.

 

 

Now create a script and attach to a game object it will look like below image.

 

 

Copy below script to “TextInput” Function.

Now Select image and click on "+" icon and add event.

 

 

Now drag “Text Input script” of Hierarchy window to PointerDown event.

 

 

Select onPointerDownEventTrigger() function from Event.

 

 

Now Run the project. When your pointer is down on image the function is called.

 

 

1.3 PointerUp

This Event is called when the mouse pointer is Up from the Game Object.

Demonstration

Add Image, Right click in property window > select UI > select Image.

 

 

Select Image > Add component > select Event Trigger from property window.

 

 

Now select “Add new event type”.

 

 

Now select PointerUp event.

 

 

It will look like below image.

 

 

Now create a script and attach to a gameobject it will look like below image.

 

 

Copy below script to “TextInput” Function.

Now select image and click on “+” icon and add event.

 

 

Now drag “Text Input script” of Hierarchy window to pointerUp event.

 

 

Select onPointerUpEventTrigger() function from the Event.

 

 

Now Run the project. When your pointer is up on image the function is called.

 

 

1.4 PointerExit

This Event is called when the mouse pointer is exit from the Game Object.

Demonstration

 

 

Select Image > Add component > select Event Trigger from property window.

 

 

Now select “Add new event type”.

 

 

Now select PointerExit event.

 

 

It will look like below image.

 

 

Now create a script and attach to a gameobject it will look like below image.

 

 

Copy below script to “TextInput” Function.

Now select image and click on “+” icon and add event.

 

 

Now drag “Text Input script” of Hierarchy window to pointerExit event.

 

 

Select onPointerExitEventTrigger() function from the Event.

 

 

Now Run the project. When your pointer is exit from image when the function is called.

 

 

1.5 PointerClick

This Event is called when the mouse pointer is click on the Game Object.

Demonstration

Add Image, Right click in property window > select UI > select Image.

 

 

Select Image > Add component > select Event Trigger from property window.

 

 

Now select “Add new event type”.

 

 

Now select PointerClick event.

 

 

It will look like below image.

Now create a script and attach to a gameobject it will look like below image.

 

 

Copy below script to “TextInput” Function.

Now select image and click on “+” icon and add event.

 

 

Now drag the “Text Input script” of the Hierarchy window to the pointerClick event.

 

 

Select onPointerClickEventTrigger() function from the Event.

 

 

Now Run the project. When your pointer clicks on an image the function is called.

 

 

Conclusion: 

This is a step-by-step guide on Event Trigger which is one of the components of Unity. That will make you learn the different types of Event Trigger in Unity and how they are used. Try this for your business project. 

img

Agnesh Pipaliya

COO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Latest 13 Web Development Trends To Expect In 2022
April 11, 2022 Category : company news

Revealing Vasundhara’s New Identity

Read More
Leave a Comment