- //GenAlphaTexture.cs
- using UnityEngine;
- using UnityEditor;
- using System.IO;
- public class GenAlphaTexture
- {
- [MenuItem("GameTools/GenAlphaTexture")]
- public static void StartGenAlphaTexture()
- {
- var textures = Selection.GetFiltered<Texture2D>(SelectionMode.DeepAssets);
- foreach (var t in textures)
- {
- var path = AssetDatabase.GetAssetPath(t);
- // 如果提示图片不可读, 需要设置一下 isReadable 为 true, 操作完记得再设置为 false
- // var imp = AssetImporter.GetAtPath(path) as TextureImporter;
- // imp.isReadable = true;
- // AssetDatabase.ImportAsset(path);
- var newTexture = new Texture2D(t.width, t.height, TextureFormat.RGBA32, false);
- var colors = t.GetPixels32();
- var targetColors = newTexture.GetPixels32();
- for (int i = 0, len = colors.Length; i < len; ++i)
- {
- var c = colors[i];
- targetColors[i] = new Color32(c.a, c.a, c.a, c.a);
- }
- newTexture.SetPixels32(targetColors);
- string fname = path.Split('.')[0] + "_a.png";
- File.WriteAllBytes(fname, newTexture.EncodeToPNG());
- // imp.isReadable = false;
- // AssetDatabase.ImportAsset(path);
- AssetDatabase.Refresh();
- }
- }
- }
把上面的脚本放到 Unity 工程目录: Assets/Editor 目录中
来源: http://www.bubuko.com/infodetail-3447943.html