如何AlphaPeel Alpha分离

首先将我们的脚本拉进去

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;

public class AlphaPeel
{
    [MenuItem("Tools/Texture/Alpha Peel")]
    public static void TextureAlphaPeel()
    {
        Object[] textures = GetSelectTextures();
        foreach (Texture2D texture in textures)
        {
            CreateRGBandAlphaTexture(texture);
        }
        AssetDatabase.Refresh();
    }

    private static Object[] GetSelectTextures()
    {
        return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
    }

    private static void CreateRGBandAlphaTexture(Texture2D texture)
    {
        if (texture == null)
            return;
        //带有A通道的才可以A通道剥离,此处需判断
        //todo;texture.format == TextureFormat.ARGB32....
        string texturePath = AssetDatabase.GetAssetPath(texture);
        Texture2D rgbTexture = new Texture2D(texture.width,texture.height,TextureFormat.RGB565,false);
        Texture2D alphaTexture = new Texture2D(texture.width,texture.height,TextureFormat.RGB565,false);
        for(int i=0;i<texture.width;i++)
        {
            for(int j=0;j<texture.height;j++)
            {
                Color c = texture.GetPixel(i,j);
                rgbTexture.SetPixel(i,j,new Color(c.r,c.g,c.b));
                alphaTexture.SetPixel(i,j,new Color(c.a,c.a,c.a));
            }
        }
        rgbTexture.Apply();
        alphaTexture.Apply();

        //todo: .png .jpg .psd .......
        File.WriteAllBytes(texturePath.Replace(".png","_RGB.png"),rgbTexture.EncodeToPNG());
        File.WriteAllBytes(texturePath.Replace(".png","_Alpha.png"),alphaTexture.EncodeToPNG());
    }

}

1.新建一个Editor的文件夹(这是编辑器文件夹)
2.将这个脚本拖入到Editor文件夹中,然后在菜单栏中Editor就会多一个"Tools/Texture/Alpha Peel"这种的东西
3.将图集的如何AlphaPeel Alpha分离
读写功能打开,然后Apply
4.选中图集,然后"Tools/Texture/Alpha Peel"
如何AlphaPeel Alpha分离
5.剥离之后按照如图的步骤来进行如何AlphaPeel Alpha分离
6.写shader(所有的改动都用注释标记了出来)

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Unlit/Transparent Colored"
{
	/*Properties
	{
		_MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
    }*/  //----------之前的

		Properties
	{
		_MainTex("Base (RGB)", 2D) = "black" {}
		_AlphaTex("AlphaTex", 2D) = "black" {}

	}
	
	SubShader
	{
		LOD 200

		Tags
		{
			"Queue" = "Transparent"
			"IgnoreProjector" = "True"
			"RenderType" = "Transparent"
			"DisableBatching" = "True"
		}
		
		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;
	        //声明一下_AlphaTex		
	        sampler2D _AlphaTex;

			float4 _MainTex_ST;
	
			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 = UnityObjectToClipPos(v.vertex);
				o.texcoord = v.texcoord;
				o.color = v.color;
				return o;
			}
				
			fixed4 frag (v2f IN) : SV_Target
			{
				fixed3 rgbColor = tex2D(_MainTex, IN.texcoord).rgb; //主纹理
			    fixed alphaCol = tex2D(_AlphaTex, IN.texcoord).r;  //采样
				fixed4 finalCol = fixed4(rgbColor, alphaCol);  //将rgb和alpha拼起来
				return finalCol * IN.color;
			}
			ENDCG
		}
	}

	SubShader
	{
		LOD 100

		Tags
		{
			"Queue" = "Transparent"
			"IgnoreProjector" = "True"
			"RenderType" = "Transparent"
			"DisableBatching" = "True"
		}
		
		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
			}
		}
	}
}

7.回到unity将RGB和Alpha拖到Shader材质球中如何AlphaPeel Alpha分离
8.成了OVER.当然高版本的unity自带一个Alpha分离的功能具体可以看这篇
UGUI Sprite Packer:图集自动ETC1+Alpha