先上效果
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)
_Interval("Interval", Float) = 5
}
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 _Interval;
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
fixed4 color : COLOR;
};
struct v2f
{
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
half2 texcoord1 : TEXCOORD1;
fixed4 color : COLOR;
};
v2f o;
float mymod(float x, float y)
{
return x - y * floor(x/y);
}
v2f vert (appdata_t v)
{
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = v.texcoord;
o.texcoord1 = o.texcoord;
o.texcoord1.x += mymod(-_Time.w, _Interval) - 1.0f;
o.color = v.color;
return o;
}
fixed4 frag (v2f IN) : COLOR
{
float4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
float4 flashCol = tex2D(_FlashTex,IN.texcoord1)*_FlashColor;
col.rgb = 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 bailu
-------------------------------------------------------------------
bailu 2016-01-26 14:45:52
带流光的UITexture
*/
[ExecuteInEditMode]
[RequireComponent(typeof(UITexture))]
public class UIFlowTexture : MonoBehaviour {
//流光颜色
[SerializeField]
private Texture FlowTexture = null;
//流光时间间隔
[SerializeField]
private float Interval = 5f;
//流光颜色
[SerializeField]
private Color FlowColor = Color.white;
//被流光的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();
});
}
[ContextMenu("Refresh Material Property")]
public void RefreshMaterialProperty()
{
var mat = mUITexture.material;
if(null==mat)
{
return;
}
if(null!=FlowTexture)
{
mat.SetTexture("_FlashTex", FlowTexture);
}
mat.SetFloat("_Interval", Interval);
mUITexture.MarkAsChanged();
}
}
搞法:
1、创建个材质命名成UITexture_Flow.mat,把上面的shader拖拽进去。
2、创建GameObject,挂上UITexture(不用修改的材质,原因是我代码里会去修改他的材质。为什么总是new 新的材质,原因是每个UITexture都要自己流动自己的)
3、然后再挂上UITextureFlow,设置流光的参数
注意:如果你放ScrollView下发现裁剪不正确,那就按照NGUI的shader规则,增加XXX 1.shader,XXX 2.shader。