Skip to content

Instantly share code, notes, and snippets.

@nekochanfood
Last active July 29, 2022 13:29
Show Gist options
  • Save nekochanfood/cf14d01622da467617f9b150f4818d3e to your computer and use it in GitHub Desktop.
Save nekochanfood/cf14d01622da467617f9b150f4818d3e to your computer and use it in GitHub Desktop.
Texture2dをPNGに変換するスクリプト
//Texture2dをTexture[]に設定し、...から"Convert to PNG"で変換することができます
//UnityEditorでの使用を想定
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.IO;
public class Texture2dToPNG : MonoBehaviour
{
[SerializeField]Texture2D[] texture;
[Space(20)]
[SerializeField]string _path;
[SerializeField]string FileName;
int i,o;
Texture2D createReadabeTexture2D(Texture2D texture2d)
{
RenderTexture renderTexture = RenderTexture.GetTemporary(
texture2d.width,
texture2d.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(texture2d, renderTexture);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = renderTexture;
Texture2D readableTextur2D = new Texture2D(texture2d.width, texture2d.height);
readableTextur2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
readableTextur2D.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(renderTexture);
return readableTextur2D;
}
[ContextMenu("Convert to PNG")]void toPNG(){Convert(0);}
[ContextMenu("Convert to JPEG")]void toJPEG(){Convert(1);}
[ContextMenu("Convert to TGA")]void toEXR(){Convert(2);}
void Convert(int Format)
{
string[] _missing = new string[texture.Length];
string missing = "0, ";
string notmissing = "";
bool firstmissing = false;
bool UseTexture2dName = false;
byte[] bytes = null;
string _format = "";
for(i=0;i!=texture.Length;i++)
{
if(texture[i] == null)
{
for(o=0;o!=_missing.Length;o++)
{
if(_missing[o] == "")
{
_missing[o] = i.ToString();
break;
}
}
if(!firstmissing)
{
firstmissing = true;
missing = "";
}
missing = missing + i.ToString() + ", ";
}else
{
notmissing = notmissing + i.ToString() + ", ";
}
}
if(missing != "")
{
missing = missing.Remove((missing.Length - 2));
if(missing == "0" && notmissing != ""){
}else
{
if(notmissing != "")
{
notmissing = notmissing.Remove((notmissing.Length - 2));
bool ConvertOnlyNotMissing = EditorUtility.DisplayDialog("確認","Texture[" + missing + "]にTexture2dが設定されていませんが、\nTexture[" + notmissing + "]にはTexture2dが設定されています。このまま実行しますか?", "はい","いいえ");
if(!ConvertOnlyNotMissing)return;
}else
{
EditorUtility.DisplayDialog("エラー","Texture[" + missing + "]にTexture2dが設定されていません。", "了解");
Debug.LogError("Texture[" + missing + "]にTexture2dが設定されていません。");
return;
}
}
}
string DateAndTime = System.DateTime.Now.ToString().Replace("/","_").Replace(":",".").Replace(" ","-");
_path = _path.Replace("<date>",DateAndTime);
if(_path == "")
{
_path = EditorUtility.OpenFolderPanel("フォルダを指定してください","","");
}else if(!Directory.Exists(_path))
{
bool CreateFolder = EditorUtility.DisplayDialog("確認","選択されたパスは存在しません。フォルダを作成しますか?","はい","いいえ");
if(CreateFolder)
{
Directory.CreateDirectory(_path);
}else
{
return;
}
}
if( _path.EndsWith("/") || _path.EndsWith("\\") ) _path = _path.Remove((_path.Length - 1));
bool skipConvert = false;
for(i=0;i!=texture.Length;i++)
{
if(FileName == "") UseTexture2dName = true;
if(texture[i] == null) skipConvert = true;
if(!skipConvert)
{
switch(Format)
{
case 0:
bytes = createReadabeTexture2D(texture[i]).EncodeToPNG();
_format = ".png";
break;
case 1:
bytes = createReadabeTexture2D(texture[i]).EncodeToJPG();
_format = ".jpeg";
break;
case 2:
bytes = createReadabeTexture2D(texture[i]).EncodeToTGA();
_format = ".tga";
break;
default:
_format = "";
break;
}
string size = createReadabeTexture2D(texture[i]).height + "x" + createReadabeTexture2D(texture[i]).width;
string path = _path + "/";
if(UseTexture2dName) FileName = texture[i].name;
File.WriteAllBytes(path + size + "_" + DateAndTime + "." + FileName + i.ToString() + _format, bytes);
Debug.Log("\"" + texture[i].name + "\" の" + _format.Replace(".","").ToUpper() + "ファイルの生成に成功しました。");
}else if(skipConvert)
{
skipConvert = false;
}
}
if(UseTexture2dName) FileName = null;
System.Diagnostics.Process.Start(_path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment