// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using System; using System.Collections.Generic; using System.IO; using System.Linq; using Doozy.Runtime.Common.Utils; using UnityEditor; using UnityEngine; using Debug = System.Diagnostics.Debug; namespace Doozy.Editor.EditorUI.Utils { public static class TextureUtils { /// Get a Texture2D found at the target file path /// Target file path public static Texture2D GetTexture2D(string filePath) => AssetDatabase.LoadAssetAtPath(PathUtils.ToRelativePath(filePath)); /// Get a sorted (by filename) list of Texture2D for all the .png files found at the target folderPath /// Target folder path public static List GetTextures2D(string folderPath) { string[] paths = Directory.GetFiles(folderPath, "*.png"); //get the paths for all the png files in the target folder Array.Sort(paths); for (int i = 0; i < paths.Length; i++) paths[i] = PathUtils.ToRelativePath(paths[i]); return paths.Select(AssetDatabase.LoadAssetAtPath).ToList(); } /// /// Set the textures at the target paths to have the following settings: /// textureType = TextureImporterType.GUI /// mipmapEnabled = true /// filterMode = FilterMode.Trilinear /// textureCompression = TextureImporterCompression.Uncompressed /// /// Example of file path 'Assets/MyFolderName/MyTextureName.png' /// public static void SetTextureSettingsToGUI(IEnumerable filePaths, bool startStopAssetEditing = true) { if (filePaths == null) return; if (startStopAssetEditing) AssetDatabase.StartAssetEditing(); foreach (string filePath in filePaths) { SetTextureSettingsToGUI(filePath); } if (startStopAssetEditing) AssetDatabase.StopAssetEditing(); } /// /// Set the texture at the target path to have the following settings: /// textureType = TextureImporterType.GUI /// mipmapEnabled = true /// filterMode = FilterMode.Trilinear /// textureCompression = TextureImporterCompression.Uncompressed /// /// Texture file path 'Assets/MyFolderName/MyTextureName.png' public static void SetTextureSettingsToGUI(string filePath) { filePath = PathUtils.ToRelativePath(filePath); var textureImporter = AssetImporter.GetAtPath(filePath) as TextureImporter; if (textureImporter == null) return; Debug.Assert(textureImporter != null, nameof(textureImporter) + " != null"); bool requiresImport = false; if (textureImporter.textureType != TextureImporterType.GUI) { textureImporter.textureType = TextureImporterType.GUI; requiresImport = true; } if (textureImporter.mipmapEnabled != true) { textureImporter.mipmapEnabled = true; requiresImport = true; } if (textureImporter.filterMode != FilterMode.Trilinear) { textureImporter.filterMode = FilterMode.Trilinear; requiresImport = true; } if (textureImporter.textureCompression != TextureImporterCompression.Uncompressed) { textureImporter.textureCompression = TextureImporterCompression.Uncompressed; requiresImport = true; } if (!requiresImport) return; UnityEngine.Debug.Log($"Importing: {filePath}"); AssetDatabase.ImportAsset(filePath, ImportAssetOptions.ForceUpdate); } /// /// Set the texture at the target path to have the following settings: /// textureType = TextureImporterType.Sprite /// spriteImportMode = SpriteImportMode.Multiple /// isReadable = true (Read/Write Enabled) /// spriteMeshType = SpriteMeshType.FullRect /// spriteExtrude = 0 /// spriteGenerateFallbackPhysicsShape = false /// /// Example of file path 'Assets/MyFolderName/MySpriteSheetName.png' /// public static void SetTextureSettingsToSpriteSheet(IEnumerable filePaths, bool startStopAssetEditing = true) { if (filePaths == null) return; if (startStopAssetEditing) AssetDatabase.StartAssetEditing(); foreach (string filePath in filePaths) SetTextureSettingsToSpriteSheet(filePath); if (startStopAssetEditing) AssetDatabase.StopAssetEditing(); } /// /// Set the texture at the target path to have the following settings: /// textureType = TextureImporterType.Sprite /// spriteImportMode = SpriteImportMode.Multiple /// isReadable = true (Read/Write Enabled) /// spriteMeshType = SpriteMeshType.FullRect /// spriteExtrude = 0 /// spriteGenerateFallbackPhysicsShape = false /// /// SpriteSheet file path 'Assets/MyFolderName/MySpriteSheetName.png' public static void SetTextureSettingsToSpriteSheet(string filePath) { if (filePath.StartsWith(Application.dataPath)) filePath = "Assets" + filePath.Substring(Application.dataPath.Length); var textureImporter = AssetImporter.GetAtPath(filePath) as TextureImporter; if (textureImporter == null) throw new NullReferenceException($"Could not load TextureImporter for '{filePath}'"); bool isDirty = false; if (textureImporter.textureType != TextureImporterType.Sprite) { textureImporter.textureType = TextureImporterType.Sprite; isDirty = true; } if (textureImporter.spriteImportMode != SpriteImportMode.Multiple) { textureImporter.spriteImportMode = SpriteImportMode.Multiple; isDirty = true; } if (textureImporter.isReadable == false) { textureImporter.isReadable = true; isDirty = true; } var textureSettings = new TextureImporterSettings(); textureImporter.ReadTextureSettings(textureSettings); if (textureSettings.spriteMeshType != SpriteMeshType.FullRect) { textureSettings.spriteMeshType = SpriteMeshType.FullRect; isDirty = true; } if (textureSettings.spriteExtrude != 0) { textureSettings.spriteExtrude = 0; isDirty = true; } if (textureSettings.spriteGenerateFallbackPhysicsShape) { textureSettings.spriteGenerateFallbackPhysicsShape = false; isDirty = true; } textureImporter.SetTextureSettings(textureSettings); if (!isDirty) return; Texture2D asset = AssetDatabase.LoadAssetAtPath(filePath); EditorUtility.SetDirty(asset); AssetDatabase.SaveAssetIfDirty(asset); AssetDatabase.ImportAsset(filePath, ImportAssetOptions.ForceUpdate); } } }