banner



How To Call An Animation Event In Unity

AnyPortrait > Manual > Animation Events and Script

Animation Events and Script


ane.iii.four

Depending on the game, at some betoken in the animation, the graphic symbol needs some interaction.
When a running character makes a leap, it may get muddy or a swinging sword may hit a monster.
In this folio, you will learn how to utilize the animation event office to interact with the script.
(C # script included.)


In the Animation Backdrop UI, click the Animation Events button to open a dialog that registers an animation event.

Adding an Animation Event and associating them with a Script


(1) Click the Add Event button to add together a new event.
(2) Gear up the proper noun of the event and enter the frame to be called.
The proper noun of the outcome is linked with the script, so remember the proper noun at that time.
I created it hither as SampleAnimEvent .


When an blitheness consequence is added, you lot can see that an consequence marking has been added to that location in the timeline GUI.


Bake and become to the scene.
Create a script to be associated with the effect.
(1) Create a GameObject to incorporate the script. (It does non have to be a new 1; it can be anything GameObject that contains the script.)
(two) Create and add a MonoBehaviour script.

Write the following code:
Remember that the blitheness consequence y'all created above is named SampleAnimEvent .


As you can see in the code above, nosotros phone call a function with the same proper noun as the created animation event.
Therefore, animation events and scripts must have the same name and the same arguments.


When you lot select a character, the apPortrait properties are displayed in the Inspector.
To allow the script to receive events, gear up the GameObject that contains the script you only created into the Effect Listener.


When you run the game, an outcome occurs, and yous tin see in the console window that the script takes it and prints the log.

Adding a Parameter to an animation event


Yous can add a parameter to an animation event.
At this fourth dimension, the parameter is called equally an argument of the office.
(1) Press the Add together Parameter button to add the parameter.
(2) Select the parameter type and (3) enter the value to exist transferred.
(The types that can be passed as parameters are Bool, Int, Float, Vector2, String.)


Modify the function to receive the parameters.
Declare a parameter for the variable blazon.


When you run the game, you tin can encounter that the value of the parameter is passed.

Making Animation Events phone call continuously on multiple frames


Sometimes y'all need to fire an consequence continuously over a sure frame instead of raising an event in ane frame.
(1) Change the Phone call Method from In one case to Continuous.
(two) Prepare Commencement Frame and End Frame respectively.
(3) Two parameter values tin be specified.
Each value is chosen past the Start Frame and the Stop Frame. In betwixt, the value interpolated linearly is passed to the script.
(String blazon is not interpolated, and then this function is not supported.)


Continuous type animation events are represented past markers of different colors. The starting point is blueish and the ending indicate is green.


If yous run the game after bake, you volition see that the interpolated value is called every frame.

Create an animation result with two or more parameters


You can besides create blitheness events with multiple parameters.
Each parameter can have a different type.
The above screen is a screen with three parameters, Integer, Float, and Vector2 respectively.
(I create a new animation effect, named MultipleParamAnimEvent.)


Just i statement of the object array type is used equally a function parameter, not iii arguments.
This array contains the parameters in the gild you defined them in the editor.
Variables tin be referenced via type casting.


When you lot run the game, you lot will see that 3 parameters are passed to the script.

Animated Events in the improved Inspector


ane.three.4

Through the inspector (Related page) updated in AnyPortrait v1.iii.four, you lot tin quickly cheque animation events.
It is besides possible to re-create animation events into a script, or change the method they are called.


When you select a grapheme with an animated event in the Unity scene, a screen similar the one in a higher place volition appear in the inspector.
(one) The blitheness telephone call method tin can be selected from Send Message and Callback. The method used is Transport Message.
(2) A listener object to receive and process blitheness events. (The location of this item that existed at the top of the existing Inspector has been inverse.)
(3) Animation event listing. You lot can cheque the event name and parameters.
(4) Copy the animation event to the clipboard. The copied form is processed so that it can be pasted into a C# script.
(5) Copy all animation events to the clipboard.

Permit's use the added function "Copy animation events to clipboard".
First, press the copy button in (4) or (5).


If y'all open the script editor and paste it, the lawmaking that matches the names and formats of the blitheness events is added equally shown above.
You lot will at present be able to avoid the unnecessary try of memorizing and writing animation events.


Some other feature added in v1.three.4 is the Callback method.
If the existing Ship Message uses Unity Send Bulletin, this is similar to Unity Event, which specifies a office direct.
It non only improves functioning, merely also has the reward of non having to set the aforementioned name of the function to receive the event.
(Nosotros didn't actually use Unity Event, but we developed it almost similarly so that users tin can apply it like Unity Event.)
(i) Let's modify Event Method to Callback.


The original Outcome Listener disappears, and the UI is changed to exist able to assign the call targets for each event.


(1) Press the + button.
(2) An item that can set event listeners and callback functions is added.


(1) Allocate a Monobehaviour object that will receive events from the scene.
(ii) If you click the blank on the correct, a list of functions that can receive events appears. Choose the appropriate part.
If more than one Monobehaviour component is added to ane GameObject, the listener can be replaced through the role list.


(1) In the aforementioned way, you can set one event to be received by multiple listeners or multiple functions at the same time.
(ii) To delete an detail, printing the - button.


When designating a callback part, if there is no role that matches the event type (number, type, social club of parameters), nothing appears in the listing equally higher up.

Either way, you tin can make your script receive blitheness events and run.
Try the method that works for you!


Source: https://rainyrizzle.github.io/en/AdvancedManual/AD_AnimationEvents.html

Posted by: snowgiviled.blogspot.com

0 Response to "How To Call An Animation Event In Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel