playMaker

Author Topic: Help, I'm getting confused with this script x_x[SOLVED]  (Read 2620 times)

DeepShader

  • Playmaker Newbie
  • *
  • Posts: 33
Help, I'm getting confused with this script x_x[SOLVED]
« on: October 03, 2012, 03:18:30 PM »
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:

Code: [Select]
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:

Code: [Select]
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!!!!
« Last Edit: October 04, 2012, 12:43:07 AM by jeanfabre »

DeepShader

  • Playmaker Newbie
  • *
  • Posts: 33
Re: Help, I'm getting confused with this script x_x
« Reply #1 on: October 03, 2012, 04:05:52 PM »
I found finally a solution!!

Code: [Select]
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();    

    }
}