playMaker

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

DeepShader

  • Playmaker Newbie
  • *
  • Posts: 32
    • View Profile
Help, I'm getting confused with this script x_x[SOLVED]
« on: October 03, 2012, 12: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 03, 2012, 09:43:07 PM by jeanfabre »

DeepShader

  • Playmaker Newbie
  • *
  • Posts: 32
    • View Profile
Re: Help, I'm getting confused with this script x_x
« Reply #1 on: October 03, 2012, 01: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();    

    }
}