跳至主要内容

Scripting

Code Snippets

The namespace
using ChocDino.UIFX;
Add the BlurFilter component to your GameObject
// Add the component to your GameObject and set default properties
var blur = AddComponent<BlurFilter>();

blur.Algorithm = BlurAlgorithm.MultiBox;
blur.Downsample = Downsample.Auto;
blur.Blur = 4f;
blur.Strength = 1f;
blur.BlurAxes2D = BlurAxes2D.Default;
blur.ApplyAlphaCurve = false;
blur.AlphaCurve = new AnimationCurve(new Keyframe(0f, 1f, -1f, -1f), new Keyframe(1f, 0f, -1f, -1f)); // AlphaCurve is optional and can be null
Globally scale all BlurFilter Strength
BlurFilter.GlobalStrength = 0.5f;

Complete Example

TrailExample.cs
using UnityEngine;
using ChocDino.UIFX;

/// Demonstrates the scripting API for the BlurFilter 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 BlurExample : MonoBehaviour
{
private BlurFilter _blur;
private float _blurTarget = 0f;

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

void Update()
{
// Keys 1..5 demonstrate some API functionality
if (Input.GetKeyDown(KeyCode.Alpha1))
{
_blur.BlurAxes2D = BlurAxes2D.Default;
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
_blur.BlurAxes2D = BlurAxes2D.Horizontal;
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
_blur.BlurAxes2D = BlurAxes2D.Vertical;
}
else if (Input.GetKeyDown(KeyCode.Alpha4))
{
_blurTarget = 0f;
}
else if (Input.GetKeyDown(KeyCode.Alpha5))
{
_blurTarget = 1f;
}

// Animate blur towards target amount
if (_blur.Strength != _blurTarget)
{
_blur.Strength = Mathf.MoveTowards(_blur.Strength, _blurTarget, Time.deltaTime);
}
}
}