Skip to content

Instantly share code, notes, and snippets.

@bbeaumont
Last active July 1, 2021 20:13
Show Gist options
  • Save bbeaumont/9537b52506fa171d062c to your computer and use it in GitHub Desktop.
Save bbeaumont/9537b52506fa171d062c to your computer and use it in GitHub Desktop.
Unity UI Bevel or Emboss effect
using System.Collections.Generic;
namespace UnityEngine.UI
{
[AddComponentMenu("UI/Effects/Bevel", 15)]
public class Bevel : Shadow
{
[SerializeField] private Color highlightColor;
protected Bevel()
{ }
public override void ModifyVertices(List<UIVertex> verts)
{
if (!IsActive())
return;
var start = 0;
var end = verts.Count;
ApplyShadow(verts, highlightColor, start, verts.Count, -effectDistance.x, effectDistance.y);
start = end;
ApplyShadow(verts, effectColor, start, verts.Count, effectDistance.x, -effectDistance.y);
}
}
}
@shukerullah
Copy link

shukerullah commented Feb 10, 2021

How to use this script?

@SrejonKhan
Copy link

I've updated the code to work in 2020.x version. Just create a class Bevel.cs and paste it. Then, add Bevel component to your UI Element.

Please note, ListPool<T> is an internal class of UnityEngine till 2021.1 version. So, if you're trying this in 2021.1 or more, please remove ListPool<T> and ObjectPool<T>.

using System.Collections.Generic;
using UnityEngine.Events;

namespace UnityEngine.UI
{
    [AddComponentMenu("UI/Effects/Bevel", 15)]
    public class Bevel : Shadow
    {
        [SerializeField] private Color highlightColor;

        protected Bevel()
        { }
        public override void ModifyMesh(VertexHelper vh)
        {
            if (!IsActive())
                return;

            var verts = ListPool<UIVertex>.Get();
            vh.GetUIVertexStream(verts);

            var start = 0;
            var end = verts.Count;
            ApplyShadow(verts, highlightColor, start, verts.Count, -effectDistance.x, effectDistance.y);

            start = end;
            ApplyShadow(verts, effectColor, start, verts.Count, effectDistance.x, -effectDistance.y);
            vh.Clear();
            vh.AddUIVertexTriangleStream(verts);
            ListPool<UIVertex>.Release(verts);
        }

    }

    // REMOVE FROM HERE IF 2021.1 or more 

    public static class ListPool<T>
    {
        // Object pool to avoid allocations.
        private static readonly ObjectPool<List<T>> s_ListPool = new ObjectPool<List<T>>(null, l => l.Clear());

        public static List<T> Get()
        {
            return s_ListPool.Get();
        }

        public static void Release(List<T> toRelease)
        {
            s_ListPool.Release(toRelease);
        }
    }

    public class ObjectPool<T> where T : new()
    {
        private readonly Stack<T> m_Stack = new Stack<T>();
        private readonly UnityAction<T> m_ActionOnGet;
        private readonly UnityAction<T> m_ActionOnRelease;

        public int countAll { get; private set; }
        public int countActive { get { return countAll - countInactive; } }
        public int countInactive { get { return m_Stack.Count; } }

        public ObjectPool(UnityAction<T> actionOnGet, UnityAction<T> actionOnRelease)
        {
            m_ActionOnGet = actionOnGet;
            m_ActionOnRelease = actionOnRelease;
        }

        public T Get()
        {
            T element;
            if (m_Stack.Count == 0)
            {
                element = new T();
                countAll++;
            }
            else
            {
                element = m_Stack.Pop();
            }
            if (m_ActionOnGet != null)
                m_ActionOnGet(element);
            return element;
        }

        public void Release(T element)
        {
            if (m_Stack.Count > 0 && ReferenceEquals(m_Stack.Peek(), element))
                Debug.LogError("Internal error. Trying to destroy object that is already released to pool.");
            if (m_ActionOnRelease != null)
                m_ActionOnRelease(element);
            m_Stack.Push(element);

        }
    }

    // STOP REMOVING
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment