Skip to main content

Scripting

Code Snippets

The namespace
using ChocDino.UIFX;
Add the TrailEffect component to your GameObject
// Add the component to your GameObject and set default properties
// NOTE: Change this to TrailEffectTMP for TextMeshPro
var trail = AddComponent<TrailEffect>();

Gradient gradient = new Gradient();
{
GradientColorKey[] keys = new GradientColorKey[2];
keys[0].time = 0f;
keys[0].color = Color.white * 0.8f;
keys[1].time = 1f;
keys[1].color = Color.white * 0.8f;
GradientAlphaKey[] alpha = new GradientAlphaKey[2];
alpha[0].time = 0f;
alpha[0].alpha = 1f;
alpha[1].time = 1f;
alpha[1].alpha = 0f;
gradient.SetKeys(keys, alpha);
}
trail.Gradient = gradient;

trail.LayerCount = 16;
trail.DampingFront = trail.DampingBack= 50f;
trail.AlphaCurve = new AnimationCurve(new Keyframe(0f, 1f, -1f, -1f), new Keyframe(1f, 0f, -1f, -1f)); // AlphaCurve is optional and can be null
trail.VertexModifierSource = VertexModifierSource.Transform;
trail.GradientOffset = 0f;
trail.GradientScale = 1f;
trail.GradientOffsetSpeed = 0f;
trail.ShowTrailOnly = false;
trail.blendMode = BlendMode.Multiply;
trail.StengthMode = TrailStrengthMode.FadeLayers;
trail.Strength = 1f;
Resetting Motion
// Call ResetMotion() whenever you want to reset the trail - for example after resetting the position/transform of the object.
trail.ResetMotion();

Complete Example

TrailExample.cs
using UnityEngine;
using ChocDino.UIFX;

/// Demonstrates the scripting API for the TrailEffect component

/// Press keys 1 to 5 to test some API functionality.

/// NOTE: The GameObject is required to have a UI component of type Graphic
[RequireComponent(typeof(Graphic))]
public class TrailExample : MonoBehaviour
{
private TrailEffectBase _trail;

void Start()
{
// Get or create the component, detecting whether to use TextMeshPro or not
_trail = GetComponent<TrailEffect>();
if (_trail == null)
{
#if TMP_PRESENT
if (GetComponent<TMP_Text>() != null)
{
_trail = GetComponent<TrailEffectTMP>();
if (_trail == null)
{
_trail = AddComponent<TrailEffectTMP>();
}
}
else
#endif
{
_trail = AddComponent<TrailEffect>();
}
}
}

void Update()
{
// Keys 1..5 demonstrate some API functionality
if (Input.GetKeyDown(KeyCode.Alpha1))
{
_trail.LayerCount = 16;
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
_trail.LayerCount = 64;
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
_trail.GradientOffsetspeed = 0f;
}
else if (Input.GetKeyDown(KeyCode.Alpha4))
{
_trail.GradientOffsetspeed = 0.2f;
}
else if (Input.GetKeyDown(KeyCode.Alpha5))
{
_trail.ResetMotion();
}
}
}