Created
April 27, 2026 12:52
-
-
Save aras-p/5b5897296a35421d9f4b5fb6287657f6 to your computer and use it in GitHub Desktop.
Unity Texture2D Custom Mipmap Importer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using UnityEditor; | |
| using System.IO; | |
| using UnityEditor.AssetImporters; | |
| [ScriptedImporter(2026_04_27, kFileExtension)] | |
| public class CustomMipImporter : ScriptedImporter | |
| { | |
| public Texture2D[] m_Mips; | |
| public TextureFormat m_Format = TextureFormat.BC7; | |
| private const string kFileExtension = "tex2dmips"; | |
| public override void OnImportAsset(AssetImportContext ctx) | |
| { | |
| foreach (var tex in m_Mips) | |
| { | |
| if (tex != null) | |
| { | |
| var path = AssetDatabase.GetAssetPath(tex); | |
| ctx.DependsOnArtifact(path); | |
| } | |
| } | |
| var width = m_Mips.Length > 0 ? m_Mips[0].width : 64; | |
| var height = m_Mips.Length > 0 ? m_Mips[0].height : 64; | |
| var format = TextureFormat.RGBA32; | |
| Texture2D result = new Texture2D(width, height, format, m_Mips.Length, false); | |
| for (var mip = 0; mip < m_Mips.Length; ++mip) | |
| { | |
| Color32[] pixels = m_Mips[mip].GetPixels32(); | |
| result.SetPixels32(pixels, mip); | |
| } | |
| result.Apply(false, false); | |
| EditorUtility.CompressTexture(result, m_Format, TextureCompressionQuality.Normal); | |
| ctx.AddObjectToAsset("Tex2DMips", result); | |
| ctx.SetMainObject(result); | |
| } | |
| [MenuItem("Assets/Create/Rendering/Tex2D Custom Mips")] | |
| static void CreateCustomMipsMenuItem() | |
| { | |
| string folder = "Assets"; | |
| foreach (Object obj in Selection.GetFiltered(typeof(Object), SelectionMode.Assets)) | |
| { | |
| folder = AssetDatabase.GetAssetPath(obj); | |
| if (!string.IsNullOrEmpty(folder) && File.Exists(folder)) | |
| { | |
| folder = Path.GetDirectoryName(folder); | |
| break; | |
| } | |
| } | |
| folder = folder.Replace("\\", "/"); | |
| if (folder.Length > 0 && folder[^1] != '/') | |
| folder += "/"; | |
| if (string.IsNullOrEmpty(folder)) | |
| folder = "Assets/"; | |
| var fileName = $"New Tex2DCustomMips.{kFileExtension}"; | |
| folder = AssetDatabase.GenerateUniqueAssetPath(folder + fileName); | |
| ProjectWindowUtil.CreateAssetWithContent(folder, "This is a CustomMips Asset"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment