Skip to content

Commit de015ae

Browse files
committed
Add LICENSE.md and README.md from package folder
1 parent 5cd7386 commit de015ae

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 - 2019 Roman Fadeev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
[![license](https://img.shields.io/github/license/rfadeev/unity-forge-anim-callbacks.svg)](https://github.com/rfadeev/unity-forge-anim-callbacks/blob/master/LICENSE.md)
2+
3+
# Unity Forge Anim Callbacks
4+
Runtime callbacks for Unity animation clips used in Animator and Animation components.
5+
6+
## Motivation
7+
While Unity animation events provide ability to call method from specific time point of animation clip, there is no Unity API for binding such method at runtime from code. So it was decided to implement such way to add callbacks to Unity animation clips.
8+
9+
## Installation
10+
Project supports Unity Package Manager. To install project as Git package do following:
11+
1. Close Unity project and open the `Packages/manifest.json` file.
12+
2. Update `dependencies` to have `com.rfadeev.unityforge.animcallbacks` package:
13+
```json
14+
{
15+
"dependencies": {
16+
"com.rfadeev.unityforge.animcallbacks": "https://github.com/rfadeev/unity-forge-anim-callbacks.git"
17+
}
18+
}
19+
```
20+
3. Open Unity project.
21+
22+
Alternatively, add this repository as submodule under `Assets` folder or download it and put to `Assets` folder of your Unity project.
23+
24+
## Usage
25+
Import `UnityForge.AnimCallbacks` namespace to be able to use extensions for callbacks. Both Animator and Animation extension methods have same names:
26+
* `AddClipStartCallback` - to add callback for start of animation clip.
27+
* `AddClipEndCallback` - to add callback for end of animation clip.
28+
* `AddClipCallback` - to add callback for given timeline position of animation clip.
29+
* `RemoveClipStartCallback` - to remove callback for start of animation clip.
30+
* `RemoveClipEndCallback` - to remove callback for end of animation clip.
31+
* `RemoveClipCallback` - to remove callback for given timeline position of animation clip.
32+
33+
Several callbacks can be added at the same position of animation clip timeline. Callbacks are called in order they were added.
34+
Note that if using anonymous functions, you need to store delegate instance for correct removal of callback via `RemoveClip*` methods.
35+
36+
### Animator
37+
For Animator's animation clip callbacks layer index and clip name are required to add callback. To add callback at given timeline position, position parameter representing time in seconds from clip start is required.
38+
```csharp
39+
var animator = GetComponent<Animator>();
40+
var layerIndex = 0;
41+
var clipName = "AnimatorClipName";
42+
43+
animator.AddClipStartCallback(layerIndex, clipName, () =>
44+
{
45+
Debug.LogFormat("Clip \"{0}\": started", clipName);
46+
});
47+
animator.AddClipEndCallback(layerIndex, clipName, () =>
48+
{
49+
Debug.LogFormat("Clip \"{0}\": ended", clipName);
50+
});
51+
animator.AddClipCallback(layerIndex, clipName, 0.5f, () =>
52+
{
53+
Debug.LogFormat("Clip \"{0}\": callback at 0.5f seconds after start", clipName);
54+
});
55+
```
56+
57+
To remove callback same parameters are required as in complementary add callback method.
58+
```csharp
59+
private void AddExampleCallback()
60+
{
61+
animator.AddClipStartCallback(layerIndex, clipName, LogStart);
62+
}
63+
64+
private void RemoveExampleCallback()
65+
{
66+
animator.RemoveClipStartCallback(layerIndex, clipName, LogStart);
67+
}
68+
69+
private void LogStart()
70+
{
71+
Debug.LogFormat("Clip \"{0}\": started", clipName);
72+
}
73+
```
74+
75+
Find more Animator examples [here](https://github.com/rfadeev/unity-forge-anim-callbacks/tree/master/Source/Examples/Animator).
76+
77+
### Animation
78+
For Animation's animation clip callbacks clip name is required to add callback. To add callback at given timeline position, position parameter representing time in seconds from clip start is required.
79+
```csharp
80+
var animation = GetComponent<Animation>();
81+
var clipName = "AnimationClipName";
82+
83+
animation.AddClipStartCallback(clipName, () =>
84+
{
85+
Debug.LogFormat("Clip \"{0}\": started", clipName);
86+
});
87+
animation.AddClipEndCallback(clipName, () =>
88+
{
89+
Debug.LogFormat("Clip \"{0}\": ended", clipName);
90+
});
91+
animation.AddClipCallback(clipName, 0.5f, () =>
92+
{
93+
Debug.LogFormat("Clip \"{0}\": callback at 0.5f seconds after start", clipName);
94+
});
95+
```
96+
97+
To remove callback same parameters are required as in complementary add callback method.
98+
```csharp
99+
private void AddExampleCallback()
100+
{
101+
animation.AddClipStartCallback(clipName, LogStart);
102+
}
103+
104+
private void RemoveExampleCallback()
105+
{
106+
animation.RemoveClipStartCallback(clipName, LogStart);
107+
}
108+
109+
private void LogStart()
110+
{
111+
Debug.LogFormat("Clip \"{0}\": started", clipName);
112+
}
113+
```
114+
115+
Find more Animation examples [here](https://github.com/rfadeev/unity-forge-anim-callbacks/tree/master/Source/Examples/Animation).
116+
117+
## Caveats
118+
Callbacks are implemented via adding Unity animation events to the animation clip and `AnimationEventReceiver` component to the same object Animator or Animation is attached. Following should be taken into account when using callbacks:
119+
* Since Unity animation event calls all components which have method with the name from animation event, attaching component which has method named `OnTimelineEventRaised` on the same object which adds callback at runtime, can have undesired consequences since these component's method will be called.
120+
* Using `AnimationEventReceiver` directly (in editor or from user code) can result in not desired calls if callbacks are populated from user code or if `OnTimelineEventRaised` method is used in animation event added directly (in editor or from user code).
121+
* Runtime representation of `AnimationClip` is persistent while application is running. So if callback is added, but not removed, coressponding animation event will be present in animation clip even if Animation or Animator component is destroyed.
122+
* Negative animator state speed does not trigger start and end animation events hence no callbacks are called.

0 commit comments

Comments
 (0)