码迷,mamicode.com
首页 > 其他好文 > 详细

NGUI图片闪光

时间:2016-01-27 14:22:40      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

先上效果

技术分享

 

上Shader

Shader "Unlit/Transparent Colored Flow Texture"
{
	Properties
	{
		_MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
		_FlashTex("Flash (RGB), Alpha (A)", 2D) = "white" {}
		_FlashColor("Flash Color", Color) = (1,1,1,0)
		_Width("Width", Float) = 0.2
        _Speed("Speed", Float) = 1.0
        _Angle("Angle", Float) = 0.1
        _TotalTime("Begin Time", Float) = 0.0
	}
	
	SubShader
	{
		LOD 200

		Tags
		{
			"Queue" = "Transparent"
			"IgnoreProjector" = "True"
			"RenderType" = "Transparent"
		}
		
		Pass
		{
			Cull Off
			Lighting Off
			ZWrite Off
			Fog { Mode Off }
			Offset -1, -1
			Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag			
			#include "UnityCG.cginc"

			sampler2D _MainTex;
			float4 _MainTex_ST;
			sampler2D _FlashTex;
			float4 _FlashColor;
			float _Width;
            float _Speed;
            float _Angle;
			float _TotalTime;
			
			struct appdata_t
			{
				float4 vertex : POSITION;
				float2 texcoord : TEXCOORD0;
				fixed4 color : COLOR;
			};
	
			struct v2f
			{
				float4 vertex : SV_POSITION;
				half2 texcoord : TEXCOORD0;
				fixed4 color : COLOR;
			};
	
			v2f o;

			v2f vert (appdata_t v)
			{
				o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
				o.texcoord = v.texcoord;
				o.color = v.color;
				return o;
			}
				
			fixed4 frag (v2f IN) : COLOR
			{
				float4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
				
                float pivot = fmod(_Time.y,_TotalTime)*_Speed+IN.texcoord.y*tan(_Angle);
                //pivot = pivot-floor(pivot);
                float diff = IN.texcoord.x-pivot;
                
                if(abs(diff)<_Width*0.5)
                {
                	float2 uv = float2((0.5+diff)/_Width,IN.texcoord.y);
                	float4 flashCol = tex2D(_FlashTex,uv)*_FlashColor;
                	col.rgb = lerp(col.rgb,flashCol.rgb,flashCol.w);
                }
                return col;
			}
			ENDCG
		}
	}

	SubShader
	{
		LOD 100

		Tags
		{
			"Queue" = "Transparent"
			"IgnoreProjector" = "True"
			"RenderType" = "Transparent"
		}
		
		Pass
		{
			Cull Off
			Lighting Off
			ZWrite Off
			Fog { Mode Off }
			Offset -1, -1
			ColorMask RGB
			Blend SrcAlpha OneMinusSrcAlpha
			ColorMaterial AmbientAndDiffuse
			
			SetTexture [_MainTex]
			{
				Combine Texture * Primary
			}
		}
	}
}

  

脚本

using UnityEngine;
using System.Collections;

/*-------------------------------------------------------------------
Copyright 2015 Minty Game LTD. All Rights Reserved.
Maintained by  blue 
-------------------------------------------------------------------
blue                2016-01-26 14:45:52
            带流光的UITexture
*/

[ExecuteInEditMode]
[RequireComponent(typeof(UITexture))]
public class UIFlowTexture : MonoBehaviour {

    //流光纹理的宽度
    [SerializeField]
    private float FlowWidth = 0.2f;

    //流光颜色
    [SerializeField]
    private Texture FlowTexture = null;

    //流光速度
    [SerializeField]
    private float FlowTime = 0.5f;

    //流光播放完之后的延迟时间
    [SerializeField]
    private float DelayTime = 1.0f;

    //流光纹理的斜度
    [SerializeField]
    private float Angle = -0.1f;

    //流光颜色
    [SerializeField]
    private Color FlowColor = Color.white;

    //是否开启流光
    [SerializeField]
    private bool Flow = true;

    //被流光的UITexture
    private UITexture mUITexture;

    void Awake()
    {
        mUITexture = gameObject.GetComponent<UITexture>();
    }

    // Use this for initialization
    void Start () 
    {
    //注意这是我们的资源加载方式,你可以改成自己的 ResourceManager.PrepareResource
<Material>(Resource.Dir.Material + "UITexture_Flow.mat", material => { mUITexture.material = new Material(material); RefreshMaterialProperty(); }); } void SetFlow(bool flag) { if(Flow!=flag) { Flow = flag; RefreshMaterialProperty(); } } [ContextMenu("Refresh Material Property")] public void RefreshMaterialProperty() { var mat = mUITexture.material; if(null==mat) { return; } if(null!=FlowTexture) { mat.SetTexture("_FlashTex", FlowTexture); } mat.SetFloat("_Width", FlowWidth); mat.SetFloat("_Speed", 1.0f/FlowTime); mat.SetFloat("_Angle", Angle); if (Flow) { mat.SetColor("_FlashColor", FlowColor); } else { mat.SetColor("_FlashColor", new Color(0, 0, 0, 0)); } if (null != mat) { mat.SetFloat("_TotalTime", FlowTime+DelayTime); } mUITexture.SetDirty(); } }

 

搞法:

1、创建个材质命名成UITexture_Flow.mat,把上面的shader拖拽进去。

2、创建GameObject,挂上UITexture(不用修改的材质,原因是我代码里会去修改他的材质。为什么总是new 新的材质,原因是每个UITexture都要自己流动自己的)

3、然后再挂上UITextureFlow,设置流光的参数

NGUI图片闪光

标签:

原文地址:http://www.cnblogs.com/mrblue/p/5163030.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!