Help, I'm getting confused with this script x_x

Hi there,
I’m trying to build a PlayMaker-Action, which stores a Texture2D from a WebCamTexture as a PNG-File to disk.

I’ve build two version and both got a problem -.-

The (1) one:

import System;
import System.IO;
import HutongGames.PlayMaker;

@ActionCategory("Webcam")
public class PNGSave extends FsmStateAction{

    	
	@UIHint(UIHint.Variable)
	@ObjectTypeAttribute(typeof(WebCamTexture))
	public var webCamTexture : FsmObject;

	public var fileName : FsmString;
	
	public function Reset(){
		fileName = null;
	}
	
	
	

	public function OnEnter(){
		
		var tx2d = Texture2D(webCamTexture.height,webCamTexture.width,TextureFormat.RGB24, false);

		tx2d.SetPixels(webCamTexture.GetPixels());
		tx2d.Apply();

		var bytes = tx2d.EncodeToPNG();

		File.WriteAllBytes(Application.dataPath + "/../" + fileName + ".png", bytes);


    }
       
      
}

This stops with error: NullReferenceException: Object reference not set to an instance of an object

What’s wrong here?


The (2) one:

import System;
import System.IO;
import HutongGames.PlayMaker;

 

@ActionCategory("Webcam")
public class PNGSave extends FsmStateAction{

    @UIHint(UIHint.Variable)
    public var gameobject : FsmOwnerDefault;

    public var fileName : FsmString;

    public function Reset(){
        gameobject = null;
        fileName = null;
    }

    

 

    public function OnEnter(){

		var  go : GameObject = Fsm.GetOwnerDefaultTarget(gameobject);
		
		var tx2d = Texture2D(go.renderer.material.mainTexture.height,go.renderer.material.mainTexture.width,TextureFormat.RGB24, false);

		tx2d.SetPixels(go.renderer.material.mainTexture.GetPixels());
		tx2d.Apply();

		var bytes = tx2d.EncodeToPNG();
		

    	var file = new File.Open(Application.dataPath + "../" + fileName + ".png",FileMode.Create);
    	var binary = new BinaryWriter(file);
    	
    	binary.Write(bytes);
    	file.Close();	   

    }
}

This one starts and works, but the result PNG is corrupt. I think this is because the script get not the correct hight and witdh!?


I hope someone could help me with this problem!!!

I have no idea about the syntaxes, but something that occurs to me is:

File.WriteAllBytes(Application.dataPath + “/…/” + fileName + “.png”, bytes);

script 1:31… datapath + /…/ + filename…

I’m not sure about that /…/ thing, doesnt seem correct… (unless your folder is named ‘…’)

then in script 2:36:

var file = new File.Open(Application.dataPath + “…/” + fileName + “.png”,FileMode.Create);

you use a ‘different path’… “…/” (instead of “/…/”.

Maybe something there. If not. it might help if you tell us where the error occurs:

NullReferenceException: Object reference not set to an instance of an object (at line x in script y)

Thank you for your fast response!

Script 1:31 and 2:36 only saves the PNG one folder-level higher. That’s working perfect :slight_smile:

I found finally a solution!!

import System;
import System.IO;
import HutongGames.PlayMaker;

 

@ActionCategory("Webcam")
public class PNGSave extends FsmStateAction{

    @UIHint(UIHint.Variable)
    public var gameobject : FsmOwnerDefault;

    public var fileName : FsmString;
    
    public function Reset(){
        gameobject = null;
        fileName = null;
    }

    

 

    public function OnEnter(){

		var go : GameObject = Fsm.GetOwnerDefaultTarget(gameobject);
		var tx2d = Texture2D(go.renderer.material.mainTexture.width,go.renderer.material.mainTexture.height,TextureFormat.RGB24, false);

		tx2d.SetPixels(go.renderer.material.mainTexture.GetPixels());
		tx2d.Apply();

		var bytes = tx2d.EncodeToPNG();
		

    	var file = new File.Open(Application.dataPath + "/../" + fileName + ".png",FileMode.Create);
    	var binary = new BinaryWriter(file);
    	
    	binary.Write(bytes);
    	file.Close();	   

    }
}