Shader的代码如下,主要是将透明度为1的像素点输出为白色,其中_BeAttack表示角色被攻击的泛白状态
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Custom/BeAttackTest" {
Properties{
_MainTex("texture", 2D) = "black"{}
_Color("add color", Color) = (1,1,1,1)
//_BeAttack("BeAttack",Int)=0
}
SubShader{
Tags{ "QUEUE" = "Transparent" "IGNOREPROJECTOR" = "true" "RenderType" = "Transparent" }
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
LOD 100
Cull Off//设置双面渲染,避免角色缩放翻转时无渲染情况
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
fixed4 _MainTex_ST;
fixed4 _Color;
int _BeAttack;//对外参数表示是否被攻击了
struct vIn {
half4 vertex:POSITION;
float2 texcoord:TEXCOORD0;
fixed4 color : COLOR;
};
struct vOut {
half4 pos:SV_POSITION;
float2 uv:TEXCOORD0;
fixed4 color : COLOR;
};
vOut vert(vIn v) {
vOut o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.color = v.color;
return o;
}
fixed4 frag(vOut i) :COLOR{
fixed4 tex = tex2D(_MainTex, i.uv).rgba;
/*
if (tex.a == 1)return fixed4(1, 1, 1, 1);
else return fixed4(0, 0, 0, 0);
*/
if (_BeAttack==1) {//是否被攻击
if (tex.a == 1)return fixed4(1, 1, 1, 1);//对透明度为1的像素输出为白色
else return fixed4(0, 0, 0, 0);
}
else {
return tex;
}
}
ENDCG
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
角色被攻击的代码如下,通过设置时间参数控制泛白的持续时间:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy01Die : MonoBehaviour,IDie {
SpriteRenderer spriteRenderer;
public float whiteCoolTime;//泛白效果的持续时间长度
float whiteCoolTimer;
private void Awake()
{
spriteRenderer = GetComponent<SpriteRenderer>();
}
void Update()
{
//被攻击状态冷却,设置被攻击参数为0
if (whiteCoolTimer <= 0)
spriteRenderer.material.SetInt("_BeAttack", 0);
else
whiteCoolTimer -= Time.deltaTime;
}
public void Die()
{
Destroy(gameObject);
}
public void BeAttack()
{
//被攻击的时候设置shader的被攻击参数为1
spriteRenderer.material.SetInt("_BeAttack", 1);
whiteCoolTimer = whiteCoolTime;
}
}
public interface IDie
{
void Die();
void BeAttack();
}