IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原](二十八)unity4.6学习Ugui中文文档-------uGui Effect Tool

    u010019717发表于 2015-05-10 10:48:02
    love 0

    浏览一下 GitHub ,找了找UGUI开源的东西 https://github.com/WestHillApps/uGUI-Effect-Tool


    发现了 uGuiEffectTool (包括Blend【意义不大】 和 渐变)

    这个是原始图片


    Blend的效果:

    Blend的代码:

    using UnityEngine;
    using System.Collections.Generic;
    using UnityEngine.UI;
    
    namespace UiEffect
    {
        [AddComponentMenu ("UI/Effects/Blend Color")]
        [RequireComponent (typeof (Graphic))]
        public class BlendColor : BaseVertexEffect
        {
            public enum BLEND_MODE
            {
                Multiply,
                Additive,
                Subtractive,
                Override,
            }
    
            public BLEND_MODE blendMode = BLEND_MODE.Multiply;
            public Color color = Color.grey;
    
            Graphic graphic;
    
            public override void ModifyVertices (List vList)
            {
                if (IsActive () == false || vList == null || vList.Count == 0) {
                    return;
                }
    
                UIVertex tempVertex = vList[0];
                for (int i = 0; i < vList.Count; i++) {
                    tempVertex = vList[i];
                    byte orgAlpha = tempVertex.color.a;
                    switch (blendMode) {
                        case BLEND_MODE.Multiply:
                            tempVertex.color *= color;
                            break;
                        case BLEND_MODE.Additive:
                            tempVertex.color += color;
                            break;
                        case BLEND_MODE.Subtractive:
                            tempVertex.color -= color;
                            break;
                        case BLEND_MODE.Override:
                            tempVertex.color = color;
                            break;
                    }
                    tempVertex.color.a = orgAlpha;
                    vList[i] = tempVertex;
                }
            }
    
            /// 
            /// Refresh Blend Color on playing.
            /// 
            public void Refresh ()
            {
                if (graphic == null) {
                    graphic = GetComponent ();
                }
                if (graphic != null) {
                    graphic.SetVerticesDirty ();
                }
            }
        }
    }


    Gradient效果:

    Gradient代码:

    using UnityEngine;
    using System.Collections.Generic;
    using UnityEngine.UI;
    
    namespace UiEffect
    {
        [AddComponentMenu ("UI/Effects/Gradient Color")]
        [RequireComponent (typeof (Graphic))]
        public class GradientColor : BaseVertexEffect
        {
            public enum DIRECTION
            {
                Vertical,
                Horizontal,
                Both,
            }
    
            public DIRECTION direction = DIRECTION.Both;
            public Color colorTop = Color.white;
            public Color colorBottom = Color.black;
            public Color colorLeft = Color.red;
            public Color colorRight = Color.blue;
    
            Graphic graphic;
    
            public override void ModifyVertices (List vList)
            {
                if (IsActive () == false || vList == null || vList.Count == 0) {
                    return;
                }
    
                float topX = 0f, topY = 0f, bottomX = 0f, bottomY = 0f;
                foreach (var vertex in vList) {
                    topX = Mathf.Max (topX, vertex.position.x);
                    topY = Mathf.Max (topY, vertex.position.y);
                    bottomX = Mathf.Min (bottomX, vertex.position.x);
                    bottomY = Mathf.Min (bottomY, vertex.position.y);
                }
                float width = topX - bottomX;
                float height = topY - bottomY;
    
                UIVertex tempVertex = vList[0];
                for (int i = 0; i < vList.Count; i++) {
                    tempVertex = vList[i];
                    byte orgAlpha = tempVertex.color.a;
                    Color colorOrg = tempVertex.color;
                    Color colorV = Color.Lerp (colorBottom, colorTop, (tempVertex.position.y - bottomY) / height);
                    Color colorH = Color.Lerp (colorLeft, colorRight, (tempVertex.position.x - bottomX) / width);
                    switch (direction) {
                        case DIRECTION.Both:
                            tempVertex.color = colorOrg * colorV * colorH;
                            break;
                        case DIRECTION.Vertical:
                            tempVertex.color = colorOrg * colorV;
                            break;
                        case DIRECTION.Horizontal:
                            tempVertex.color = colorOrg * colorH;
                            break;
                    }
                    tempVertex.color.a = orgAlpha;
                    vList[i] = tempVertex;
                }
            }
    
            /// 
            /// Refresh Gradient Color on playing.
            /// 
            public void Refresh ()
            {
                if (graphic == null) {
                    graphic = GetComponent ();
                }
                if (graphic != null) {
                    graphic.SetVerticesDirty ();
                }
            }
        }
    }


    


沪ICP备19023445号-2号
友情链接