using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class BakeTextureWindow : EditorWindow
{
Material ImageMaterial;
string FilePath = "Assets/MaterialImage.png";
Vector2Int Resolution;
bool hasMaterial;
bool hasResolution;
bool hasFilePath;
[MenuItem("Tools/Bake material to texture")]
static void OpenWindow()
{
BakeTextureWindow window = EditorWindow.GetWindow<BakeTextureWindow>();
window.Show();
window.CheckInput();
}
void OnGUI()
{
EditorGUILayout.HelpBox("Set the material you want to bake as well as the size " +
"and location of the texture you want to bake to, then press the \"Bake\" button.", MessageType.None);
using (var check = new EditorGUI.ChangeCheckScope())
{
ImageMaterial = (Material)EditorGUILayout.ObjectField("Material", ImageMaterial, typeof(Material), false);
Resolution = EditorGUILayout.Vector2IntField("Image Resolution", Resolution);
FilePath = FileField(FilePath);
if (check.changed)
{
CheckInput();
}
}
GUI.enabled = hasMaterial && hasResolution && hasFilePath;
if (GUILayout.Button("Bake"))
{
BakeTexture();
}
GUI.enabled = true;
if (!hasMaterial)
{
EditorGUILayout.HelpBox("You're still missing a material to bake.", MessageType.Warning);
}
if (!hasResolution)
{
EditorGUILayout.HelpBox("Please set a size bigger than zero.", MessageType.Warning);
}
if (!hasFilePath)
{
EditorGUILayout.HelpBox("No file to save the image to given.", MessageType.Warning);
}
}
void CheckInput()
{
hasMaterial = ImageMaterial != null;
hasResolution = Resolution.x > 0 && Resolution.y > 0;
hasFilePath = false;
try
{
string ext = Path.GetExtension(FilePath);
hasFilePath = ext.Equals(".png");
}
catch (ArgumentException) { }
}
string FileField(string path)
{
EditorGUILayout.LabelField("Image Path");
using (new GUILayout.HorizontalScope())
{
path = EditorGUILayout.TextField(path);
if (GUILayout.Button("choose"))
{
string directory = "Assets";
string fileName = "MaterialImage.png";
try
{
directory = Path.GetDirectoryName(path);
fileName = Path.GetFileName(path);
}
catch (ArgumentException) { }
string chosenFile = EditorUtility.SaveFilePanelInProject("Choose image file", fileName,
"png", "Please enter a file name to save the image to", directory);
if (!string.IsNullOrEmpty(chosenFile))
{
path = chosenFile;
}
Repaint();
}
}
return path;
}
void BakeTexture()
{
RenderTexture renderTexture = RenderTexture.GetTemporary(Resolution.x, Resolution.y);
Graphics.Blit(null, renderTexture, ImageMaterial);
Texture2D texture = new Texture2D(Resolution.x, Resolution.y);
RenderTexture.active = renderTexture;
texture.ReadPixels(new Rect(Vector2.zero, Resolution), 0, 0);
byte[] png = texture.EncodeToPNG();
File.WriteAllBytes(FilePath, png);
AssetDatabase.Refresh();
RenderTexture.active = null;
RenderTexture.ReleaseTemporary(renderTexture);
DestroyImmediate(texture);
}
}
|