Last active
January 4, 2020 05:21
-
-
Save but80/d36d369c641bebf63dcdd6f392652ed3 to your computer and use it in GitHub Desktop.
Unity上の画像アセットに対して自動生成されたMipMapを個別の画像ファイルとして抽出するエディタ拡張です。各画像に手を加えた後 http://baba-s.hatenablog.com/entry/2019/06/04/084000 で再読込し、1枚のテクスチャとして使用できます
This file contains 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 System; | |
using System.IO; | |
using UnityEngine; | |
using UnityEditor; | |
public class MipmapExtractor : EditorWindow | |
{ | |
[Serializable] | |
public class MipmapExtractorParams : ScriptableObject | |
{ | |
[SerializeField] | |
private Texture2D _sourceTexture; | |
public Texture2D SourceTexture | |
{ | |
get { return _sourceTexture; } | |
set { _sourceTexture = value; } | |
} | |
} | |
private MipmapExtractorParams _params; | |
[MenuItem("Assets/Mipmap Extractor")] | |
private static void Create() | |
{ | |
var window = GetWindow<MipmapExtractor>("MipmapExtractor"); | |
window.minSize = new Vector2(320, 320); | |
} | |
private void OnGUI() | |
{ | |
if (_params == null) | |
{ | |
_params = MipmapExtractorParams.CreateInstance<MipmapExtractorParams>(); | |
} | |
using (new GUILayout.HorizontalScope()) | |
{ | |
using (new GUILayout.VerticalScope()) | |
{ | |
EditorGUILayout.LabelField("Source Texture"); | |
_params.SourceTexture = (Texture2D)EditorGUILayout.ObjectField(_params.SourceTexture, typeof(Texture2D), false, GUILayout.Width(70), GUILayout.Height(70)); | |
} | |
} | |
using (new GUILayout.HorizontalScope()) | |
{ | |
if (GUILayout.Button("Extract")) | |
{ | |
if (!_params.SourceTexture.isReadable) | |
{ | |
EditorUtility.DisplayDialog("Error", "Check \"Read / Write Enabled\" of the texture and apply the change, then try again.", "OK"); | |
return; | |
} | |
Extract(); | |
} | |
} | |
} | |
private void Extract() | |
{ | |
var src = _params.SourceTexture; | |
var srcPath = AssetDatabase.GetAssetPath(src); | |
var dir = Path.GetDirectoryName(srcPath); | |
var prefix = Path.GetFileNameWithoutExtension(srcPath); | |
var pathFormat = "{0}/{1}.mip{2}.png"; | |
var w = src.width; | |
var h = src.height; | |
for (int i=1; i<src.mipmapCount; i++) { | |
w >>= 1; | |
h >>= 1; | |
var dst = new Texture2D(w, h); | |
dst.SetPixels(src.GetPixels(i)); | |
var path = String.Format(pathFormat, dir, prefix, i); | |
File.WriteAllBytes(path, dst.EncodeToPNG()); | |
} | |
AssetDatabase.Refresh(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment