Skip to main content

Scripting

Code Snippets

The namespace
using ChocDino.UIFX;
Add the MotionBlurSimple component to your GameObject
// Add the component to your GameObject and set default properties
var mb = AddComponent<MotionBlurSimple>();
mb.UpdateMode = MotionBlurSimple.Mode.Transform;
mb.SampleCount = 16;
mb.BlendStrength = 2.5f;
mb.FrameRateIndependent = true;
mb.Strength = 1f;
Resetting Motion
// Call ResetMotion() whenever you want to reset the motion blur - for example after resetting the position/transform of the object.
mb.ResetMotion();
Globals
// Global static fields useful for debugging purposes:
// (These affect all instances of MotionBlurSimple at once)

// Disable the effect - useful for low-powered systems.
MotionBlurSimple.GlobalDisabled = true;

// Add an obvious color tint to the effects - useful to make sure the effect is running.
MotionBlurSimple.GlobalDebugTint = true;

// Stop updating the motion, just display the same frame as last time. This looks like time has frozen - useful for visualising fast moving motion blur.
MotionBlurSimple.GlobalFreeze = true;

Complete Example

MotionBlurSimpleExample.cs
using UnityEngine;
using ChocDino.UIFX;

/// Demonstrates the scripting API for the MotionBlurSimple component
/// Adds the motion blur component to the gameobject and runs a simple
/// translation and rotation animation on the Transform component.

/// Press keys 1 to 4 to test API functionality.

/// NOTE: The GameObject is required to have a UI component of type Graphic
[RequireComponent(typeof(Graphic))]
public class MotionBlurSimpleExample : MonoBehaviour
{
[SerializeField] float _speed = 10f;

private MotionBlurSimple _mb;
private float _time;
private float _rotation;

void Start()
{
// Get or create the component
_mb = GetComponent<MotionBlurSimple>();
if (_mb == null)
{
_mb = AddComponent<MotionBlurSimple>();
}

// Set the properties to default values
_mb.UpdateMode = MotionBlurSimple.Mode.Transform;
_mb.SampleCount = 16;
_mb.BlendStrength = 2.5f;
_mb.Strength = 1f;
}

void Update()
{
AnimateTransform();

// Keys 1..4 demonstrate API functionality
if (Input.GetKeyDown(KeyCode.Alpha1))
{
// Toggle disabling
MotionBlurSimple.GlobalDisabled = !MotionBlurSimple.GlobalDisabled;
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
// Toggle debug color tinting
MotionBlurSimple.GlobalDebugTint = !MotionBlurSimple.GlobalDebugTint;
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
// Toggle time freezing
MotionBlurSimple.GlobalFreeze = !MotionBlurSimple.GlobalFreeze;
}
else if (Input.GetKeyDown(KeyCode.Alpha4))
{
// Restart
_time = 0f;
_rotation= 0f;
_mb.ResetMotion();
}
}

void AnimateTransform()
{
_time += Time.deltaTime * _speed;

// Animate position
float x = Mathf.Sin(_time) * Screen.width;
this.transform.localPosition = new Vector3(x, 0f, 0f);

// Animate rotation
float maxSpinSpeed = 5f;
float spinSpeed = Mathf.Sin(_time) * maxSpinSpeed;
_rotation += spinSpeed;
this.transform.localRotation = Quaternion.Euler(0f, 0f, _rotation);
}
}