void OnPreprocessTexture() { // Automatically convert any texture file with "GUIImages" in its file name into an uncompressed unchanged GUI Image. if (assetPath.Contains("UI_Images") || assetPath.Contains("SpriteFonts") || assetPath.Contains("SpriteAtlases")) { Debug.Log("Importing new GUI Image!"); TextureImporter myTextureImporter = (TextureImporter)assetImporter; myTextureImporter.textureType = TextureImporterType.Default; myTextureImporter.textureFormat = TextureImporterFormat.ARGB32; myTextureImporter.convertToNormalmap = false; myTextureImporter.maxTextureSize = 2048; myTextureImporter.grayscaleToAlpha = false; myTextureImporter.generateCubemap = TextureImporterGenerateCubemap.None; myTextureImporter.npotScale = TextureImporterNPOTScale.None; myTextureImporter.isReadable = true; myTextureImporter.mipmapEnabled = false; // myTextureImporter.borderMipmap = false; // myTextureImporter.correctGamma = false; myTextureImporter.mipmapFilter = TextureImporterMipFilter.BoxFilter; myTextureImporter.fadeout = false; // myTextureImporter.mipmapFadeDistanceStart; // myTextureImporter.mipmapFadeDistanceEnd; myTextureImporter.convertToNormalmap = false; // myTextureImporter.normalmap; // myTextureImporter.normalmapFilter; // myTextureImporter.heightmapScale; myTextureImporter.lightmap = false; myTextureImporter.ClearPlatformTextureSettings("Web"); myTextureImporter.ClearPlatformTextureSettings("Standalone"); myTextureImporter.ClearPlatformTextureSettings("iPhone"); } }
public void ApplyConfig(AssetImporter assetImporter) { TextureImporter ti = (TextureImporter)assetImporter; ti.textureType = textureType; ti.spriteImportMode = spriteImportMode; ti.spritePackingTag = spritePackingTag; ti.spritePixelsPerUnit = spritePixelsPerUnit; ti.mipmapEnabled = mipmapEnabled; ti.filterMode = filterMode; ti.maxTextureSize = maxTextureSize; ti.textureFormat = textureFormat; ti.compressionQuality = compressionQuality; if (std_override) { ti.SetPlatformTextureSettings("Standalone", std_maxTextureSize, std_textureFormat, std_compressionQuality, false); } else { ti.ClearPlatformTextureSettings("Standalone"); } if (ios_override) { ti.SetPlatformTextureSettings("iPhone", ios_maxTextureSize, ios_textureFormat, ios_compressionQuality, false); } else { ti.ClearPlatformTextureSettings("iPhone"); } if (and_override) { ti.SetAllowsAlphaSplitting(allowsAlphaSplit); ti.SetPlatformTextureSettings("Android", and_maxTextureSize, and_textureFormat, and_compressionQuality, allowsAlphaSplit); } else { ti.ClearPlatformTextureSettings("Android"); ti.SetAllowsAlphaSplitting(false); } }
static void SetTextureReadable(string relativeAssetPath) { TextureImporter ti = (TextureImporter)TextureImporter.GetAtPath(relativeAssetPath); ti.isReadable = true; currentFormat = ti.textureFormat; ti.textureFormat = TextureImporterFormat.AutomaticTruecolor; ti.ClearPlatformTextureSettings("Android"); ti.ClearPlatformTextureSettings("iPhone"); AssetDatabase.ImportAsset(relativeAssetPath); }
static void ResetTexture(Texture texture) { if (texture == null) { return; } string assetPath = AssetDatabase.GetAssetPath(texture); TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; textureImporter.ClearPlatformTextureSettings("Android"); textureImporter.ClearPlatformTextureSettings("iPhone"); textureImporter.SaveAndReimport(); }
void OnPostprocessTexture(Texture2D texture) { if (assetPath.Contains(mEnchantPath)) { TextureImporter textureImporter = (TextureImporter)assetImporter; textureImporter.maxTextureSize = texture.width; textureImporter.textureFormat = TextureImporterFormat.ETC_RGB4; textureImporter.ClearPlatformTextureSettings("Android"); textureImporter.ClearPlatformTextureSettings("iPhone"); textureImporter.SetPlatformTextureSettings("Android", texture.width, TextureImporterFormat.ETC_RGB4, 50); textureImporter.SetPlatformTextureSettings("iPhone", texture.width, TextureImporterFormat.PVRTC_RGB4, 50); } }
void OnPreprocessTexture() { if (assetPath.Contains("CitizenParts") || assetPath.Contains("Graphics") && !assetPath.Contains("Planet") && !assetPath.Contains("NonChangeable")) { Debug.Log("Importing new sprite!"); TextureImporter myTextureImporter = (TextureImporter)assetImporter; myTextureImporter.textureType = TextureImporterType.Sprite; myTextureImporter.textureShape = TextureImporterShape.Texture2D; myTextureImporter.spriteImportMode = SpriteImportMode.Single; myTextureImporter.spritePixelsPerUnit = 1000; myTextureImporter.ClearPlatformTextureSettings("Web"); myTextureImporter.ClearPlatformTextureSettings("Standalone"); myTextureImporter.ClearPlatformTextureSettings("iPhone"); } }
void RevertTexture(TextureData data) { string textureName = strUIPath + data.Name + ".png"; string alphaName = strUIPath + data.Name + "_Alpha.png"; DeleteAlphaTexture(data); data.AlphaName = ""; TextureImporter importerMain = AssetImporter.GetAtPath(textureName) as TextureImporter; importerMain.textureType = TextureImporterType.Default; importerMain.mipmapEnabled = false; importerMain.ClearPlatformTextureSettings("Android"); importerMain.ClearPlatformTextureSettings("iPhone"); AssetDatabase.ImportAsset(textureName); }
void MakeTextureReadable(string name) { string textureName = strUIPath + name + ".png"; TextureImporter ti = AssetImporter.GetAtPath(textureName) as TextureImporter; if (ti == null) { return; } ti.isReadable = true; ti.textureFormat = TextureImporterFormat.ARGB32; ti.ClearPlatformTextureSettings("Android"); ti.ClearPlatformTextureSettings("iPhone"); AssetDatabase.ImportAsset(textureName); }
private static void _SetTrueColorFormat(TextureImporter textureImporter) { textureImporter.ClearPlatformTextureSettings(Android); textureImporter.ClearPlatformTextureSettings(iPhone); TextureImporterPlatformSettings platformTextureSettings; platformTextureSettings = textureImporter.GetDefaultPlatformTextureSettings(); platformTextureSettings.format = TextureImporterFormat.RGBA32; platformTextureSettings.overridden = true; textureImporter.SetPlatformTextureSettings(platformTextureSettings); textureImporter.alphaSource = TextureImporterAlphaSource.FromInput; textureImporter.maxTextureSize = 2048; textureImporter.generateCubemap = TextureImporterGenerateCubemap.None; textureImporter.npotScale = TextureImporterNPOTScale.None; textureImporter.mipmapEnabled = false; }
void OnPostprocessTexture(Texture2D texture) { if (assetPath.Contains("Assets/Art_new/UI/")) { //UI 贴图处理; if (assetPath.Contains("Assets/Art_new/UI/texture/ui_texture/") || assetPath.Contains("Assets/Art_new/UI/texture/icon_texture/")) { TextureImporter textureImporter = (TextureImporter)assetImporter; textureImporter.maxTextureSize = texture.width; textureImporter.textureFormat = TextureImporterFormat.ETC_RGB4; textureImporter.ClearPlatformTextureSettings("Android"); textureImporter.ClearPlatformTextureSettings("iPhone"); textureImporter.SetPlatformTextureSettings("Android", texture.width, TextureImporterFormat.ETC_RGB4, 50); textureImporter.SetPlatformTextureSettings("iPhone", texture.width, TextureImporterFormat.PVRTC_RGB4, 50); } } }
void OnPreprocessTexture() { // Automatically convert any texture file (put in folders when Editor run) with "UI_Image" // in its file name into an uncompressed unchanged GUI Image. // All setting taked from example from Lesson // If file contain name corresponding foler if (assetPath.Contains("UI_Images")) { //Debug.Log("Importing new GUI Image!"); Replaced by OnPostprocessTexture Debug.Log() TextureImporter myTextureImporter = (TextureImporter)assetImporter; myTextureImporter.textureType = TextureImporterType.GUI; myTextureImporter.textureCompression = TextureImporterCompression.Uncompressed; // Some parameters for TextureImporte set through TextureImporterPlatformSettings now TextureImporterPlatformSettings platformSettings = new TextureImporterPlatformSettings { // Set special setting for PC, Mac & Unix name = "Standalone", overridden = true, maxTextureSize = 2048, format = TextureImporterFormat.RGBA32 }; myTextureImporter.SetPlatformTextureSettings(platformSettings); //myTextureImporter.textureFormat = TextureImporterFormat.ARGB32; obsolete //myTextureImporter.maxTextureSize = 2048; move to platformSettings myTextureImporter.convertToNormalmap = false; myTextureImporter.alphaSource = TextureImporterAlphaSource.FromInput; //myTextureImporter.generateCubemap = TextureImporterGenerateCubemap.None; obsolete myTextureImporter.textureShape = TextureImporterShape.Texture2D; myTextureImporter.npotScale = TextureImporterNPOTScale.None; myTextureImporter.isReadable = true; myTextureImporter.mipmapEnabled = false; myTextureImporter.mipmapFilter = TextureImporterMipFilter.BoxFilter; myTextureImporter.fadeout = false; myTextureImporter.convertToNormalmap = false; //myTextureImporter.lightmap = false; obsolete //myTextureImporter.ClearPlatformTextureSettings("Web"); doesn't touched in this scrpit // Return to default setting for PC, Mac & Unix myTextureImporter.ClearPlatformTextureSettings("Standalone"); //myTextureImporter.ClearPlatformTextureSettings("iPhone"); doesn't touched in this scrpit } }
private static void ClearOverrides(MenuCommand command) { //Grab current Texture Importer TextureImporter currentTexture = command.context as TextureImporter; //Clear overrides from current texture foreach (string tempString in availablePlatforms) { #if UNITY_4_5 || UNITY_4_6 || UNITY_5_0 || UNITY_5_1 currentTexture.SetPlatformTextureSettings(tempString, 1024, TextureImporterFormat.AutomaticCompressed, 0); #else currentTexture.SetPlatformTextureSettings(tempString, 1024, TextureImporterFormat.AutomaticCompressed, 0, true); #endif currentTexture.ClearPlatformTextureSettings(tempString); } //Refresh asset/apply settings AssetDatabase.ImportAsset(currentTexture.assetPath, ImportAssetOptions.ForceUpdate); }
private static void ClearOverrides(MenuCommand command) { //Grab current Texture Importer TextureImporter currentTexture = command.context as TextureImporter; //Clear overrides from current texture foreach (string tempString in availablePlatforms) { #if UNITY_4_5 || UNITY_4_6 || UNITY_5_0 || UNITY_5_1 currentTexture.SetPlatformTextureSettings(tempString, 1024, TextureImporterFormat.AutomaticCompressed, 0); #else TextureImporterPlatformSettings textureImporterPFsettings = new TextureImporterPlatformSettings(); textureImporterPFsettings.maxTextureSize = 1024; textureImporterPFsettings.format = TextureImporterFormat.Automatic; textureImporterPFsettings.compressionQuality = 0; textureImporterPFsettings.allowsAlphaSplitting = true; currentTexture.SetPlatformTextureSettings(textureImporterPFsettings); #endif currentTexture.ClearPlatformTextureSettings(tempString); } //Refresh asset/apply settings AssetDatabase.ImportAsset(currentTexture.assetPath, ImportAssetOptions.ForceUpdate); }
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; } } } }
/// <summary> /// Attempt to apply the given settings to the asset found at the path /// </summary> /// <param name="assetPath">Path to the asset</param> /// <param name="importSettings">Settings to apply</param> public static void ApplyImportSettingsToAsset(string assetPath, ImportSettings importSettings) { if (importSettings != null) { AssetImporter importer = AssetImporter.GetAtPath(assetPath); #region Apply Texture Import Settings if (importer.GetType() == typeof(TextureImporter) && importSettings.UseTextureSettings == true) { TextureImporter textureImporter = (TextureImporter)importer; //User wants to override the Android texture settings, create the appropriate settings and override them for Android if (importSettings.OverrideAndroidTextureSettings == true) { TextureImporterPlatformSettings androidTextureSettings = new TextureImporterPlatformSettings() { name = "Android", maxTextureSize = (int)importSettings.AndroidMaxTextureSize, overridden = true }; textureImporter.SetPlatformTextureSettings(androidTextureSettings); } else { //Make sure to stop overriding the Android settings, if they're disabled textureImporter.ClearPlatformTextureSettings("Android"); } textureImporter.filterMode = importSettings.TetxureFilterMode; textureImporter.maxTextureSize = (int)importSettings.MaxTextureSize; textureImporter.anisoLevel = importSettings.FilterLevel; s_validAssetCount++; } #endregion #region Apply Audio Import Settings if (importer.GetType() == typeof(AudioImporter) && importSettings.UseAudioSettings == true) { AudioImporter audioImporter = (AudioImporter)importer; AudioImporterSampleSettings audioImportSettings = new AudioImporterSampleSettings() { sampleRateSetting = importSettings.AudioSampleRate, sampleRateOverride = (uint)importSettings.SampleRateOverride, compressionFormat = importSettings.CompressionFormat, loadType = importSettings.AudioLoadType }; //User wants to override the Android audio settings, create the appropriate settings and override them for Android if (importSettings.OverrideAndroidAudioSettings == true) { AudioImporterSampleSettings androidAudioImportSettings = new AudioImporterSampleSettings() { sampleRateSetting = importSettings.AndroidAudioSampleRate, sampleRateOverride = (uint)importSettings.AndroidSampleRateOverride, compressionFormat = importSettings.AndroidCompressionFormat, loadType = importSettings.AndroidAudioClipLoadType }; audioImporter.SetOverrideSampleSettings("Android", androidAudioImportSettings); } else { //Make sure to stop overriding the Android settings, if they're disabled audioImporter.ClearSampleSettingOverride("Android"); } audioImporter.defaultSampleSettings = audioImportSettings; s_validAssetCount++; } #endregion AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); } else { Debug.Log($"No import settings found for asset { assetPath } "); } }
private static void PasteScaledSettings(MenuCommand command, int scale) { //Grab current Texture Importer TextureImporter currentTexture = command.context as TextureImporter; // change names to match current texture // String newName = GetFileName(currentTexture); // clipboard.spriteData = UpdateSpriteNames(newName, clipboard); // scale clipboard.spriteData = ScaleSprites(clipboard, scale); //Copy over platform specific settings switch (clipboard.copyType) { //*************************************** case "AllData": //Copy over sprites currentTexture.spritesheet = clipboard.spriteData.ToArray(); //Copy over settings currentTexture.SetTextureSettings(clipboard.spriteSettings); //Copy over overrides foreach (string tempString in availablePlatforms) { #if UNITY_4_5 || UNITY_4_6 || UNITY_5_0 || UNITY_5_1 currentTexture.SetPlatformTextureSettings(tempString, 1024, TextureImporterFormat.AutomaticCompressed, 0); #else currentTexture.SetPlatformTextureSettings(tempString, 1024, TextureImporterFormat.AutomaticCompressed, 0, true); #endif currentTexture.ClearPlatformTextureSettings(tempString); } foreach (PlatformTextureSettings tempPlatSettings in platformTextureSettings) { #if UNITY_4_5 || UNITY_4_6 || UNITY_5_0 || UNITY_5_1 || UNITY_5_0 currentTexture.SetPlatformTextureSettings(tempPlatSettings.spritePlatform, tempPlatSettings.spriteMaxTextureSize, tempPlatSettings.spriteTextureFormat, tempPlatSettings.spriteCompressionQuality); #else currentTexture.SetPlatformTextureSettings(tempPlatSettings.spritePlatform, tempPlatSettings.spriteMaxTextureSize, tempPlatSettings.spriteTextureFormat, tempPlatSettings.spriteCompressionQuality, true); #endif } break; //*************************************** case "AllDataNoOverride": //Copy over sprites currentTexture.spritesheet = clipboard.spriteData.ToArray(); //Copy over settings currentTexture.SetTextureSettings(clipboard.spriteSettings); break; //*************************************** case "OnlyOverride": //Copy over overrides foreach (string tempString in availablePlatforms) { #if UNITY_4_5 || UNITY_4_6 || UNITY_5_0 || UNITY_5_1 currentTexture.SetPlatformTextureSettings(tempString, 1024, TextureImporterFormat.AutomaticCompressed, 0); #else currentTexture.SetPlatformTextureSettings(tempString, 1024, TextureImporterFormat.AutomaticCompressed, 0, true); #endif currentTexture.ClearPlatformTextureSettings(tempString); } foreach (PlatformTextureSettings tempPlatSettings in platformTextureSettings) { #if UNITY_4_5 || UNITY_4_6 || UNITY_5_0 || UNITY_5_1 currentTexture.SetPlatformTextureSettings(tempPlatSettings.spritePlatform, tempPlatSettings.spriteMaxTextureSize, tempPlatSettings.spriteTextureFormat, tempPlatSettings.spriteCompressionQuality); #else currentTexture.SetPlatformTextureSettings(tempPlatSettings.spritePlatform, tempPlatSettings.spriteMaxTextureSize, tempPlatSettings.spriteTextureFormat, tempPlatSettings.spriteCompressionQuality, true); #endif } break; } //Refresh asset/apply settings AssetDatabase.ImportAsset(currentTexture.assetPath, ImportAssetOptions.ForceUpdate); }
void OnPreprocessTexture() { if (assetPath.Contains("Res/Atlas")) { string atlasName = new DirectoryInfo(Path.GetDirectoryName(assetPath)).Name; TextureImporter textureImporter = assetImporter as TextureImporter; //string assetName = assetPath.Substring(assetPath.LastIndexOf("/") + 1); //assetName = assetName.Replace(".png", string.Empty); if (atlasName == "Atlas") { //if (assetPath.Contains("always") || assetPath.EndsWith("_a.png")) //{ textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spritePackingTag = string.Empty; textureImporter.mipmapEnabled = false; textureImporter.spriteImportMode = SpriteImportMode.Multiple; textureImporter.filterMode = FilterMode.Bilinear; textureImporter.maxTextureSize = 2048; textureImporter.isReadable = false; if (assetPath.Contains("main_ui") || assetPath.Contains("introduction_1") || assetPath.Contains("introduction_2") || assetPath.Contains("introduction_3") || assetPath.Contains("introduction_4") || assetPath.Contains("introduction_5")) { textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor; textureImporter.ClearPlatformTextureSettings("iPhone"); textureImporter.ClearPlatformTextureSettings("Android"); textureImporter.ClearPlatformTextureSettings("Standalone"); textureImporter.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.RGBA32, 100, false); textureImporter.SetPlatformTextureSettings("iPhone", 2048, TextureImporterFormat.AutomaticTruecolor, 100, false); } else { textureImporter.ClearPlatformTextureSettings("iPhone"); textureImporter.ClearPlatformTextureSettings("Android"); textureImporter.ClearPlatformTextureSettings("Standalone"); textureImporter.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.ETC_RGB4, 100, false); textureImporter.SetPlatformTextureSettings("iPhone", 2048, TextureImporterFormat.PVRTC_RGB4, 100, false); } //} //else //{ // Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath); // Color newColor = new Color(0f, 0f, 0f, 1f); // bool hasAlpha = false; // for (int i = 0; i < texture.width; ++i) // { // for (int j = 0; j < texture.height; ++j) // { // newColor = texture.GetPixel(i, j); // if (newColor.a <= 0) // { // Debugger.Log(newColor.a.ToString()); // hasAlpha = true; // break; // } // } // if (hasAlpha) // break; // } // if (hasAlpha) // return; // textureImporter.textureType = TextureImporterType.Sprite; // textureImporter.spritePackingTag = string.Empty; // textureImporter.mipmapEnabled = false; // textureImporter.spriteImportMode = SpriteImportMode.Multiple; // textureImporter.filterMode = FilterMode.Bilinear; // textureImporter.maxTextureSize = 2048; // textureImporter.isReadable = false; // //textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor; // textureImporter.ClearPlatformTextureSettings("iPhone"); // textureImporter.ClearPlatformTextureSettings("Android"); // textureImporter.ClearPlatformTextureSettings("Standalone"); // textureImporter.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.ETC_RGB4, 100, false); // textureImporter.SetPlatformTextureSettings("iPhone", 2048, TextureImporterFormat.PVRTC_RGB4, 100, false); //} } else { if (assetPath.Contains("Res/Atlas/map")) { /*if (assetPath.ToArray('_').Length > 2) * { * textureImporter.spritePackingTag = atlasName; * textureImporter.spriteImportMode = SpriteImportMode.Single; * } * else * {*/ textureImporter.spritePackingTag = string.Empty; textureImporter.spriteImportMode = SpriteImportMode.Multiple; //} textureImporter.textureType = TextureImporterType.Sprite; textureImporter.mipmapEnabled = false; textureImporter.filterMode = FilterMode.Bilinear; textureImporter.maxTextureSize = 2048; //textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor; textureImporter.isReadable = false; textureImporter.ClearPlatformTextureSettings("iPhone"); textureImporter.ClearPlatformTextureSettings("Android"); textureImporter.ClearPlatformTextureSettings("Standalone"); textureImporter.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.ETC_RGB4, 100, false); textureImporter.SetPlatformTextureSettings("iPhone", 2048, TextureImporterFormat.PVRTC_RGB4, 100, false); } else { //Debugger.Log(assetPath); //string AtlasName = new DirectoryInfo(Path.GetDirectoryName(assetPath)).Name; //TextureImporter textureImporter = (TextureImporter)assetImporter; textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spritePackingTag = atlasName; textureImporter.mipmapEnabled = false; textureImporter.spriteImportMode = SpriteImportMode.Single; textureImporter.filterMode = FilterMode.Bilinear; textureImporter.maxTextureSize = 2048; textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor; } } } else if (assetPath.Contains("Res/Model") || assetPath.Contains("Res/Resources/texture")) { //if (assetPath.Contains("shadow")) // return; //Debugger.Log(assetPath); if (Application.isPlaying) { return; } TextureImporter textureImporter = (TextureImporter)assetImporter; textureImporter.textureType = TextureImporterType.Image; textureImporter.grayscaleToAlpha = false; textureImporter.wrapMode = TextureWrapMode.Clamp; textureImporter.filterMode = FilterMode.Bilinear; textureImporter.mipmapEnabled = false; textureImporter.anisoLevel = 0; textureImporter.ClearPlatformTextureSettings("iPhone"); textureImporter.ClearPlatformTextureSettings("Android"); textureImporter.ClearPlatformTextureSettings("Standalone"); //Debugger.Log(assetPath); if (assetPath.EndsWith("shadow.tga") || assetPath.EndsWith("shadow.png")) { textureImporter.SetPlatformTextureSettings("Android", 32, TextureImporterFormat.RGBA16); textureImporter.SetPlatformTextureSettings("iPhone", 32, TextureImporterFormat.PVRTC_RGBA4, 100, false); } //else if (assetPath.EndsWith("_alpha.tga") || assetPath.EndsWith("_alpha.png")) //{ // textureImporter.SetPlatformTextureSettings("Android", 256, TextureImporterFormat.RGBA32); // textureImporter.SetPlatformTextureSettings("iPhone", 256, TextureImporterFormat.PVRTC_RGBA4, 100, false); //} //else if (assetPath.EndsWith("_alpha_512.tga") || assetPath.EndsWith("_alpha_512.png")) //{ // textureImporter.SetPlatformTextureSettings("Android", 512, TextureImporterFormat.RGBA32); // textureImporter.SetPlatformTextureSettings("iPhone", 512, TextureImporterFormat.PVRTC_RGBA4, 100, false); //} else if (assetPath.EndsWith("_512.tga") || assetPath.EndsWith("_512.png")) { textureImporter.SetPlatformTextureSettings("Android", 512, TextureImporterFormat.ETC_RGB4, 100, false); textureImporter.SetPlatformTextureSettings("iPhone", 512, TextureImporterFormat.PVRTC_RGB4, 100, false); } //else if (assetPath.EndsWith("_refl.tga")) //{ // textureImporter.SetPlatformTextureSettings("Android", 256, TextureImporterFormat.RGBA32, 100, false); // textureImporter.SetPlatformTextureSettings("iPhone", 256, TextureImporterFormat.PVRTC_RGBA4, 100, false); //} //else if (assetPath.EndsWith("reflection.tga") || assetPath.EndsWith("reflection.png")) //{ // textureImporter.SetPlatformTextureSettings("Android", 256, TextureImporterFormat.ETC_RGB4, 100, false); // textureImporter.SetPlatformTextureSettings("iPhone", 256, TextureImporterFormat.PVRTC_RGB4, 100, false); //} else if (assetPath.EndsWith("_info.tga") || assetPath.EndsWith("_info.png")) { textureImporter.SetPlatformTextureSettings("Android", 128, TextureImporterFormat.RGB16); textureImporter.SetPlatformTextureSettings("iPhone", 128, TextureImporterFormat.RGB16); } else { textureImporter.SetPlatformTextureSettings("Android", 256, TextureImporterFormat.ETC_RGB4, 100, false); textureImporter.SetPlatformTextureSettings("iPhone", 256, TextureImporterFormat.PVRTC_RGB4, 100, false); } } else if (assetPath.Contains("Res/Resources/effects")) { TextureImporter textureImporter = (TextureImporter)assetImporter; string textureName = assetPath.Replace(".tga", string.Empty); textureName = textureName.Replace(".png", string.Empty); bool isAphla = false, turecolor = false; if (textureName.EndsWith("_a")) { isAphla = true; textureName = assetPath.Replace("_a.tga", string.Empty); textureName = textureName.Replace("_a.png", string.Empty); } else if (textureName.EndsWith("_t")) { turecolor = true; textureName = assetPath.Replace("_t.tga", string.Empty); textureName = textureName.Replace("_t.png", string.Empty); } string sizeStr = textureName.Substring(textureName.LastIndexOf('_') + 1); //Debugger.Log(sizeStr.ToString()); int size = 0; int.TryParse(sizeStr, out size); //Debugger.Log(size.ToString()); bool isPowerOf2 = false; switch (size) { case 16: case 32: case 64: case 128: case 256: case 512: isPowerOf2 = true; break; default: isPowerOf2 = false; break; } if (isPowerOf2) { if (turecolor) { textureImporter.SetPlatformTextureSettings("Android", size, TextureImporterFormat.RGBA32, 100, false); textureImporter.SetPlatformTextureSettings("iPhone", size, TextureImporterFormat.PVRTC_RGBA4, 100, false); } else if (isAphla) { textureImporter.SetPlatformTextureSettings("Android", size, TextureImporterFormat.RGB16); textureImporter.SetPlatformTextureSettings("iPhone", size, TextureImporterFormat.RGB16); } else { textureImporter.SetPlatformTextureSettings("Android", size, TextureImporterFormat.ETC_RGB4, 100, false); textureImporter.SetPlatformTextureSettings("iPhone", size, TextureImporterFormat.PVRTC_RGB4, 100, false); } } else { Debugger.LogError("{0}'s size is not power of 2 .", assetPath); //if (isAphla) //{ // textureImporter.SetPlatformTextureSettings("Android", size, TextureImporterFormat.RGB16); // textureImporter.SetPlatformTextureSettings("iPhone", size, TextureImporterFormat.RGB16); //} //else //{ // textureImporter.SetPlatformTextureSettings("Android", 128, TextureImporterFormat.ETC_RGB4, 100, false); // textureImporter.SetPlatformTextureSettings("iPhone", 128, TextureImporterFormat.PVRTC_RGB4, 100, false); //} } } else if (assetPath.Contains("Res/Resources/launch")) { if (assetPath.Contains("duwan") || assetPath.Contains("shunwang")) { return; } TextureImporter textureImporter = (TextureImporter)assetImporter; textureImporter.textureType = TextureImporterType.Image; textureImporter.grayscaleToAlpha = false; textureImporter.wrapMode = TextureWrapMode.Clamp; textureImporter.filterMode = FilterMode.Trilinear; textureImporter.mipmapEnabled = false; textureImporter.anisoLevel = 0; textureImporter.ClearPlatformTextureSettings("iPhone"); textureImporter.ClearPlatformTextureSettings("Android"); textureImporter.ClearPlatformTextureSettings("Standalone"); textureImporter.SetPlatformTextureSettings("Android", 1024, TextureImporterFormat.ETC_RGB4, 100, false); textureImporter.SetPlatformTextureSettings("iPhone", 1024, TextureImporterFormat.PVRTC_RGBA4, 100, false); } else if (assetPath.Contains("Res/Resources/ui_textures")) { //if (assetPath.Contains("loading_07")) return; return; TextureImporter textureImporter = (TextureImporter)assetImporter; textureImporter.textureType = TextureImporterType.Image; textureImporter.grayscaleToAlpha = false; textureImporter.wrapMode = TextureWrapMode.Clamp; textureImporter.filterMode = FilterMode.Trilinear; textureImporter.mipmapEnabled = false; textureImporter.anisoLevel = 0; textureImporter.ClearPlatformTextureSettings("iPhone"); textureImporter.ClearPlatformTextureSettings("Android"); textureImporter.ClearPlatformTextureSettings("Standalone"); textureImporter.SetPlatformTextureSettings("Android", 1024, TextureImporterFormat.ETC_RGB4, 100, false); textureImporter.SetPlatformTextureSettings("iPhone", 1024, TextureImporterFormat.PVRTC_RGBA4, 100, false); } }
static public void PackTexture(List <TextureImporter> imps, string atlasName, string atlasPath, string texturePath) { if (imps == null || imps.Count <= 0) { Debug.Log("选择打包的小图数量为0"); return; } //检查文件夹是否存在 if (!Directory.Exists(atlasPath)) { Directory.CreateDirectory(atlasPath); } //检查文件夹是否存在 if (!Directory.Exists(texturePath)) { Directory.CreateDirectory(texturePath); } //图集预制相对路径 string relativePath = atlasPath + atlasName + ".prefab"; //图集预制绝对路径 //string absolutePath = Path.GetFullPath(atlasPath) + atlasName + ".png"; //纹理相对路径 string relativeTexPath = texturePath + atlasName + ".png"; //纹理绝对路径 string absoluteTexPath = Path.GetFullPath(texturePath) + atlasName + ".png"; //判断图片格式不对了,先进性格式的转换 CheckFormat(imps); //加载图片资源 Texture2D[] texs = LoadTextures(imps); //创建Atlas实例 UIAtlas atlasInfo = ScriptableObject.CreateInstance <UIAtlas>(); atlasInfo.name = atlasName; //打包小图到大纹理并返回uv信息 Texture2D atlasTexture = new Texture2D(1, 1); Rect[] rs = atlasTexture.PackTextures(texs, (int)padding, (int)matAtlasSize); //把图集写入到磁盘文件 File.WriteAllBytes(absoluteTexPath, atlasTexture.EncodeToPNG()); //刷新图片 AssetDatabase.Refresh(); AssetDatabase.ImportAsset(relativeTexPath); //记录图片的名字,只是用于输出日志用; StringBuilder names = new StringBuilder(); //SpriteMetaData结构可以让我们编辑图片的一些信息,想图片的name,包围盒border,在图集中的区域rect等 SpriteMetaData[] sheet = new SpriteMetaData[rs.Length]; for (var i = 0; i < sheet.Length; i++) { SpriteMetaData meta = new SpriteMetaData(); meta.name = texs[i].name; //这里的rect记录的是单个图片在图集中的uv坐标值 meta.rect = rs[i]; meta.rect.Set( meta.rect.x * atlasTexture.width, meta.rect.y * atlasTexture.height, meta.rect.width * atlasTexture.width, meta.rect.height * atlasTexture.height ); TextureImporter texImp = imps[i]; //如果图片有包围盒信息的话 if (texImp != null) { meta.border = texImp.spriteBorder; meta.pivot = texImp.spritePivot; } sheet[i] = meta; SpriteData sd = new SpriteData(); sd.name = meta.name; sd.alignment = meta.alignment; sd.border = new Vector4(meta.border.x, meta.border.y, meta.border.z, meta.border.w); sd.pivot = new Vector2(meta.pivot.x, meta.pivot.y); sd.rect = new Rect(meta.rect); if (!atlasInfo.HasSpriteData(meta.name)) { atlasInfo._uvs.Add(sd); } else { Debug.LogErrorFormat("{0}图集中存在相同名称的图片:{1}", atlasInfo.name, meta.name); } //打印日志用 names.Append(meta.name); if (i < sheet.Length - 1) { names.Append(","); } } //设置图集纹理信息 TextureImporter imp = TextureImporter.GetAtPath(relativeTexPath) as TextureImporter; imp.textureType = TextureImporterType.Sprite;//图集的类型 imp.textureCompression = TextureImporterCompression.Uncompressed; imp.alphaIsTransparency = true; //imp.SetPlatformTextureSettings("iPhone", 2048, TextureImporterFormat.AutomaticTruecolor); imp.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.ETC2_RGBA8); imp.ClearPlatformTextureSettings("iPhone"); imp.textureFormat = TextureImporterFormat.AutomaticTruecolor; //TextureImporterFormat.AutomaticCompressed;//图集的格式 imp.spriteImportMode = SpriteImportMode.Multiple; //Multiple表示我们这个大图片(图集)中包含很多小图片 imp.mipmapEnabled = false; //是否开启mipmap imp.isReadable = false; imp.spritesheet = sheet; //设置图集中小图片的信息(每个图片所在的区域rect等) // 保存并刷新 imp.SaveAndReimport(); //重新加载导出的纹理 Texture2D texture2d = AssetDatabase.LoadAssetAtPath <Texture2D>(relativeTexPath); //填充图集信息 atlasInfo._tex2d = texture2d; AssetDatabase.CreateAsset(atlasInfo, relativePath); GameObject.DestroyImmediate(atlasInfo); //输出日志 Debug.Log("Atlas create ok. " + names.ToString()); }
void OnPreprocessTexture() { if (assetPath.Contains("Res/Atlas") || assetPath.Contains("Res/Sprite") || assetPath.Contains("Res/Resources/Sprite")) { string atlasName = new DirectoryInfo(Path.GetDirectoryName(assetPath)).Name; TextureImporter textureImporter = assetImporter as TextureImporter; //string assetName = assetPath.Substring(assetPath.LastIndexOf("/") + 1); //assetName = assetName.Replace(".png", string.Em pty); //if (atlasName == "Atlas") { textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spritePackingTag = null; //atlasName; textureImporter.mipmapEnabled = false; textureImporter.spriteImportMode = SpriteImportMode.Single; textureImporter.filterMode = FilterMode.Bilinear; //textureImporter.maxTextureSize = 2048; //textureImporter.isReadable = false; //textureImporter.ClearPlatformTextureSettings("android"); //textureImporter.ClearPlatformTextureSettings("iPhone"); //textureImporter.ClearPlatformTextureSettings("Standalone"); #if UNITY_ANDROID //TextureImporterPlatformSettings androidTextureImporterPlatformSettings = new TextureImporterPlatformSettings(); //androidTextureImporterPlatformSettings.format = TextureImporterFormat.RGBA32; //androidTextureImporterPlatformSettings.textureCompression = TextureImporterCompression.Uncompressed; //androidTextureImporterPlatformSettings.overridden = true; //textureImporter.SetPlatformTextureSettings(androidTextureImporterPlatformSettings); #endif #if UNITY_IOS TextureImporterPlatformSettings iOSTextureImporterPlatformSettings = new TextureImporterPlatformSettings(); iOSTextureImporterPlatformSettings.format = TextureImporterFormat.PVRTC_RGBA4; iOSTextureImporterPlatformSettings.textureCompression = TextureImporterCompression.Uncompressed; iOSTextureImporterPlatformSettings.overridden = true; textureImporter.SetPlatformTextureSettings(iOSTextureImporterPlatformSettings); #endif } } if (assetPath.Contains("Res/Textures")) { string atlasName = new DirectoryInfo(Path.GetDirectoryName(assetPath)).Name; TextureImporter textureImporter = assetImporter as TextureImporter; //string assetName = assetPath.Substring(assetPath.LastIndexOf("/") + 1); //assetName = assetName.Replace(".png", string.Empty); //if (atlasName == "Atlas") { string fileName = assetPath.Substring(assetPath.LastIndexOf("/")); MemoryStream mstream = new MemoryStream(); FileStream fs = new FileStream(textureImporter.assetPath, FileMode.Open); byte[] byteData = new byte[fs.Length]; fs.Read(byteData, 0, byteData.Length); fs.Close(); string root = Application.dataPath.Substring(0, Application.dataPath.IndexOf("Assets")); string path = root + textureImporter.assetPath; Debug.LogError(path.LastIndexOf("/")); path = path.Substring(0, path.LastIndexOf("/")) + "/" + fileName + ".bytes"; Debug.LogError(atlasName); Debug.LogError(path); Stream s = new FileStream(path, FileMode.Create); BinaryWriter sw = new BinaryWriter(s, Encoding.Unicode); for (int i = 0; i < byteData.Length; i++) { sw.Write(byteData[i]); } sw.Flush(); sw.Close(); s.Close(); textureImporter.textureType = TextureImporterType.Default; textureImporter.spritePackingTag = null; //atlasName; textureImporter.mipmapEnabled = false; textureImporter.npotScale = TextureImporterNPOTScale.None; textureImporter.filterMode = FilterMode.Bilinear; textureImporter.isReadable = false; textureImporter.ClearPlatformTextureSettings("android"); textureImporter.ClearPlatformTextureSettings("iPhone"); textureImporter.ClearPlatformTextureSettings("Standalone"); #if UNITY_ANDROID //TextureImporterPlatformSettings androidTextureImporterPlatformSettings = new TextureImporterPlatformSettings(); //androidTextureImporterPlatformSettings.format = TextureImporterFormat.RGBA32; //androidTextureImporterPlatformSettings.textureCompression = TextureImporterCompression.Uncompressed; //androidTextureImporterPlatformSettings.overridden = true; //textureImporter.SetPlatformTextureSettings(androidTextureImporterPlatformSettings); #endif #if UNITY_IOS //TextureImporterPlatformSettings iOSTextureImporterPlatformSettings = new TextureImporterPlatformSettings(); //iOSTextureImporterPlatformSettings.format = TextureImporterFormat.RGBA32; //iOSTextureImporterPlatformSettings.textureCompression = TextureImporterCompression.Uncompressed; //iOSTextureImporterPlatformSettings.overridden = true; //textureImporter.SetPlatformTextureSettings(iOSTextureImporterPlatformSettings); #endif } } if (assetPath.Contains("Res/Materials")) { TextureImporter textureImporter = assetImporter as TextureImporter; //暂不知压缩格式 } AssetDatabase.SaveAssets(); }
// ref copy some code from https://github.com/unity3d-jp/AssetGraph protected override void OverwriteImportSettings(AssetImporter importer, AssetImporter templeteImporter) { TextureImporter reference = templeteImporter as TextureImporter; TextureImporter target = importer as TextureImporter; target.textureType = reference.textureType; TextureImporterSettings dstSettings = new TextureImporterSettings(); TextureImporterSettings srcSettings = new TextureImporterSettings(); target.ReadTextureSettings(srcSettings); reference.ReadTextureSettings(dstSettings); target.SetTextureSettings(dstSettings); // some unity version do not properly copy properties via TextureSettings, // so also perform manual copy target.anisoLevel = reference.anisoLevel; target.borderMipmap = reference.borderMipmap; target.compressionQuality = reference.compressionQuality; target.convertToNormalmap = reference.convertToNormalmap; target.fadeout = reference.fadeout; target.filterMode = reference.filterMode; target.generateCubemap = reference.generateCubemap; target.heightmapScale = reference.heightmapScale; target.isReadable = reference.isReadable; target.maxTextureSize = reference.maxTextureSize; target.mipMapBias = reference.mipMapBias; target.mipmapEnabled = reference.mipmapEnabled; target.mipmapFadeDistanceEnd = reference.mipmapFadeDistanceEnd; target.mipmapFadeDistanceStart = reference.mipmapFadeDistanceStart; target.mipmapFilter = reference.mipmapFilter; target.normalmapFilter = reference.normalmapFilter; target.npotScale = reference.npotScale; target.wrapMode = reference.wrapMode; /* read only */ // target.qualifiesForSpritePacking #if !UNITY_5_5_OR_NEWER // obsolete features target.generateMipsInLinearSpace = reference.generateMipsInLinearSpace; target.grayscaleToAlpha = reference.grayscaleToAlpha; target.lightmap = reference.lightmap; target.linearTexture = reference.linearTexture; target.normalmap = reference.normalmap; target.textureFormat = reference.textureFormat; foreach (var platformName in new string[] { "Android", "iPhone" }) { int maxTextureSize; TextureImporterFormat format; int compressionQuality; if (reference.GetPlatformTextureSettings(platformName, out maxTextureSize, out format, out compressionQuality)) { target.SetPlatformTextureSettings(platformName, maxTextureSize, format, compressionQuality, false); } else { target.ClearPlatformTextureSettings(platformName); } } #else target.allowAlphaSplitting = reference.allowAlphaSplitting; target.alphaIsTransparency = reference.alphaIsTransparency; target.textureShape = reference.textureShape; target.alphaSource = reference.alphaSource; target.sRGBTexture = reference.sRGBTexture; target.textureCompression = reference.textureCompression; target.crunchedCompression = reference.crunchedCompression; foreach (var platformName in new string[] { "Android", "iOS" }) { var impSet = reference.GetPlatformTextureSettings(platformName); target.SetPlatformTextureSettings(impSet); } #endif #if UNITY_2017_1_OR_NEWER target.alphaTestReferenceValue = reference.alphaTestReferenceValue; target.mipMapsPreserveCoverage = reference.mipMapsPreserveCoverage; target.wrapModeU = reference.wrapModeU; target.wrapModeV = reference.wrapModeV; target.wrapModeW = reference.wrapModeW; #endif }
void OnPreprocessTexture() { if (assetPath.Contains("Res/Atlas")) { string atlasName = new DirectoryInfo(Path.GetDirectoryName(assetPath)).Name; TextureImporter textureImporter = assetImporter as TextureImporter; //string assetName = assetPath.Substring(assetPath.LastIndexOf("/") + 1); //assetName = assetName.Replace(".png", string.Empty); //if (atlasName == "Atlas") { textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spritePackingTag = atlasName; textureImporter.mipmapEnabled = false; textureImporter.spriteImportMode = SpriteImportMode.Single; textureImporter.filterMode = FilterMode.Bilinear; textureImporter.maxTextureSize = 2048; textureImporter.isReadable = false; textureImporter.ClearPlatformTextureSettings("android"); textureImporter.ClearPlatformTextureSettings("iPhone"); textureImporter.ClearPlatformTextureSettings("Standalone"); #if UNITY_ANDROID TextureImporterPlatformSettings androidTextureImporterPlatformSettings = new TextureImporterPlatformSettings(); androidTextureImporterPlatformSettings.format = TextureImporterFormat.RGBA32; androidTextureImporterPlatformSettings.textureCompression = TextureImporterCompression.Uncompressed; androidTextureImporterPlatformSettings.overridden = true; textureImporter.SetPlatformTextureSettings(androidTextureImporterPlatformSettings); #endif #if UNITY_IOS TextureImporterPlatformSettings iOSTextureImporterPlatformSettings = new TextureImporterPlatformSettings(); iOSTextureImporterPlatformSettings.format = TextureImporterFormat.RGBA32; iOSTextureImporterPlatformSettings.textureCompression = TextureImporterCompression.Uncompressed; iOSTextureImporterPlatformSettings.overridden = true; textureImporter.SetPlatformTextureSettings(iOSTextureImporterPlatformSettings); #endif } } if (assetPath.Contains("Res/Resources/textures")) { return; TextureImporter textureImporter = (TextureImporter)assetImporter; textureImporter.textureType = TextureImporterType.Sprite; textureImporter.wrapMode = TextureWrapMode.Clamp; textureImporter.filterMode = FilterMode.Trilinear; textureImporter.mipmapEnabled = false; textureImporter.anisoLevel = 0; textureImporter.ClearPlatformTextureSettings("iPhone"); textureImporter.ClearPlatformTextureSettings("Android"); textureImporter.ClearPlatformTextureSettings("Standalone"); #if UNITY_ANDROID TextureImporterPlatformSettings androidTextureImporterPlatformSettings = new TextureImporterPlatformSettings(); androidTextureImporterPlatformSettings.format = TextureImporterFormat.RGBA32; androidTextureImporterPlatformSettings.textureCompression = TextureImporterCompression.Uncompressed; androidTextureImporterPlatformSettings.overridden = true; textureImporter.SetPlatformTextureSettings(androidTextureImporterPlatformSettings); #endif #if UNITY_IOS TextureImporterPlatformSettings iOSTextureImporterPlatformSettings = new TextureImporterPlatformSettings(); iOSTextureImporterPlatformSettings.format = TextureImporterFormat.RGBA32; iOSTextureImporterPlatformSettings.textureCompression = TextureImporterCompression.Uncompressed; iOSTextureImporterPlatformSettings.overridden = true; textureImporter.SetPlatformTextureSettings(iOSTextureImporterPlatformSettings); #endif } if (assetPath.Contains("Res/Resources/texture_720"))// 处理720图片 { TextureImporter textureImporter = (TextureImporter)assetImporter; textureImporter.textureType = TextureImporterType.Default; textureImporter.textureShape = TextureImporterShape.Texture2D; textureImporter.wrapMode = TextureWrapMode.Clamp; textureImporter.filterMode = FilterMode.Bilinear; textureImporter.mipmapEnabled = false; textureImporter.anisoLevel = 0; textureImporter.ClearPlatformTextureSettings("iPhone"); textureImporter.ClearPlatformTextureSettings("Android"); textureImporter.ClearPlatformTextureSettings("Standalone"); TextureImporterPlatformSettings androidTextureImporterPlatformSettings = new TextureImporterPlatformSettings(); androidTextureImporterPlatformSettings.format = TextureImporterFormat.ETC_RGB4Crunched; androidTextureImporterPlatformSettings.textureCompression = TextureImporterCompression.CompressedHQ; androidTextureImporterPlatformSettings.overridden = true; androidTextureImporterPlatformSettings.crunchedCompression = true; androidTextureImporterPlatformSettings.maxTextureSize = 4096; androidTextureImporterPlatformSettings.compressionQuality = 100; textureImporter.SetPlatformTextureSettings(androidTextureImporterPlatformSettings); } AssetDatabase.SaveAssets(); }
private static void PasteSpriteTextureSettings(MenuCommand command) { //Grab current Texture Importer TextureImporter currentTexture = command.context as TextureImporter; TextureImporterPlatformSettings textureImporterPFsettings = new TextureImporterPlatformSettings(); //Copy over platform specific settings switch (clipboard.copyType) { //*************************************** case "AllData": //Copy over sprites currentTexture.spritesheet = clipboard.spriteData.ToArray(); //Copy over settings currentTexture.SetTextureSettings(clipboard.spriteSettings); //Copy over overrides foreach (string tempString in availablePlatforms) { #if UNITY_4_5 || UNITY_4_6 || UNITY_5_0 || UNITY_5_1 currentTexture.SetPlatformTextureSettings(tempString, 1024, TextureImporterFormat.AutomaticCompressed, 0); #else textureImporterPFsettings.maxTextureSize = 1024; textureImporterPFsettings.format = TextureImporterFormat.Automatic; textureImporterPFsettings.compressionQuality = 0; textureImporterPFsettings.allowsAlphaSplitting = true; currentTexture.SetPlatformTextureSettings(textureImporterPFsettings); #endif currentTexture.ClearPlatformTextureSettings(tempString); } foreach (PlatformTextureSettings tempPlatSettings in platformTextureSettings) { #if UNITY_4_5 || UNITY_4_6 || UNITY_5_0 || UNITY_5_1 || UNITY_5_0 currentTexture.SetPlatformTextureSettings(tempPlatSettings.spritePlatform, tempPlatSettings.spriteMaxTextureSize, tempPlatSettings.spriteTextureFormat, tempPlatSettings.spriteCompressionQuality); #else textureImporterPFsettings.maxTextureSize = tempPlatSettings.spriteMaxTextureSize; textureImporterPFsettings.format = tempPlatSettings.spriteTextureFormat; textureImporterPFsettings.compressionQuality = tempPlatSettings.spriteCompressionQuality; textureImporterPFsettings.allowsAlphaSplitting = true; currentTexture.SetPlatformTextureSettings(textureImporterPFsettings); #endif } break; //*************************************** case "AllDataNoOverride": //Copy over sprites currentTexture.spritesheet = clipboard.spriteData.ToArray(); //Copy over settings currentTexture.SetTextureSettings(clipboard.spriteSettings); break; //*************************************** case "OnlyOverride": //Copy over overrides foreach (string tempString in availablePlatforms) { #if UNITY_4_5 || UNITY_4_6 || UNITY_5_0 || UNITY_5_1 currentTexture.SetPlatformTextureSettings(tempString, 1024, TextureImporterFormat.AutomaticCompressed, 0); #else textureImporterPFsettings.maxTextureSize = 1024; textureImporterPFsettings.format = TextureImporterFormat.Automatic; textureImporterPFsettings.compressionQuality = 0; textureImporterPFsettings.allowsAlphaSplitting = true; currentTexture.SetPlatformTextureSettings(textureImporterPFsettings); #endif currentTexture.ClearPlatformTextureSettings(tempString); } foreach (PlatformTextureSettings tempPlatSettings in platformTextureSettings) { #if UNITY_4_5 || UNITY_4_6 || UNITY_5_0 || UNITY_5_1 currentTexture.SetPlatformTextureSettings(tempPlatSettings.spritePlatform, tempPlatSettings.spriteMaxTextureSize, tempPlatSettings.spriteTextureFormat, tempPlatSettings.spriteCompressionQuality); #else textureImporterPFsettings.maxTextureSize = tempPlatSettings.spriteMaxTextureSize; textureImporterPFsettings.format = tempPlatSettings.spriteTextureFormat; textureImporterPFsettings.compressionQuality = tempPlatSettings.spriteCompressionQuality; textureImporterPFsettings.allowsAlphaSplitting = true; currentTexture.SetPlatformTextureSettings(textureImporterPFsettings); #endif } break; } //Refresh asset/apply settings AssetDatabase.ImportAsset(currentTexture.assetPath, ImportAssetOptions.ForceUpdate); }
void OnGUI() { EditorGUILayout.BeginVertical(); templateTexture = EditorGUILayout.ObjectField("Template texture", templateTexture, typeof(Texture), false) as Texture; if (templateTexture != null) { if (GUILayout.Button("Reset all textures")) { foreach (UnityEngine.Object obj in Selection.objects) { string path = AssetDatabase.GetAssetPath(obj); if (Directory.Exists(path)) { string templatePath = AssetDatabase.GetAssetPath(templateTexture); TextureImporter templateImporter = TextureImporter.GetAtPath(templatePath) as TextureImporter; TextureImporterSettings templateSettings = new TextureImporterSettings(); templateImporter.ReadTextureSettings(templateSettings); PrintTextureImporterSettings(templateSettings); List <string> urls = new List <string>(); PlatformOption[] templateOptions = new PlatformOption[platformNames.Length]; for (int i = 0; i < platformNames.Length; i++) { templateOptions[i] = GetPlatformOption(templateImporter, platformNames[i]); } WalkThroughAssetsInDirectory <Texture>(path, urls); int numFiles = urls.Count; // Debug.Log("\ntemplateImporter"); // PrintTextureImporter(templateImporter); for (int i = 0; i < numFiles; i++) { if (EditorUtility.DisplayCancelableProgressBar("Setting texture", i + "/" + numFiles, (float)i / (float)numFiles)) { break; } string url = urls[i]; TextureImporter importer = TextureImporter.GetAtPath(url) as TextureImporter; if (importer == null) { continue; } bool isDirty = false; TextureImporterSettings settings = new TextureImporterSettings(); importer.ReadTextureSettings(settings); CopyTextureImporter(importer, templateImporter); CopyTextureImporterSettings(settings, templateSettings); // Debug.Log("\n " + url); // PrintTextureImporter(importer); for (int j = 0; j < platformNames.Length; j++) { string platform = platformNames[j]; PlatformOption templateOption = templateOptions[j]; PlatformOption option = GetPlatformOption(importer, platform); if (templateOption.enable != option.enable || templateOption.maxTextureSize != option.maxTextureSize || templateOption.format != option.format) { isDirty = true; importer.SetPlatformTextureSettings(platform, templateOption.maxTextureSize, templateOption.format, templateOption.compressionQuality, false); Debug.Log(platform + " " + templateOption.maxTextureSize); if (!templateOption.enable) { importer.ClearPlatformTextureSettings(platform); } } // int maxTextureSize; // TextureImporterFormat textureFormat; // if (templateImporter.GetPlatformTextureSettings(platform, out maxTextureSize, out textureFormat)) // { // importer.SetPlatformTextureSettings(platform, maxTextureSize, textureFormat); // } // else // importer.ClearPlatformTextureSettings(platform); } importer.SetTextureSettings(templateSettings); AssetDatabase.ImportAsset(url); // AssetDatabase.SaveAssets(); // Resources.UnloadUnusedAssets(); } EditorUtility.ClearProgressBar(); } } } } EditorGUILayout.EndVertical(); }
private void OverwriteImportSettings(TextureImporter importer) { var reference = referenceImporter as TextureImporter; UnityEngine.Assertions.Assert.IsNotNull(reference); importer.textureType = reference.textureType; TextureImporterSettings settings = new TextureImporterSettings(); reference.ReadTextureSettings(settings); importer.SetTextureSettings(settings); // some unity version do not properly copy properties via TextureSettings, // so also perform manual copy importer.anisoLevel = reference.anisoLevel; importer.borderMipmap = reference.borderMipmap; importer.compressionQuality = reference.compressionQuality; importer.convertToNormalmap = reference.convertToNormalmap; importer.fadeout = reference.fadeout; importer.filterMode = reference.filterMode; importer.generateCubemap = reference.generateCubemap; importer.heightmapScale = reference.heightmapScale; importer.isReadable = reference.isReadable; importer.maxTextureSize = reference.maxTextureSize; importer.mipMapBias = reference.mipMapBias; importer.mipmapEnabled = reference.mipmapEnabled; importer.mipmapFadeDistanceEnd = reference.mipmapFadeDistanceEnd; importer.mipmapFadeDistanceStart = reference.mipmapFadeDistanceStart; importer.mipmapFilter = reference.mipmapFilter; importer.normalmapFilter = reference.normalmapFilter; importer.npotScale = reference.npotScale; importer.spriteBorder = reference.spriteBorder; importer.spriteImportMode = reference.spriteImportMode; importer.spritePackingTag = reference.spritePackingTag; importer.spritePivot = reference.spritePivot; importer.spritePixelsPerUnit = reference.spritePixelsPerUnit; importer.spritesheet = reference.spritesheet; importer.wrapMode = reference.wrapMode; /* read only */ // importer.qualifiesForSpritePacking #if !UNITY_5_5_OR_NEWER // obsolete features importer.generateMipsInLinearSpace = reference.generateMipsInLinearSpace; importer.grayscaleToAlpha = reference.grayscaleToAlpha; importer.lightmap = reference.lightmap; importer.linearTexture = reference.linearTexture; importer.normalmap = reference.normalmap; importer.textureFormat = reference.textureFormat; foreach (var g in NodeGUIUtility.SupportedBuildTargetGroups) { var platformName = BuildTargetUtility.TargetToAssetBundlePlatformName(g, BuildTargetUtility.PlatformNameType.TextureImporter); int maxTextureSize; TextureImporterFormat format; int compressionQuality; if (reference.GetPlatformTextureSettings(platformName, out maxTextureSize, out format, out compressionQuality)) { importer.SetPlatformTextureSettings(platformName, maxTextureSize, format, compressionQuality, false); } else { importer.ClearPlatformTextureSettings(platformName); } } #else importer.allowAlphaSplitting = reference.allowAlphaSplitting; importer.alphaIsTransparency = reference.alphaIsTransparency; importer.textureShape = reference.textureShape; importer.alphaSource = reference.alphaSource; importer.sRGBTexture = reference.sRGBTexture; importer.textureCompression = reference.textureCompression; importer.crunchedCompression = reference.crunchedCompression; foreach (var g in NodeGUIUtility.SupportedBuildTargetGroups) { var platformName = BuildTargetUtility.TargetToAssetBundlePlatformName(g, BuildTargetUtility.PlatformNameType.TextureImporter); var impSet = reference.GetPlatformTextureSettings(platformName); importer.SetPlatformTextureSettings(impSet); } #endif #if UNITY_2017_1_OR_NEWER importer.alphaTestReferenceValue = reference.alphaTestReferenceValue; importer.mipMapsPreserveCoverage = reference.mipMapsPreserveCoverage; importer.wrapModeU = reference.wrapModeU; importer.wrapModeV = reference.wrapModeV; importer.wrapModeW = reference.wrapModeW; #endif }
void OnPreprocessTexture() { if (Path.GetExtension(assetPath) == ".png") { // If the asset is a png file, look for a matching xml file string atlasPath = Path.ChangeExtension(assetPath, ".xml"); if (File.Exists(atlasPath)) { TextureImporter importer = assetImporter as TextureImporter; // if we're not in sprite mode, nothing to do here if (importer.textureType != TextureImporterType.Sprite) { return; } XDocument xmlDocument = XDocument.Load(atlasPath); // attempt to verify that we're really looking at a TexturePacker atlas if (xmlDocument.Root.Name.LocalName != "TextureAtlas") { return; } int imageHeight = int.Parse(xmlDocument.Root.Attribute("height").Value, CultureInfo.InvariantCulture); var sprites = xmlDocument.Descendants("sprite"); importer.spriteImportMode = SpriteImportMode.Multiple; List <SpriteMetaData> metaData = new List <SpriteMetaData>(); foreach (var sprite in sprites) { string n = sprite.Attribute("n").Value; int x = int.Parse(sprite.Attribute("x").Value, CultureInfo.InvariantCulture); int y = int.Parse(sprite.Attribute("y").Value, CultureInfo.InvariantCulture); int w = int.Parse(sprite.Attribute("w").Value, CultureInfo.InvariantCulture); int h = int.Parse(sprite.Attribute("h").Value, CultureInfo.InvariantCulture); int oX = (sprite.Attribute("oX") == null) ? 0 : int.Parse(sprite.Attribute("oX").Value, CultureInfo.InvariantCulture); int oY = (sprite.Attribute("oY") == null) ? 0 : int.Parse(sprite.Attribute("oY").Value, CultureInfo.InvariantCulture); int oW = (sprite.Attribute("oW") == null) ? w : int.Parse(sprite.Attribute("oW").Value, CultureInfo.InvariantCulture); int oH = (sprite.Attribute("oH") == null) ? h : int.Parse(sprite.Attribute("oH").Value, CultureInfo.InvariantCulture); bool r = sprite.Attribute("r") != null; bool trim = (sprite.Attribute("oX") != null) || (sprite.Attribute("oY") != null) || (sprite.Attribute("oW") != null) || (sprite.Attribute("oH") != null); if (r) { Debug.LogWarning("Rotated TexturePacker Sprites are not currently supported. File: " + assetPath + " Sprite: " + n); } SpriteMetaData spriteMetaData = new SpriteMetaData(); spriteMetaData = new SpriteMetaData(); // According to Unity docs, Center = 0, Custom = 9 spriteMetaData.alignment = trim ? 9 : 0; spriteMetaData.name = n; spriteMetaData.pivot = new Vector2(((oW / 2.0f) - (oX)) / (float)w, 1.0f - ((oH / 2.0f) - (oY)) / (float)h); spriteMetaData.rect = new Rect(x, imageHeight - y - h, w, h); metaData.Add(spriteMetaData); } if (importer.spritesheet == null || importer.spritesheet.Length == 0) { importer.spritesheet = metaData.ToArray(); } else { // merge importer.spritesheet and metaData // We don't want to change the indices of existing sprites List <SpriteMetaData> spriteData = new List <SpriteMetaData>(); int deletedCount = 0; SpriteMetaData deletedData = new SpriteMetaData(); deletedData.name = "DELETED_SPRITE"; deletedData.rect = new Rect(0, 0, 1, 1); // Add all of the data that matches the existing data foreach (var sprite in importer.spritesheet) { int index = metaData.FindIndex(p => p.name == sprite.name); if (index != -1) { spriteData.Add(metaData[index]); SpriteMetaData temp = metaData[index]; temp.name = "IGNORE_USED_SPRITE_DATA"; metaData[index] = temp; } else { spriteData.Add(deletedData); deletedCount++; } } // Add all of the new data foreach (var sprite in metaData) { if (sprite.name == "IGNORE_USED_SPRITE_DATA") { continue; } if (deletedCount > 0) { deletedCount--; int index = spriteData.FindIndex(p => p.name == "DELETED_SPRITE"); spriteData[index] = sprite; } else { spriteData.Add(sprite); } } // Rename any remaning deleted entries to DELETED_SPRITE_# if (deletedCount > 0) { for (int i = 0; i < deletedCount; i++) { int index = spriteData.FindIndex(p => p.name == "DELETED_SPRITE"); SpriteMetaData temp = spriteData[index]; temp.name = "DELETED_SPRITE_" + i; spriteData[index] = temp; } } // Calling ClearPlatformTextureSettings to stop the texture importer // from holding on the a cached version of importer.spritesheet. // This is a workaround, and might be the wrong thing to do. importer.ClearPlatformTextureSettings("Standalone"); importer.spritesheet = spriteData.ToArray(); } } } }