Unity --- 在原目录中,将选中的Texture剥离为rgb和alpha

时间:2023-03-09 16:14:17
Unity --- 在原目录中,将选中的Texture剥离为rgb和alpha
 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using UObject = UnityEngine.Object;
using System.IO; public class lgs
{
[MenuItem("Assets/AlphaSpliter ")]
static void AlphaSpliter()
{
UObject[] objs = Selection.objects;
for (int i = ; i < objs.Length; i++)
{
Texture tex = objs[i] as Texture;
CreateRGBTexture(tex);
CreateAlphaTexture(tex as Texture2D);
}
} static void CreateAlphaTexture(Texture2D src)
{
if (null == src)
throw new ArgumentNullException("src"); //create alpha texture
var srcPixels = src.GetPixels();
var targetPixels = new Color[srcPixels.Length];
for (int i = , iMax = srcPixels.Length; i < iMax; ++i)
{
float r = srcPixels[i].a;
targetPixels[i] = new Color(r, r, r);
} Texture2D alphaTex = new Texture2D(src.width, src.height, TextureFormat.ARGB32, false);
alphaTex.SetPixels(targetPixels);
alphaTex.Apply(); //save alpha texture
string srcPath = AssetDatabase.GetAssetPath(src);
string ext = Path.GetExtension(srcPath);
string newPath = string.Format("{0}{1}{2}", srcPath.Substring(, srcPath.Length - ext.Length), "_alpha", ext);
string fullPath = GetFullPath(newPath);
var bytes = alphaTex.EncodeToPNG();
File.WriteAllBytes(fullPath, bytes); AssetDatabase.SaveAssets();
AssetDatabase.Refresh(); int size = Mathf.Max(src.width, src.height, );
Setting(newPath, size, TextureImporterFormat.ETC_RGB4, TextureImporterFormat.PVRTC_RGB4);
} static void CreateRGBTexture(Texture src)
{
if (null == src)
throw new ArgumentNullException("src"); string srcPath = AssetDatabase.GetAssetPath(src);
string ext = Path.GetExtension(srcPath);
string newPath = string.Format("{0}{1}{2}", srcPath.Substring(, srcPath.Length - ext.Length), "_rgb", ext); AssetDatabase.DeleteAsset(newPath);
AssetDatabase.CopyAsset(srcPath, newPath);
AssetDatabase.ImportAsset(newPath); int size = Mathf.Max(src.width, src.height, );
Setting(newPath, size, TextureImporterFormat.ETC_RGB4, TextureImporterFormat.PVRTC_RGB4);
} static void Setting(string assetPath, int maxSize, TextureImporterFormat androidFormat, TextureImporterFormat iosFormat)
{
var texImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
{
texImporter.npotScale = TextureImporterNPOTScale.ToNearest;
texImporter.isReadable = false;
texImporter.mipmapEnabled = false;
texImporter.alphaIsTransparency = true;
texImporter.wrapMode = TextureWrapMode.Clamp;
texImporter.filterMode = FilterMode.Bilinear;
texImporter.anisoLevel = ; //纹理的各向异性滤波水平
texImporter.SetPlatformTextureSettings("Android", maxSize, androidFormat);
texImporter.SetPlatformTextureSettings("iPhone", maxSize, iosFormat);
texImporter.SetPlatformTextureSettings("Standalone", maxSize, TextureImporterFormat.ARGB32);
} AssetDatabase.ImportAsset(assetPath);
AssetDatabase.SaveAssets();
} /// <summary>
/// asset path 转 full path
/// </summary>
public static string GetFullPath(string assetPath)
{
if (string.IsNullOrEmpty(assetPath))
return ""; string p = Application.dataPath + assetPath.Substring();
return p.Replace("\\", "/");
} /// <summary>
/// full path 转 asset path
/// </summary>
public static string GetAssetPath(string fullPath)
{
if (string.IsNullOrEmpty(fullPath))
return ""; fullPath = fullPath.Replace("\\", "/");
return fullPath.StartsWith("Assets/") ?
fullPath :
"Assets" + fullPath.Substring(Application.dataPath.Length);
}
}