1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEditor;
5 using System;
6 using UObject = UnityEngine.Object;
7 using System.IO;
8
9 public class lgs
10 {
11 [MenuItem("Assets/AlphaSpliter ")]
12 static void AlphaSpliter()
13 {
14 UObject[] objs = Selection.objects;
15 for (int i = 0; i < objs.Length; i++)
16 {
17 Texture tex = objs[i] as Texture;
18 CreateRGBTexture(tex);
19 CreateAlphaTexture(tex as Texture2D);
20 }
21 }
22
23 static void CreateAlphaTexture(Texture2D src)
24 {
25 if (null == src)
26 throw new ArgumentNullException("src");
27
28 //create alpha texture
29 var srcPixels = src.GetPixels();
30 var targetPixels = new Color[srcPixels.Length];
31 for (int i = 0, iMax = srcPixels.Length; i < iMax; ++i)
32 {
33 float r = srcPixels[i].a;
34 targetPixels[i] = new Color(r, r, r);
35 }
36
37 Texture2D alphaTex = new Texture2D(src.width, src.height, TextureFormat.ARGB32, false);
38 alphaTex.SetPixels(targetPixels);
39 alphaTex.Apply();
40
41 //save alpha texture
42 string srcPath = AssetDatabase.GetAssetPath(src);
43 string ext = Path.GetExtension(srcPath);
44 string newPath = string.Format("{0}{1}{2}", srcPath.Substring(0, srcPath.Length - ext.Length), "_alpha", ext);
45 string fullPath = GetFullPath(newPath);
46 var bytes = alphaTex.EncodeToPNG();
47 File.WriteAllBytes(fullPath, bytes);
48
49 AssetDatabase.SaveAssets();
50 AssetDatabase.Refresh();
51
52 int size = Mathf.Max(src.width, src.height, 32);
53 Setting(newPath, size, TextureImporterFormat.ETC_RGB4, TextureImporterFormat.PVRTC_RGB4);
54 }
55
56 static void CreateRGBTexture(Texture src)
57 {
58 if (null == src)
59 throw new ArgumentNullException("src");
60
61 string srcPath = AssetDatabase.GetAssetPath(src);
62 string ext = Path.GetExtension(srcPath);
63 string newPath = string.Format("{0}{1}{2}", srcPath.Substring(0, srcPath.Length - ext.Length), "_rgb", ext);
64
65
66 AssetDatabase.DeleteAsset(newPath);
67 AssetDatabase.CopyAsset(srcPath, newPath);
68 AssetDatabase.ImportAsset(newPath);
69
70 int size = Mathf.Max(src.width, src.height, 32);
71 Setting(newPath, size, TextureImporterFormat.ETC_RGB4, TextureImporterFormat.PVRTC_RGB4);
72 }
73
74 static void Setting(string assetPath, int maxSize, TextureImporterFormat androidFormat, TextureImporterFormat iosFormat)
75 {
76 var texImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
77 {
78 texImporter.npotScale = TextureImporterNPOTScale.ToNearest;
79 texImporter.isReadable = false;
80 texImporter.mipmapEnabled = false;
81 texImporter.alphaIsTransparency = true;
82 texImporter.wrapMode = TextureWrapMode.Clamp;
83 texImporter.filterMode = FilterMode.Bilinear;
84 texImporter.anisoLevel = 4; //纹理的各向异性滤波水平
85 texImporter.SetPlatformTextureSettings("Android", maxSize, androidFormat);
86 texImporter.SetPlatformTextureSettings("iPhone", maxSize, iosFormat);
87 texImporter.SetPlatformTextureSettings("Standalone", maxSize, TextureImporterFormat.ARGB32);
88 }
89
90 AssetDatabase.ImportAsset(assetPath);
91 AssetDatabase.SaveAssets();
92 }
93
94 /// <summary>
95 /// asset path 转 full path
96 /// </summary>
97 public static string GetFullPath(string assetPath)
98 {
99 if (string.IsNullOrEmpty(assetPath))
100 return "";
101
102 string p = Application.dataPath + assetPath.Substring(6);
103 return p.Replace("\\", "/");
104 }
105
106 /// <summary>
107 /// full path 转 asset path
108 /// </summary>
109 public static string GetAssetPath(string fullPath)
110 {
111 if (string.IsNullOrEmpty(fullPath))
112 return "";
113
114 fullPath = fullPath.Replace("\\", "/");
115 return fullPath.StartsWith("Assets/") ?
116 fullPath :
117 "Assets" + fullPath.Substring(Application.dataPath.Length);
118 }
119 }