Writing Custom Effects (C#)
Other than using built-in effects and creating custom ones from the Inspector, you can easily program custom Effects via C#.
1. Effect Base Class
All you need to do is inherit from the base Febucci.UI.Effects.AnimationScriptableBase
class (Scripting Api).
There are also some useful classes already available for you that you can view from the API, like the βBehaviorScriptableBaseβ or βAppearanceScriptableBaseβ which are intended for each effect category, βBehaviorSineBaseβ which already handles modifiers such as βa, f, wβ and more.
2. Attributes
Since youβre creating a ScriptableObject, just like Actions, make sure to add the needed attributes as well in order to instance and serialize them:
This way youβll be able to create them from your Project Window.
By inheriting from AnimationScriptableBase
, youβll find some methods to override, a few obligatory (abstracts) and others optional (virtual) based on your needs and the upper class that youβre inheriting from, and in the docs (both Scripting API and in IDE) youβll find more examples and info about new ones as well.
3. Modifier Methods
Since effects can be affected by βModifiersβ, you can override the following methods to handle them in your animations:
These methods will be applied, in order, before the animation.
3.1 ResetContext
You can use this method to reset your animationβs variables to their initial state.
For example, in the inspector you can publicly expose a variable called baseSpeed
, but in your effect only use currentSpeed
and have that variable be currentSpeed=baseSpeed
inside this method.
3.2 SetModifierTo
You can use this method to apply a modifier to your variables.
For example this will multiply the currentSpeed
value by the s
modifier (if there is any).
ππ» If you want to create a Behavior effect that has three modifiers such as βamplitudeβ, βfrequencyβ and βwaveSizeβ, you can create a class that inherits from BehaviorSineBase
which will handle them for you.
4. Animation Methods
Here are a few methods that you can override to create your custom animations.
4.1 GetMaxDuration
The max duration of the effect. This is used to calculate the total duration of an animation and have smooth transitions, but if you have an effect that never ends you should return -1;
4.2 CanApplyEffectTo
Used to check if the effect can be applied to the current letter. For example if youβre creating an appearance effect, which only applies if a character passed time is within the effectβs durattion (but in that case you can inherit from βAppearanceScriptableBaseβ and have it handle that for you).
You can go beyond that, for example check if the character is a number or a letter, or their word index and much more.
4.3 ApplyEffect
Main method to apply an animation to a letter, called if βCanApplyEffectToβ returned true.
character.current
is reset every frame (matchingcharacter.source
), so youβre expected to modify its values in order to animate a letter.character.source
is the original placement and color of a letter, meaning that if modified youβre basically changing the letter permanently (until a new text is set). It is recommended that you only use this as a base, modifyingcharacter.current
instead.
β
Done!
Youβve completed all the steps necessary, yay! The more effects you add, the more this process will sound familiar and simpler.
Remember to create your effect ScriptableObject in the ProjectView, and add it to a database.
You can read more here: Databases
ππ» You can always take a look at the built-in effects classes and see how theyβre implemented.
Have fun applying your effects!
Last updated