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!!!!