using UnityEngine;
using System.Collections;
public class texExport : MonoBehaviour {
public ProceduralMaterial MySub;
private Texture[] Outputs;
private ProceduralTexture CurrentOutput;
private Texture2D MyTex;
void Start(){
SaveOutputs();
}
void SaveOutputs()
{
Outputs = new Texture[MySub.GetGeneratedTextures().Length];
for (int i=0;i<Outputs.Length;i++)
{
CurrentOutput = MySub.GetGeneratedTexture (Outputs[i].name);
MyTex = new Texture2D(CurrentOutput.width, CurrentOutput.height);
MyTex.SetPixels32 (CurrentOutput.GetPixels32 (0, 0, CurrentOutput.width, CurrentOutput.height));
MyTex.Apply();
Outputs = new Texture[MySub.GetGeneratedTextures().Length];
Outputs = MySub.GetGeneratedTextures();
}
}
}
I am getting this message in console when i run my scene -
NullReferenceException: Object reference not set to an instance of an object
texExport.SaveOutputs () (at Assets/ObjExporter/texExport.cs:24)
texExport.Start () (at Assets/ObjExporter/texExport.cs:15)
When you create an array, the contents of the array is null. You tried to access the name variable of the contents in Outputs (which is null), hence, null exception.
Hi jessica, what you may wanna do it look into the Image class, I believe (not sure) that textures inherit from there, if not I’m sure you can parse/cast it to an image, and then use Image.Save(PATH, ImageEncodingFlag)
I think the textures returned by GetGeneratedTextures are of type ProceduralTexture so you need to cast it to that rather than Texture2D. Unfortunately ProceduralTexture has no EncodeToPNG method so you’ll need to copy the pixels to an intermediate texture (as you did in your commented out code).
void SaveOutputs()
{
CurrentOutput = MySub.GetGeneratedTextures();
for (int i=0;i<CurrentOutput.Length;++i)
{
ProceduralTexture srcTexture = CurrentOutput[i] as ProceduralTexture;
MyTex = new Texture2D(srcTexture.width, srcTexture.height);
MyTex.SetPixels32(srcTexture.GetPixels32(0, 0, srcTexture.width, srcTexture.height));
MyTex.Apply();
// Encode texture into PNG
var bytes = MyTex.EncodeToPNG();
Destroy (MyTex);
File.WriteAllBytes(Application.dataPath + string.Format("/Extraccted Textures/SavedScreen{0}.png",i.ToString("F4")) , bytes);
}
}
using UnityEngine;
using System.Collections;
using System.IO;
public class texExport : MonoBehaviour
{
public ProceduralMaterial MySub;
private Texture[] CurrentOutput;
private Texture2D MyTex;
void Start()
{
SaveOutputs();
}
void SaveOutputs()
{
CurrentOutput = MySub.GetGeneratedTextures();
for (int i=0;i<CurrentOutput.Length;++i)
{
ProceduralTexture srcTexture = CurrentOutput[i] as ProceduralTexture;
MyTex = new Texture2D(srcTexture.width, srcTexture.height);
MyTex.SetPixels32(srcTexture.GetPixels32(0, 0, srcTexture.width, srcTexture.height));
MyTex.Apply();
// Encode texture into PNG
var bytes = MyTex.EncodeToPNG();
Destroy (MyTex);
File.WriteAllBytes(Application.dataPath + string.Format("/ExtractedTextures/SavedScreen{0}.png",i.ToString("F4")) , bytes);
}
}
}
Blank images are being generated , and i see yellow messages in the console -
Substance concrete_049 should be set to RAW in order to use GetPixels32 on its texture outputs.
UnityEngine.ProceduralTexture:GetPixels32(Int32, Int32, Int32, Int32)
texExport:SaveOutputs() (at Assets/ObjExporter/texExport.cs:28)
texExport:Start() (at Assets/ObjExporter/texExport.cs:14)
what is left ??
Another problem is, if i export it to exe, i dont find blank png textures anywhere. I have created the required folder