- Home /
Saving texture of selected GO (Editor Script)
I am having trouble using my Editor script to save the selected objects texture to file. Right now, it saves a small 241 pixel square from what the Scene view shows, and not the texture on the object. Does anyone have any experience with this?
TLDR/Goal; Save the texture of a selected GO's Mesh to a file.
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
[CustomEditor(typeof(MeshRenderer))]
public class SaveMaterialAsAsset : Editor
{
public override void OnInspectorGUI()
{
if (GUILayout.Button("Save Material as File"))
{
DrawDefaultInspector();
//Renderer renderer2 = target as Renderer; // captures view both work
MeshRenderer renderer2 = (MeshRenderer)target; // captures view both work
Material material = new Material(renderer2.sharedMaterial);
SavePNG(material);
}
}
void SavePNG(Material mat)
{
int width = mat.mainTexture.width;
int height = mat.mainTexture.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Object.Destroy(tex);
File.WriteAllBytes(Application.dataPath + "/../Assets/Materials/Saved/SavedScreen" + System.DateTime.Now.ToString("yyyymmdd") +".png", bytes);
}
}
I wonder if Selection.activeTransform.gameObject.GetComponent().material or similar is needed.
@Grish_tad has part of the anser : your need to get the correct object from selection.
Also, ins$$anonymous$$d of creating a new texture, you cand directly do :
byte[] bytes = mat.mainTexture.EncodeToPNG();
And last but not least, you can also use assetdatabase to find mat.mainTexture path, and use File.Copy to copy directly the file.
Doing this will export the texture source, while Texture2D.ReadPixel will get the compressed texture.
Answer by Bunny83 · Mar 16, 2018 at 01:09 PM
If your texture that you want to save is actually a Texture2D you can simply do:
[CustomEditor(typeof(MeshRenderer))]
public class SaveMaterialAsAsset : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if (GUILayout.Button("Save Material as File"))
{
Renderer renderer = (Renderer)target;
Material mat = renderer.sharedMaterial;
Texture2D tex = (Texture2D)mat.mainTexture;
byte[] data = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/../Assets/Materials/Saved/SavedScreen" + System.DateTime.Now.ToString("yyyymmdd") +".png", data);
}
}
If your texture is not a Texture2D you can use CopyTexture to copy the context of the source texture into a temporary texture. To cover both cases you could replace those two lines:
Texture2D tex = (Texture2D)mat.mainTexture;
byte[] data = tex.EncodeToPNG();
with this:
Texture2D tex = mat.mainTexture as Texture2D;
byte[] data;
if (tex == null)
{
Texture source = mat.mainTexture;
if (source == null)
return;
tex = new Texture2D(source.width, source.height, TextureFormat.ARGB24, false);
Graphics.CopyTexture(source, tex);
data = tex.EncodeToPNG();
DestroyImmediate(tex);
}
else
{
data = tex.EncodeToPNG();
}
File.WriteAllBytes(Application.dataPath + "/../Assets/Materials/Saved/SavedScreen" + System.DateTime.Now.ToString("yyyymmdd") +".png", data);
However keep in mind that this code will just save the texture of the material of the selected meshrenderer as it is. Your filename looks like you want to save a screenshot which this code doesn't do.
Answer by Grish_tad · Mar 16, 2018 at 11:37 AM
Hi @CaKeMeaT , try this
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
[CustomEditor(typeof(MeshRenderer))]
public class SaveMaterialAsAsset : Editor
{
public override void OnInspectorGUI()
{
if (GUILayout.Button("Save Material as File"))
{
DrawDefaultInspector();
GameObject obj = Selection.activeGameObject;
//Renderer renderer2 = target as Renderer; // captures view both work
//MeshRenderer renderer2 = (MeshRenderer)target; // captures view both work
//Material material = new Material(renderer2.sharedMaterial);
SavePNG( obj.GetComponent<Renderer>().sharedMaterial);
}
}
void SavePNG(Material mat)
{
int width = mat.mainTexture.width;
int height = mat.mainTexture.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex= mat.mainTexture;
// tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Object.Destroy(tex);
File.WriteAllBytes(Application.dataPath + "/../Assets/Materials/Saved/SavedScreen" + System.DateTime.Now.ToString("yyyymmdd") +".png", bytes);
}
}
While this is more correct than the original code those three lines make no sense:
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex= mat.mainTexture;
tex.Apply();
You create a new texture and store it in the "tex" variable. Then you replace the texture in tex with the texture from the material which makes the newly created texture unavailable anymore. Apply is also pointless since nothing has been changed on the texture.
Object.Destroy(tex);
This line would try to destroy the actual texture from the material since that is referenced in the tex variable. So also pointless. As a sidenote "Destroy" can't be used from an editors script. You have to use DestroyImmediate at edit time.
Finally the DrawDefaultInspector has to be called outside the button.
Your answer
Follow this Question
Related Questions
"Source file could not be found" error after editor script creates and deletes a cs file 3 Answers
UnityEditor - Drawing on a Texture 1 Answer
How can I save the Bild from a Camera, which not main camera is. 1 Answer
Get RenderTexture 1 Answer
android app: load a local image into texture by WWW class 0 Answers