Skip to main content

Command Palette

Search for a command to run...

Creating of wave / ripple effect for buttons like in Material Design in Unity

Published
3 min read
Creating of wave / ripple effect for buttons like in Material Design in Unity
E

🤔 My name is Elijah, I've been working in the game development industry for more than 10 years and I love to solve various problems related to my field.

🔭 My main tool as a person working with both mobile and console games is of course Unity.

⚡I also worked with WebGL and used mostly C# or NodeJS as a server language. I would be glad to share my experience - you can always write to me in Discord (SodaBoom).

Hey, everybody. In today's short tutorial I'd like to show you how to work with the built-in Unity UI (UGUI) event system on the example of creating a wave effect when you click on an element (whether it's a button or Image doesn't matter), like in Material Design


So, let's get started!

Let's make an universal component based on MonoBehaviour and IPointerClickHandler

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

// We need to Disallow Multiple Component for Performance Issue
[DisallowMultipleComponent]
public class UIRippleEffect : MonoBehaviour, IPointerClickHandler
{
    [Header("Ripple Setup")]
    public Sprite m_EffectSprite;     // Our Ripple Sprite
    public Color RippleColor;         // Ripple Color
    public float MaxPower = .25f;     // Max Opacity of Ripple (from 0 to 1)
    public float Duration = .25f;     // Duration of Ripple effect (in sec)

    // Our Internal Parameters
    private bool m_IsInitialized = false;  // Initialization Flag
    private RectMask2D m_RectMask;         // Rect Mask for Ripple

    // Here we Check our Effect Sprite and Setup Container
    private void Awake() {
        if (m_EffectSprite == null) {
            Debug.LogWarning("Failed to add ripple graphics. Not Ripple found.");
            return;
        }

        SetupRippleContainer();
    }

    // Here we add our mask for ripple effect
    private void SetupRippleContainer() {
        m_RectMask = gameObject.AddComponent<RectMask2D>();
        m_RectMask.padding = new Vector4(5, 5, 5, 5);
        m_RectMask.softness = new Vector2Int(20, 20);
        m_IsInitialized = true;
    }

    // This is our Click event based on IPointerClickHandler for Unity Event System
    public void OnPointerClick(PointerEventData pointerEventData) {
        if(!m_IsInitialized) return;
        GameObject rippleObject = new GameObject("_ripple_");
        LayoutElement crl = rippleObject.AddComponent<LayoutElement>();
        crl.ignoreLayout = true;

        Image currentRippleImage = rippleObject.AddComponent<Image>();
        currentRippleImage.sprite = m_EffectSprite;
        currentRippleImage.transform.SetAsLastSibling();
        currentRippleImage.transform.SetPositionAndRotation(pointerEventData.position, Quaternion.identity);
        currentRippleImage.transform.SetParent(transform);
        currentRippleImage.color = new Color(RippleColor.r, RippleColor.g, RippleColor.b, 0f);
        currentRippleImage.raycastTarget = false;
        StartCoroutine(AnimateRipple(rippleObject.GetComponent<RectTransform>(), currentRippleImage, () => {
            currentRippleImage = null;
            Destroy(rippleObject);
            StopCoroutine(nameof(AnimateRipple));
        }));
    }

    // Here we work with animation of single ripple
    private IEnumerator AnimateRipple(RectTransform rippleTransform, Image rippleImage, Action onComplete) {
        Vector2 initialSize = Vector2.zero;
        Vector2 targetSize = new Vector2(150,150);
        Color initialColor = new Color(RippleColor.r, RippleColor.g, RippleColor.b, MaxPower);
        Color targetColor = new Color(RippleColor.r, RippleColor.g, RippleColor.b, 0f);
        float elapsedTime = 0f;

        while (elapsedTime < Duration)
        {
            elapsedTime += Time.deltaTime;
            rippleTransform.sizeDelta = Vector2.Lerp(initialSize, targetSize, elapsedTime / Duration);
            rippleImage.color = Color.Lerp(initialColor, targetColor, elapsedTime / Duration);
            yield return null;
        }

        onComplete?.Invoke();
    }
}

So, using standard Unity interfaces, we created a wave effect inside the mask created on our element (this can also be replaced with a shader-based effect for better performance) when clicked. It doesn't matter what type of element our UI will be - the main thing is that we can catch it with Raycast.

Do not forgot to setup your new component at UI:

You can practice more by adding new effects using hover/unhover and other UIs for that. Use the IPointerEnterHandler, IPointerExitHandler interfaces to do this.

Thanks for reading the article, I'll always be happy to discuss any projects with you and help you with your ideas on Unity:

My Discord | My GitHub | My Blog | Buy me a Beer

Z
zgd1y ago

We're looking for experienced MMO mobile game developers who have successfully developed multiple large-scale multiplayer online role-playing games (MMORPGs) at renowned gaming companies. This is a remote position offering highly competitive salaries with flexible working hours. Candidates can arrange their own schedules, and we're willing to provide an advance deposit upfront. If you meet these criteria and are interested, please contact us directly.

EMail:q791864008q@gmail.com Discord:q791864008q_gmail_com Telegram:q791864008q_gmail_com

Z
zgd1y ago

We are willing to pay a high price to acquire the source code of a mobile game that is compatible with both Android and iOS platforms, for the purpose of learning and research. The game must meet the following criteria: it should be developed by a top-tier gaming company within the past 5 years, fully completed and matured, and preferably of the multiplayer online MMORPG or ARPG genre. If your game meets our interest, we will proceed with the transaction promptly and offer an upfront payment as a gesture of goodwill. Contact me q791864008q@gmail.com

Z
zgd1y ago

Who has the source code of a large multiplayer online mobile game that was completed within the last 5 years? This game must possess a carefully designed commercialization numerical system. I am willing to pay a high fee to purchase it for learning and research purposes. email:q791864008q@gmail.com. If the game meets our expectations, we are willing to pay an additional recognition fee in advance.

Text Tutorials

Part 11 of 17

Detailed text-based tutorials for learning various aspects of game development on Unity. Basic and professional articles on the most important topics that no one else will tell you.

Up next

Create stylish and modern tutorials in Unity games using video tips in Pop-Up windows

Simple Video-Based Tutorials for your Unity Game