Help me with EncodeToPNG

I am trying to export the textures to png during runtime from unity.

This is my script so far -

It is attached to the object and i have dragged the procedural material on it from the inspector.

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)

Please help me completing this script

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.

this is my latest code -

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()
  {    
    //GetProceduralTexture (inputName : String) :
    CurrentOutput =MySub.GetGeneratedTextures();
    for (int i=0;i<CurrentOutput.Length;++i)
    {
      //MyTex = new Texture2D(CurrentOutput[i].width, CurrentOutput[i].height);        
      //MyTex.SetPixels32 (CurrentOutput[i].GetPixels32 (0, 0, CurrentOutput[i].width, CurrentOutput[i].height));
     //MyTex.Apply(); 
     
			
			//MyTex = CurrentOutput[i] as Texture2D;
			
      // Encode texture into PNG
     
			//var bytes = MyTex.EncodeToPNG();
      //Destroy (MyTex);
			
      // For testing purposes, also write to a file in the project folder
      //File.WriteAllBytes(Application.dataPath + string.Format("/Extraccted Textures/SavedScreen{0}.png",i.ToString("F4")) , bytes);
			File.WriteAllBytes(Application.dataPath + string.Format("/Extraccted Textures/SavedScreen{0}.png",i.ToString("F4")) ,((Texture2D)CurrentOutput [i]).EncodeToPNG());
    }
  }
}

and this is the error i am facing

InvalidCastException: Cannot cast from source type to destination type.
texExport.SaveOutputs () (at Assets/ObjExporter/texExport.cs:37)
texExport.Start () (at Assets/ObjExporter/texExport.cs:14)

Please help me to solve this.

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)

for more on the topic look here; http://msdn.microsoft.com/en-us/library/9t4syfhh.aspx from my understanding mono should support system.Drawing

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);
	}
}

My current code -

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

This:

You can change the texture to RAW format in the material asset’s per-platform overrides section:
http://docs.unity3d.com/Documentation/Components/class-ProceduralMaterial.html

  1. Now textures are generated but they are not similar to the textures in the game scene… see here -

  2. And the normal maps are also not generated

This is my latest code till now

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()

{    
		MySub.isReadable = true;
    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();
			renderer.material.mainTexture = MyTex;

        // Encode texture into PNG
        var bytes = MyTex.EncodeToPNG(); 
        Destroy (MyTex);
        File.WriteAllBytes(Application.dataPath + string.Format("/ExtractedTextures/SavedScreen{0}.png",i.ToString("F4")) , bytes);

    }

}
}

What do i do next ??

http://forum.unity3d.com/threads/196289-Substance-Export-Bitmaps