void UpdateAlphaTextureList() { mLastAlphaTextureInfo = null; mAlphaTextureList.Clear(); if (NGUISettings.compressTextureList == null) { return; } List <TextureData> textureDataList = NGUISettings.compressTextureList.textureList; for (int i = 0; i < textureDataList.Count; i++) { TextureData data = textureDataList[i]; if (data.bAlpha && data.AlphaName != string.Empty) { bool bExist = false; foreach (AlphaTextureInfo info in mAlphaTextureList) { if (info.Name == data.AlphaName) { bExist = true; break; } } if (!bExist) { AlphaTextureInfo info = new AlphaTextureInfo(); info.Name = data.AlphaName; string alphaTexturePath = strUIPath + data.AlphaName + ".png"; Texture2D alphaTex = AssetDatabase.LoadAssetAtPath(alphaTexturePath, typeof(Texture2D)) as Texture2D; if (alphaTex == null) { continue; } info.AlphaTexture = alphaTex; info.Width = alphaTex.width; info.Height = alphaTex.height; mAlphaTextureList.Add(info); } } } }
void AddCommonAlphaTexture(string name, bool bDeleteAlphaTexture) { string textureName = strUIPath + name + ".png"; Texture2D tex = AssetDatabase.LoadAssetAtPath(textureName, typeof(Texture2D)) as Texture2D; if (tex == null) { return; } int width = tex.width; int height = tex.height; bool hasAlpha = false; Color32[] pixels = tex.GetPixels32(); Color32[] alphaBuf = new Color32[width * height]; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { int index = y * width + x; alphaBuf[index].r = pixels[index].a; alphaBuf[index].g = pixels[index].a; alphaBuf[index].b = pixels[index].a; alphaBuf[index].a = pixels[index].a; if (pixels[index].a != 255) { hasAlpha = true; } } } if (!hasAlpha) { return; } bool SameAlpha = true; int errorRange = int.Parse(NGUISettings.errorRange); if (mLastAlphaTextureInfo != null) { if (mLastAlphaTextureInfo.Width == width && mLastAlphaTextureInfo.Height == height) { for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { int index = y * width + x; int odd = (int)alphaBuf[index].r - (int)mLastAlphaTextureInfo.Buffer[index].r; if (odd < -errorRange || odd > errorRange) { SameAlpha = false; break; } } if (!SameAlpha) { break; } } } else { SameAlpha = false; } if (SameAlpha) { List <TextureData> textureList = NGUISettings.compressTextureList.textureList; for (int i = 0; i < textureList.Count; i++) { TextureData data = textureList[i]; if (data.Name == name) { if (bDeleteAlphaTexture) { DeleteAlphaTexture(data); } data.AlphaName = mLastAlphaTextureInfo.Name; data.bAlpha = true; textureList[i] = data; return; } } } } foreach (AlphaTextureInfo texture in mAlphaTextureList) { if (mLastAlphaTextureInfo != null && texture.Name == mLastAlphaTextureInfo.Name) { continue; } if (texture.Width != width || texture.Height != height) { continue; } string texturePath = strUIPath + texture.Name + ".png"; Texture2D alphaTex = AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)) as Texture2D; if (alphaTex == null) { continue; } Color32[] textureAlpha = alphaTex.GetPixels32(); bool bSame = true; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { int index = y * width + x; int odd = (int)alphaBuf[index].r - (int)textureAlpha[index].r; if (odd < -errorRange || odd > errorRange) { bSame = false; break; } } if (!bSame) { break; } } if (bSame) { alphaTex = AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)) as Texture2D; texture.AlphaTexture = alphaTex; if (mLastAlphaTextureInfo == null) { mLastAlphaTextureInfo = new LastAlphaTextureInfo(); } mLastAlphaTextureInfo.Name = texture.Name; mLastAlphaTextureInfo.Width = width; mLastAlphaTextureInfo.Height = height; mLastAlphaTextureInfo.Buffer = textureAlpha; List <TextureData> textureList = NGUISettings.compressTextureList.textureList; for (int i = 0; i < textureList.Count; i++) { TextureData data = textureList[i]; if (data.Name == name) { if (bDeleteAlphaTexture) { DeleteAlphaTexture(data); } data.AlphaName = texture.Name; data.bAlpha = true; textureList[i] = data; return; } } break; } } { mMaxID++; string alphaName = strUIPath + NGUISettings.compressTextureList.name + "/" + NGUISettings.compressTextureList.name + "_" + mMaxID.ToString() + "_Alpha.png"; if (mLastAlphaTextureInfo == null) { mLastAlphaTextureInfo = new LastAlphaTextureInfo(); } mLastAlphaTextureInfo.Name = NGUISettings.compressTextureList.name + "/" + NGUISettings.compressTextureList.name + "_" + mMaxID.ToString() + "_Alpha"; mLastAlphaTextureInfo.Width = width; mLastAlphaTextureInfo.Height = height; mLastAlphaTextureInfo.Buffer = alphaBuf; Texture2D alphaTex = new Texture2D(width, height, TextureFormat.RGBA32, false); alphaTex.SetPixels32(alphaBuf); alphaTex.Apply(); byte[] bytes = alphaTex.EncodeToPNG(); System.IO.File.WriteAllBytes(alphaName, bytes); bytes = null; AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); TextureImporter importerMain = AssetImporter.GetAtPath(textureName) as TextureImporter; TextureWrapMode wrap = importerMain.wrapMode; TextureImporter importer = AssetImporter.GetAtPath(alphaName) as TextureImporter; importer.textureType = TextureImporterType.Default; importer.mipmapEnabled = false; importer.isReadable = true; importer.textureFormat = TextureImporterFormat.ARGB32; importer.ClearPlatformTextureSettings("Android"); importer.ClearPlatformTextureSettings("iPhone"); importer.wrapMode = wrap; AssetDatabase.ImportAsset(alphaName); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); Texture2D alphatex = AssetDatabase.LoadAssetAtPath(alphaName, typeof(Texture2D)) as Texture2D; AlphaTextureInfo info = new AlphaTextureInfo(); info.Name = NGUISettings.compressTextureList.name + "/" + NGUISettings.compressTextureList.name + "_" + mMaxID.ToString() + "_Alpha"; info.Width = width; info.Height = height; info.AlphaTexture = alphatex; mAlphaTextureList.Add(info); List <TextureData> textureList = NGUISettings.compressTextureList.textureList; for (int i = 0; i < textureList.Count; i++) { TextureData data = textureList[i]; if (data.Name == name) { if (bDeleteAlphaTexture) { DeleteAlphaTexture(data); } data.AlphaName = mLastAlphaTextureInfo.Name; data.bAlpha = true; textureList[i] = data; return; } } } }
void UpdateCompressTextureList(List <TextureListInfo> list, List <Texture> textures) { int iRange; if (int.TryParse(NGUISettings.errorRange, out iRange)) { } else { EditorUtility.DisplayDialog("Range Error", "Must Number", "OK"); NGUISettings.errorRange = "0"; return; } EditorUtility.DisplayProgressBar("Update", " Update alpha texture, Please Wait...", 0); float totalNum = 0; foreach (TextureListInfo info in list) { if (info.Type != 0) { totalNum++; } } List <TextureData> textureList = NGUISettings.compressTextureList.textureList; Dictionary <string, TextureData> texturListeDic = new Dictionary <string, TextureData>(); foreach (TextureData data in textureList) { texturListeDic[data.Name] = data; } Dictionary <string, Texture> TextureDic = new Dictionary <string, Texture>(); foreach (Texture tex in textures) { string path = GetUIPathbyTexture(tex); TextureDic[path] = tex; } int num = 0; List <TextureData> UpdateList = new List <TextureData>(); foreach (TextureListInfo info in list) { if (info.Type == 0) { continue; } Texture tex = TextureDic[info.Name]; if (tex != null) { num++; if (info.Type == 1) { TextureData data = texturListeDic[info.Name]; UpdateList.Add(data); } else if (info.Type == 2) { TextureData data = new TextureData(); data.Name = info.Name; string path = GetUIPathbyTexture(tex); if (path == string.Empty) { EditorUtility.DisplayDialog("Error", "UI Texture must locate in Assets/Resources/UI", "ok"); continue; } data.Name = path; textureList.Add(data); UpdateList.Add(data); } } } for (int i = 0; i < totalNum; i++) { EditorUtility.DisplayProgressBar("Update", "Phase 1 of 5, Please Wait...", i / totalNum); TextureData d = UpdateList[i]; MakeTextureReadable(d.Name); MakeTextureReadable(d.AlphaName); } float total = mAlphaTextureList.Count; for (int i = 0; i < mAlphaTextureList.Count; i++) { EditorUtility.DisplayProgressBar("Update", "Phase 2 of 5, Please Wait...", i / total); AlphaTextureInfo info = mAlphaTextureList[i]; MakeTextureReadable(info.Name); } for (int i = 0; i < totalNum; i++) { EditorUtility.DisplayProgressBar("Update", "Phase 3 of 5, Please Wait...", i / totalNum); TextureData d = UpdateList[i]; AddCommonAlphaTexture(d.Name, true); //MakeAlphaTexture(d); } //AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); for (int i = 0; i < totalNum; i++) { EditorUtility.DisplayProgressBar("Update", "Phase 4 of 5, Please Wait...", i / totalNum); TextureData d = UpdateList[i]; UpdateTextureAsset(d.Name); } for (int i = 0; i < mAlphaTextureList.Count; i++) { EditorUtility.DisplayProgressBar("Update", "Phase 5 of 5, Please Wait...", i / total); AlphaTextureInfo info = mAlphaTextureList[i]; UpdateTextureAsset(info.Name); } //EditorUtility.DisplayProgressBar("Update", "Phase 4 of 4, Please Wait...", 0); UpdateList.Clear(); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); NGUISettings.compressTextureList.textureList = textureList; NGUITools.SetDirty(NGUISettings.compressTextureList.gameObject); AssetDatabase.SaveAssets(); EditorUtility.ClearProgressBar(); Selection.activeObject = null; }