NGUI图片闪光

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NGUI图片闪光相关的知识,希望对你有一定的参考价值。

先上效果

技术分享

 

上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图片闪光的主要内容,如果未能解决你的问题,请参考以下文章

关于Unity中NGUI图片精灵响应鼠标的方法

NGUI_Atlas

unity3d、ngui插件做界面图片模糊

NGUI input输入框转载见图片水印

Android 11.0 修复Camera 使用闪光灯拍照后,查看图片时详细信息为“未使用闪光灯”状态

Android 系统APK-Camera 使用闪光灯拍照后查看图片详细信息都为“未使用闪光灯”