unity3dText倒影效果

unity3dText倒影效果

    • 注释
    • 代码

图片: unity3dText倒影效果_第1张图片

注释

在Text中可以直接附加上,加上unity自带的shadow,描边等可以实现其他效果

代码

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System;


namespace Kola {
    [AddComponentMenu("UI/Kola/UIMirrorText")]
    [ExecuteInEditMode]

#if !UNITY_5_1

    public class UIMirrorText : BaseMeshEffect
    {
        //距离,限制范围0-30
        [Range(0, 30)]
        public float distance;

        public override void ModifyMesh(VertexHelper vh)
        {
            if (!IsActive() || vh.currentVertCount == 0)
            {
                return;
            }
            List vertexs = new List();
            vh.GetUIVertexStream(vertexs);
            UIVertex vt;
            int count = vertexs.Count;
            float miny = vertexs[0].position.y;
            float maxy = vertexs[0].position.y;
            for (int i = 1; i < count; i++)
            {
                if (vertexs[i].position.y < miny)
                {
                    miny = vertexs[i].position.y;
                }
                else if (vertexs[i].position.y > maxy)
                {
                    maxy = vertexs[i].position.y;
                }
            }
            float uiElementHeight = maxy - miny;
            float mirrorMinY = -maxy + 2 * miny - distance;
            Color32 top = GetComponent().color;
            Color32 bottom = new Color(top.r, top.g, top.b, 0);
            for (int i = 0; i < count; i++)
            {
                vt = vertexs[i];
                vertexs.Add(vt);
                Vector3 v = vt.position;
                v.y = -v.y + 2 * miny - distance;
                vt.position = v;

                //透明度效果
                vt.color = Color32.Lerp(bottom, top, (vt.position.y - mirrorMinY) / uiElementHeight);
                vertexs[i + count] = vt;
            }
            vh.Clear();
            vh.AddUIVertexTriangleStream(vertexs);
        }
#endif

#if UNITY_5_1
    public class UIEffectGradientColor : BaseVertexEffect {
        public override void ModifyVertices(List verts) {
        }
#endif
    }
}

你可能感兴趣的:(unity项目)