/// <summary> /// On GUI /// </summary> void OnGUI() { if (rerun == false) { UIDesign(); } GUILayout.Label("BattlePhaze Texture Importer", TextLargeStyling); EditorGUILayout.BeginHorizontal(); GUILayout.Label("Mip Maps", TextStyling); MipMapEnabled = EditorGUILayout.Toggle(MipMapEnabled); GUILayout.EndVertical(); EditorGUILayout.BeginHorizontal(); GUILayout.Label("Crunch Compression", TextStyling); CrunchCompression = EditorGUILayout.Toggle(CrunchCompression); GUILayout.EndVertical(); #if !UNITY_2017 EditorGUILayout.BeginHorizontal(); GUILayout.Label("Streaming MipMap", TextStyling); StreamingMipmaps = EditorGUILayout.Toggle(StreamingMipmaps); GUILayout.EndVertical(); #endif EditorGUILayout.BeginHorizontal(); GUILayout.Label("Texture Max Size", TextStyling); TextureMaxSize = EditorGUILayout.IntField(TextureMaxSize); GUILayout.EndVertical(); EditorGUILayout.BeginHorizontal(); GUILayout.Label("Texture Compression", TextStyling); TextureCompression = (TextureImporterCompression)EditorGUILayout.EnumPopup(TextureCompression, EnumStyling); GUILayout.EndVertical(); if (GUILayout.Button("Run Texture Import",StyleButton)) { TextureConvert(); } }
private void OnGUI() { EditorGUILayout.Space(); EditorGUILayout.HelpBox("设置选中图片或选中路径下的图片属性", MessageType.Info); EditorGUILayout.Space(); textureType = (TextureImporterType)EditorGUILayout.EnumPopup("类型:", textureType); textureFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("格式:", textureFormat); textureSize = (MaxSize)EditorGUILayout.EnumPopup("尺寸:", textureSize); textureCompression = (TextureImporterCompression)EditorGUILayout.EnumPopup("压缩:", textureCompression); ifAllowsAlphaSplitting = EditorGUILayout.Toggle("是否允许透明分离:", ifAllowsAlphaSplitting); ifMipmapEnabled = EditorGUILayout.Toggle("是否允许Mipmap:", ifMipmapEnabled); packingTag = EditorGUILayout.TextField("PackTag:", packingTag); EditorGUILayout.Space(); if (GUILayout.Button("设置")) { TextureImporterPlatformSettings t = new TextureImporterPlatformSettings(); t.allowsAlphaSplitting = ifAllowsAlphaSplitting; t.format = textureFormat; t.maxTextureSize = (int)textureSize; t.textureCompression = textureCompression; SelectedChangeTextureFormatSettings(t, textureType); } }
private void OnGUI() { EditorGUILayout.Space(); EditorGUILayout.HelpBox("设置UI文件夹路径,Assets/Res/UI/ 为默认值", MessageType.Info); this.texturePath = EditorGUILayout.TextField("resources folder:", this.texturePath); EditorGUILayout.Space(); this.platform = (Platform)EditorGUILayout.EnumPopup("平台:", this.platform); textureType = (TextureImporterType)EditorGUILayout.EnumPopup("类型:", textureType); textureFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("格式:", textureFormat); textureSize = (MaxSize)EditorGUILayout.EnumPopup("尺寸:", textureSize); textureCompression = (TextureImporterCompression)EditorGUILayout.EnumPopup("压缩:", textureCompression); ifAllowsAlphaSplitting = EditorGUILayout.Toggle("是否允许透明分离:", ifAllowsAlphaSplitting); ifMipmapEnabled = EditorGUILayout.Toggle("是否允许Mipmap:", ifMipmapEnabled); over_ride = EditorGUILayout.Toggle("是否允许Override:", this.over_ride); EditorGUILayout.Space(); if (GUILayout.Button("设置")) { TextureImporterPlatformSettings t = new TextureImporterPlatformSettings(); t.allowsAlphaSplitting = ifAllowsAlphaSplitting; t.overridden = over_ride; t.format = textureFormat; t.maxTextureSize = (int)textureSize; t.textureCompression = textureCompression; this.ChangeTextureFormatSettings(t); } }
void setTextureReadable(string texPath, TextureImporterFormat format, TextureImporterCompression compression, bool val, bool importNow = true) { TextureImporter textureImporter = AssetImporter.GetAtPath(texPath) as TextureImporter; textureImporter.isReadable = val; //textureImporter.textureFormat = format; textureImporter.textureCompression = compression; TextureImporterPlatformSettings texSettings = new TextureImporterPlatformSettings(); texSettings.format = format; texSettings.maxTextureSize = 4096; texSettings.allowsAlphaSplitting = true; texSettings.compressionQuality = 100; #if UNITY_ANDROID texSettings.name = "Android"; //textureImporter.SetPlatformTextureSettings ("Android", 4096, format, 100, true); #elif UNITY_IPHONE || UNITY_IOS texSettings.name = "iPhone"; //textureImporter.SetPlatformTextureSettings("iPhone", 4096, format, 100, true); #else texSettings.name = "Standalone"; //textureImporter.SetPlatformTextureSettings("Standalone", 4096, format, 100, true); #endif textureImporter.SetPlatformTextureSettings(texSettings); EditorUtility.SetDirty(textureImporter); textureImporter.SaveAndReimport(); if (importNow) { AssetDatabase.ImportAsset(texPath); } }
public static string SetCompression(this Texture2D tex, TextureImporterCompression compression = TextureImporterCompression.Compressed) { string path = AssetDatabase.GetAssetPath(tex); SetCompression(path, compression); return(path); }
private void MakePowerOf4(Texture2D tex) { string texPath = AssetDatabase.GetAssetPath(tex); TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(texPath); bool crunch = importer.crunchedCompression = false; TextureImporterCompression compression = importer.textureCompression; bool readable = importer.isReadable; importer.crunchedCompression = false; importer.textureCompression = TextureImporterCompression.Uncompressed; importer.isReadable = true; importer.SaveAndReimport(); int width = tex.width; int height = tex.height; while (width % 4 != 0) { width++; } while (height % 4 != 0) { height++; } Texture2D newTex = new Texture2D(width, height); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { newTex.SetPixel(x, y, new Color(0, 0, 0, 0)); } } for (int x = 0; x < tex.width; x++) { for (int y = 0; y < tex.height; y++) { Color col = tex.GetPixel(x, y); newTex.SetPixel(x, y, col); } } newTex.Apply(); string path = AssetDatabase.GetAssetPath(tex); string absolutePath = Application.dataPath + "/" + path.Replace("Assets/", ""); System.IO.File.WriteAllBytes(absolutePath, newTex.EncodeToPNG()); importer.crunchedCompression = crunch; importer.textureCompression = compression; importer.isReadable = readable; importer.SaveAndReimport(); AssetDatabase.Refresh(); }
public void SetTextureCompressionForAll(TextureImporterCompression textureCompression) { Debug.Assert(allAreOverridden, "Attempting to set texture compression for all platforms even though settings are not overridden for all platforms."); m_PlatformSettings.textureCompression = textureCompression; m_TextureCompressionIsDifferent = false; m_HasChanged = true; }
public static void SetCompression(string path, TextureImporterCompression compression = TextureImporterCompression.Compressed) { if (!string.IsNullOrEmpty(path)) { var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; if (textureImporter != null) { #if UNITY_ANDROID var textureImporterPlatformSetting = textureImporter.GetPlatformTextureSettings("Android"); textureImporterPlatformSetting.format = textureImporter.DoesSourceTextureHaveAlpha() ? TextureImporterFormat.ETC2_RGBA8 : TextureImporterFormat.ETC2_RGB4; textureImporter.SetPlatformTextureSettings(textureImporterPlatformSetting); textureImporter.SaveAndReimport(); #elif UNITY_IOS var textureImporterPlatformSetting = textureImporter.GetPlatformTextureSettings("iPhone"); textureImporterPlatformSetting.format = textureImporter.DoesSourceTextureHaveAlpha() ? TextureImporterFormat.ASTC_RGBA_4x4 : TextureImporterFormat.ASTC_RGB_4x4; textureImporter.SetPlatformTextureSettings(textureImporterPlatformSetting); textureImporter.SaveAndReimport(); #else textureImporter.textureCompression = compression; textureImporter.SaveAndReimport(); #endif } } }
public static Texture2D SaveAsPng(this Texture2D texture, string path, TextureImporterType textureType = TextureImporterType.Default, TextureImporterCompression compression = TextureImporterCompression.Uncompressed, bool generateMipmaps = false, bool destroyInitialTextureObject = true) { var wrapMode = texture.wrapMode; var alphaIsTransparency = texture.alphaIsTransparency; var maxSize = Mathf.Max(texture.width, texture.height); path = $"{path.GetBeforeLast("/")}/{texture.name}.png"; Debug.Assert(AssetDatabase.IsValidFolder(path.GetBefore("/"))); var bytes = texture.EncodeToPNG(); using (var fileStream = System.IO.File.Create(path)) fileStream.Write(bytes, 0, bytes.Length); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; textureImporter.textureType = textureType; textureImporter.alphaIsTransparency = alphaIsTransparency; textureImporter.wrapMode = wrapMode; textureImporter.mipmapEnabled = generateMipmaps; textureImporter.textureCompression = compression; textureImporter.maxTextureSize = maxSize; AssetDatabase.ImportAsset(path); if (destroyInitialTextureObject) { Object.DestroyImmediate(texture); } return(AssetDatabase.LoadAssetAtPath <Texture2D>(path)); }
//------------------------------------------------------------ // テクスチャの属性を設定する(読み出し・書き込み) private void SetTextureSetting(string tPath, bool tReadable, ref TextureImporterCompression rFormat, ref TextureImporterNPOTScale rNPOTScale) { TextureImporter tTextureImporter = AssetImporter.GetAtPath(tPath) as TextureImporter; if (tReadable == true) { // 読み込み許可 rFormat = tTextureImporter.textureCompression; rNPOTScale = tTextureImporter.npotScale; // 強制的に ARGB32 NPOT に変える必要がある tTextureImporter.textureCompression = TextureImporterCompression.Uncompressed; tTextureImporter.npotScale = TextureImporterNPOTScale.None; } else { // 読み込み禁止 tTextureImporter.textureCompression = rFormat; tTextureImporter.npotScale = rNPOTScale; } tTextureImporter.isReadable = tReadable; AssetDatabase.ImportAsset(tPath, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport); }
public static void SelectedChangeTextureFormatSettings(TextureImporterCompression format) { Object[] textures = GetSelectedTextures(); foreach (Texture2D tex in textures) { SetTexTextureFormatSettings(tex, format); } }
public static void SetTexTextureFormatSettings(Texture2D tex, TextureImporterCompression format) { string path = AssetDatabase.GetAssetPath(tex); TextureImporter teximp = AssetImporter.GetAtPath(path) as TextureImporter; teximp.textureCompression = format; AssetDatabase.ImportAsset(path); }
public TextureFormatInfo(TextureImporterCompression comp, string p, TextureImporterFormat pf, bool isNormMap) { compression = comp; platform = p; platformFormat = pf; platformCompressionQuality = 0; this.isNormalMap = isNormMap; }
public virtual TextureImporterCompression DrawCompression(TextureImporterCompression defaultValue, bool isMixedValue, out bool changed) { EditorGUI.BeginChangeCheck(); EditorGUI.showMixedValue = isMixedValue; defaultValue = (TextureImporterCompression)EditorGUILayout.IntPopup(s_Styles.compressionLabel, (int)defaultValue, s_Styles.kTextureCompressionOptions, s_Styles.kTextureCompressionValues); EditorGUI.showMixedValue = false; changed = EditorGUI.EndChangeCheck(); return(defaultValue); }
/// <summary> /// On GUI /// </summary> void OnGUI() { if (rerun == false) { UIDesign(); } Color oldColor = GUI.backgroundColor; GUI.backgroundColor = LightPrimaryColor; GUILayout.Label("BattlePhaze Texture Importer", TextLargeStyling); GUI.backgroundColor = oldColor; EditorGUILayout.BeginHorizontal(); GUILayout.Label("Mip Maps", TextStyling); MipMapEnabled = EditorGUILayout.Toggle(MipMapEnabled); GUILayout.EndVertical(); EditorGUILayout.BeginHorizontal(); GUILayout.Label("Crunch Compression", TextStyling); CrunchCompression = EditorGUILayout.Toggle(CrunchCompression); GUILayout.EndVertical(); #if !UNITY_2017 EditorGUILayout.BeginHorizontal(); GUILayout.Label("Streaming MipMap", TextStyling); StreamingMipmaps = EditorGUILayout.Toggle(StreamingMipmaps); GUILayout.EndVertical(); #endif EditorGUILayout.BeginHorizontal(); GUILayout.Label("Texture Max Size", TextStyling); TextureMaxSize = EditorGUILayout.IntField(TextureMaxSize); GUILayout.EndVertical(); EditorGUILayout.BeginHorizontal(); GUILayout.Label("Texture Compression", TextStyling); TextureCompression = (TextureImporterCompression)EditorGUILayout.EnumPopup(TextureCompression, EnumStyling); GUILayout.EndVertical(); GUI.backgroundColor = AccentColor; if (GUILayout.Button("Convert Textures", StyleButton)) { IgnoreLightmaps(); TextureConvert(); } if (GUILayout.Button("Convert Skinned Mesh Renders Textures", StyleButton)) { IgnoreLightmaps(); ConvertSkinnedMeshRenders(); } if (GUILayout.Button("Convert Skybox", StyleButton)) { IgnoreLightmaps(); SceneRender(); } if (GUILayout.Button("Convert Particle System", StyleButton)) { ParticleConvert(); } GUI.backgroundColor = oldColor; }
static void UpdateCompress(TextureImporterCompression compress) { TextureProcess((textureImporter) => { if (textureImporter.textureCompression != compress) { textureImporter.textureCompression = compress; return(true); } return(true); }); }
void OnGUI() { IsCrunch = EditorGUILayout.Toggle("Is Crunched ?", IsCrunch); compression = (TextureImporterCompression)EditorGUILayout.EnumPopup("Compression:", compression); KeepUncompressed = EditorGUILayout.Toggle("Keep Uncompressed ?", KeepUncompressed); if (GUILayout.Button("Begin")) { Routine = Import(); } EditorGUILayout.LabelField("Progress (%): ", Percent.ToString("0.00")); }
public static void SetCompression(this Texture2D texture, TextureImporterCompression compression) { TextureImporter tempImporter = (TextureImporter)AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture)); if (tempImporter != null) { tempImporter.textureType = TextureImporterType.Default; tempImporter.textureCompression = compression; AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(texture)); AssetDatabase.Refresh(); } }
public static bool getPixelsFromTexture(ref Texture2D texture, out Color[] pixels) { //Make texture readable TextureImporter im = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture)) as TextureImporter; if (!im) { pixels = new Color[1]; return(false); } bool readable = im.isReadable; TextureImporterCompression format = im.textureCompression; TextureImporterType type = im.textureType; bool isConvertedBump = im.convertToNormalmap; bool srgb = im.sRGBTexture; im.sRGBTexture = false; if (!readable) { im.isReadable = true; } if (type != TextureImporterType.Default) { im.textureType = TextureImporterType.Default; } im.textureCompression = TextureImporterCompression.Uncompressed; im.SaveAndReimport(); pixels = texture.GetPixels(); if (!readable) { im.isReadable = false; } if (type != TextureImporterType.Default) { im.textureType = type; } if (isConvertedBump) { im.convertToNormalmap = true; } im.sRGBTexture = srgb; im.textureCompression = format; im.SaveAndReimport(); return(true); }
private void Apply(TextureImporter importer, FilterMode filterMode, TextureImporterCompression compression) { if (importer.filterMode != filterMode || importer.textureCompression != compression) { importer.filterMode = filterMode; importer.textureCompression = compression; importer.alphaIsTransparency = true; importer.wrapMode = TextureWrapMode.Repeat; importer.mipmapEnabled = false; // Reimport. AssetDatabase.ImportAsset(importer.assetPath); } }
public void DrawGui() { GUILayout.BeginVertical(GUI.skin.box); using (new GUILayout.HorizontalScope()) { EditorGUILayout.TextField("Path to store atlases", _atlasesPath); if (GUILayout.Button("Select", GUILayout.ExpandWidth(false))) { _atlasesPath = EditorUtility.OpenFolderPanel("Select folder", _atlasesPath, ""); _atlasesPath = _atlasesPath.Substring(_atlasesPath.IndexOf("Assets", StringComparison.Ordinal)); } } _format = (TextureImporterFormat)EditorGUILayout.EnumPopup("Texture format", _format); _compression = (TextureImporterCompression)EditorGUILayout.EnumPopup("Texture compression", _compression); if (GUILayout.Button("Apply compression to ALL textures in project")) { if (EditorUtility.DisplayDialog("Warning!", "Are you sure you want to change all textures compression settings? This action cannot be undone!", "You bet!", "Hell, no!")) { var guids = AssetDatabase.FindAssets("t:Sprite"); var paths = guids.Select(AssetDatabase.GUIDToAssetPath).ToArray(); for (var i = 0; i < paths.Length; i++) { var path = paths[i]; if (!EditorUtility.DisplayCancelableProgressBar("Operation in progress", "Applying settings: " + Path.GetFileNameWithoutExtension(path), (float)i / paths.Length)) { TextureUtils.ApplyTextureCompression(path, Format, _compression); } else { EditorUtility.ClearProgressBar(); break; } } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); } } GUILayout.EndVertical(); }
public static void ApplyTextureCompression(string path, TextureImporterFormat format, TextureImporterCompression compression) { var importer = AssetImporter.GetAtPath(path) as TextureImporter; if (importer != null && importer.textureType == TextureImporterType.Sprite) { var def = importer.GetDefaultPlatformTextureSettings(); def.textureCompression = compression; def.format = format; importer.ClearPlatformTextureSettings("iPhone"); importer.ClearPlatformTextureSettings("Android"); importer.SetPlatformTextureSettings(def); EditorUtility.SetDirty(importer); AssetDatabase.WriteImportSettingsIfDirty(path); } }
public override void ApplyDefaults() { this.RuleName = "New Texture Rule"; this.SuffixFilter = ".png"; textureType = TextureImporterType.Default; isReadable = false; mipmapEnabled = false; allowsAlphaSplitting = false; npotScale = TextureImporterNPOTScale.None; wrapMode = TextureWrapMode.Clamp; filterMode = FilterMode.Bilinear; textureCompression = TextureImporterCompression.Compressed; }
public static void DoActionForTexture(ref Texture2D tex, Action <Texture2D> action) { TextureImporter im = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(tex)) as TextureImporter; if (!im) { action(tex); return; } bool readable = im.isReadable; TextureImporterCompression format = im.textureCompression; TextureImporterType type = im.textureType; bool isConvertedBump = im.convertToNormalmap; if (!readable) { im.isReadable = true; } if (type != TextureImporterType.Default) { im.textureType = TextureImporterType.Default; } im.textureCompression = TextureImporterCompression.Uncompressed; im.SaveAndReimport(); action(tex); if (!readable) { im.isReadable = false; } if (type != TextureImporterType.Default) { im.textureType = type; } if (isConvertedBump) { im.convertToNormalmap = true; } im.textureCompression = format; im.SaveAndReimport(); }
byte[] GetTextureToPNGByteArray(Texture texture, bool isReimportTexture) { #if UNITY_EDITOR if (texture == null) { return(null); } TextureImporterFormat oldImporterFormat = new TextureImporterFormat(); bool oldReadable = false; TextureImporterCompression oldCompression = TextureImporterCompression.Uncompressed; if (isReimportTexture) { //Change texture readable flag from Texture Importer string pass = AssetDatabase.GetAssetPath(texture); TextureImporter ti = TextureImporter.GetAtPath(pass) as TextureImporter; oldReadable = ti.isReadable; oldImporterFormat = ti.textureFormat; oldCompression = ti.textureCompression; ti.isReadable = true; ti.textureFormat = TextureImporterFormat.RGBA32; ti.textureCompression = TextureImporterCompression.Uncompressed; AssetDatabase.ImportAsset(pass); } //Convert the texture to raw PNG data Texture2D tex = texture as Texture2D; byte[] data = tex.EncodeToPNG(); if (isReimportTexture) { //Revert texture readable flag string pass = AssetDatabase.GetAssetPath(texture); TextureImporter ti = TextureImporter.GetAtPath(pass) as TextureImporter; ti.isReadable = oldReadable; ti.textureFormat = oldImporterFormat; ti.textureCompression = oldCompression; AssetDatabase.ImportAsset(pass); } return(data); #else return(null); #endif }
public void OnGUI() { EditorGUILayout.LabelField("This tool is designed for handling sprite collection import settings.", new GUIStyle(EditorStyles.label) { normal = { textColor = Color.green } }); SpritesFolder = EditorGUILayout.ObjectField(new GUIContent("Sprites (Folder):", "This should be sprites root folder."), SpritesFolder, typeof(UnityEngine.Object), false); ForceSingle = EditorGUILayout.Toggle(new GUIContent("Set Sprite Mode To Single:", "Check to override sprite mode to Single (this may break a layout)."), ForceSingle); ForceFullRect = EditorGUILayout.Toggle(new GUIContent("Set Mesh Type = Full Rect:", "Check to override mesh @type to Full Rect."), ForceFullRect); EnableReadWrite = EditorGUILayout.Toggle(new GUIContent("Enable Read/Write:", ""), EnableReadWrite); PackingTag = EditorGUILayout.TextField(new GUIContent("Set Packing Tag:", ""), PackingTag); FilterMode = EditorGUILayout.TextField(new GUIContent("Filter Mode:", ""), FilterMode.ToString()).ToEnum <FilterMode>(); MaxTextureSize = EditorGUILayout.IntField(new GUIContent("Texture size (32, 64...):", ""), MaxTextureSize); Compression = (TextureImporterCompression)EditorGUILayout.Popup(new GUIContent("Compression:", ""), (int)Compression, Enum.GetValues(typeof(TextureImporterCompression)).Cast <TextureImporterCompression>().Select(i => new GUIContent(i.ToString())).ToArray()); CrunchedCompression = EditorGUILayout.Toggle(new GUIContent("Use Crunch Compression:", ""), CrunchedCompression); CompressionQuality = EditorGUILayout.IntSlider(new GUIContent("Compressor Quality:", ""), CompressionQuality, 0, 100); if (GUILayout.Button("Setup")) { if (SpritesFolder == null) { Debug.LogWarning("SpritesFolder is null"); } else { var root = AssetDatabase.GetAssetPath(SpritesFolder); var files = Directory.GetFiles(root, "*.png", SearchOption.AllDirectories).Union(Directory.GetFiles(root, "*.psd", SearchOption.AllDirectories)).ToList(); for (var i = 0; i < files.Count; i++) { var progress = (float)i / files.Count; SetImportSettings(files[i], ForceSingle, ForceFullRect, PackingTag, EnableReadWrite, FilterMode, MaxTextureSize, Compression, CrunchedCompression, CompressionQuality); if (EditorUtility.DisplayCancelableProgressBar("Processing sprites", $"[{(int)(100 * progress)}%] [{i}/{files.Count}] Processing {files[i]}", progress)) { break; } } EditorUtility.ClearProgressBar(); } } }
public static TextureImporterPlatformSettings GetIOSStandardImportSetting(TextureImporter importer, TextureType type, bool hasAlpha) { TextureImporterCompression compression = TextureImporterCompression.Compressed; var format = hasAlpha ? TextureImporterFormat.ASTC_RGBA_6x6 : TextureImporterFormat.ASTC_RGB_6x6; TextureResizeAlgorithm resizeAlgorithm = TextureResizeAlgorithm.Mitchell; var ti = new TextureImporterPlatformSettings { name = "iPhone", overridden = true, format = format, textureCompression = compression, maxTextureSize = textureMaxSize(type), compressionQuality = GetTextureCompressQuality(), resizeAlgorithm = resizeAlgorithm }; return(ti); }
// Apply settings Method which searches and adds all texture files in an array, and for each file, adds the settings, overrides it for the selected platform // and imports the newly applied asset again, therefore applying all the settings properly. static void ApplySettings(int maxSize, int anisoLevel, bool alphaSplitting, TextureImporterCompression compressionType, string platform) { Object[] textures = GetSelectedTextures(); Selection.objects = new Object[0]; foreach (Texture texture in textures) { string path = AssetDatabase.GetAssetPath(texture); TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; TextureImporterPlatformSettings texturesettings = textureImporter.GetDefaultPlatformTextureSettings(); texturesettings.maxTextureSize = maxSize; texturesettings.name = platform; texturesettings.overridden = true; texturesettings.textureCompression = compressionType; texturesettings.allowsAlphaSplitting = alphaSplitting; textureImporter.anisoLevel = anisoLevel; textureImporter.SetPlatformTextureSettings(texturesettings); AssetDatabase.ImportAsset(path); } }
void Generate() { if (destination == null) { destination = new Texture2D(source.width, source.height, TextureFormat.ARGB32, false); destination.hideFlags = HideFlags.HideAndDontSave; } string path = AssetDatabase.GetAssetPath(source); TextureImporter importer = TextureImporter.GetAtPath(path) as TextureImporter; if (importer == null) { Debug.LogError("Cannot work with built-in textures."); return; } if (importer.crunchedCompression) { Debug.LogError("You have to disable crunch compression while generating the SDF texture."); return; } bool isReadble = importer.isReadable; TextureImporterCompression compression = importer.textureCompression; bool uncompressed = compression == TextureImporterCompression.Uncompressed; if (!isReadble || !uncompressed) { importer.isReadable = true; importer.textureCompression = TextureImporterCompression.Uncompressed; AssetDatabase.ImportAsset(path); } SDFTextureGenerator.Generate( source, destination, insideDistance, outsideDistance, postProcessDistance, rgbFillMode); if (!isReadble || !uncompressed) { importer.isReadable = isReadble; importer.textureCompression = compression; AssetDatabase.ImportAsset(path); } destination.Apply(); allowSave = true; }
static TextureData TextureToTextureData(Texture2D input, ref TextureFormat inputFormat) { // Modify input texture import settings temporarily string texpath = AssetDatabase.GetAssetPath(input); TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(texpath); TextureImporterCompression prev = importer.textureCompression; TextureImporterType prevType = importer.textureType; bool linearInput = importer.sRGBTexture == false || importer.textureType == TextureImporterType.NormalMap; bool prevReadable = importer.isReadable; if (importer != null) { importer.textureType = TextureImporterType.Default; importer.isReadable = true; importer.textureCompression = TextureImporterCompression.Uncompressed; AssetDatabase.ImportAsset(texpath, ImportAssetOptions.ForceUpdate); inputFormat = input.format; } // Copy input texture pixel data Color[] colors = input.GetPixels(); TextureData res = new TextureData(input.width, input.height); for (int x = 0; x < res.width; x++) { for (int y = 0; y < res.height; y++) { res.SetColorAt(x, y, linearInput || PlayerSettings.colorSpace == ColorSpace.Gamma ? colors[y * res.width + x] : colors[y * res.width + x].linear); } } // Revert input texture settings if (importer != null) { importer.textureType = prevType; importer.isReadable = prevReadable; importer.textureCompression = prev; AssetDatabase.ImportAsset(texpath, ImportAssetOptions.ForceUpdate); } return(res); }