Forum rules - please read before posting.

My First custom action

I want the player to get a PNG file to save whatever is on the MainCamera (or a piggyback version of it). I'm not interested in the UI/menus, just what the cam shows.

To this end I'm trying to put a bit of C# code in to a custom action.... but things are not going as expected. 
This is what I have tried by following the AC tutorial and using the ActionTemplate.cs

Firstly: 
I have placed a camera called RenderCam as a child of MainCamera and set the Target Texture of RenderCam to a Render Texture in my project called RenderTex. I do this so as to not interfere with the actual MainCamera. This Render Texture appears to give the image desired so if that's a suitable approach it works for me.

Then:
Made a standalone CS script to ensure the Console has no errors. This is the full contents of that Script (this is adapted from the code shown here. http://docs.unity3d.com/ScriptReference/Camera.Render.html:

================================================================
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
Texture2D RTImage(Camera CameratoRender) {
RenderTexture RenderTex = RenderTexture.active;
RenderTexture.active = CameratoRender.targetTexture;
CameratoRender.Render();
Texture2D image = new Texture2D(1920, 1080);
image.ReadPixels(new Rect(0, 0, 1920, 1080), 0, 0);
image.Apply();
RenderTexture.active = RenderTex;
return image;
// Encode texture into PNG
var bytes = image.EncodeToPNG();
// For testing purposes, also write to a file in the project folder
System.IO.File.WriteAllBytes(Application.dataPath + "SavedScreen.png", bytes);
Destroy (image);
}
}
================================================================

When I try to place the relevant portions of code in the ActionTemplate file I knock out the bits which are obviously not needed, change the relevant identifiers from ActionTemplate to my own. So far so good, but.... lastly I try to place the script to run in the spot at line 40 of the ActionTemplate   (* This function is called when the action is performed.)

I get all KINDS of console errors. I know I'm doing something wrong but I'm the first to admit I'm a weak coder and so I'm really stuck.

Am I going about this the right way in the first place and if so, what part of the above CS script needs to go where in the ActionTemplate.

Has anyone made a similar action/script in an AC game to get a file saved based off the camera.

Comments

  • Without seeing the actual code or console error it's hard to give advice!  Then again, I do recall that the ReadPixels feature only working if you've waited until the end of the frame - something you can't do from an Action directly.

    Instead, what you'd want to do it keep your downloaded (non-Action) code in a separate script, and write a new function inside that called Interact(), which does the job (i.e. calls the RTImage function or whatever).  You can then simply use the Object: Send message Action to send the Interact message to your script, without needing to write a custom action.
  • Heh oops! I should know better. I had pasted the code but the forum post limit kicked in and so I pasted the short bit of custom code instead. 

    Anyway. Your Interact() suggestion seems more noob-code-test friendly so I'll give that a shot.

    Ta.
  • Success :)

    I do get a console error (only occurs at runtime but doesn't prevent render from saving). I have included an empty game object in the scene and attached the ScreenGrabA.cs script to it.

    The console error goes like this:
    =========================================================================
    Instance of ScreenGrabA couldn't be created. The the script class needs to derive from ScriptableObject.
    UnityEngine.ScriptableObject:CreateInstance(String)
    AdventureCreator:RefreshActions() (at Assets/AdventureCreator/Scripts/Managers/Editor/AdventureCreator.cs:426)
    AdventureCreator:OnEnable() (at Assets/AdventureCreator/Scripts/Managers/Editor/AdventureCreator.cs:45)
  • edited June 2015
    Here is the contents of ScreenGrabA.cs

    ==============================================================
    using UnityEngine;
    using System.Collections;

    public class ScreenGrabA : MonoBehaviour {
    public int resWidth = 3840; 
    public int resHeight = 2160;

    public static string ScreenShotName(int width, int height) {
    return string.Format("{0}/screenshots/screen_{1}x{2}_{3}.png", 
                        Application.dataPath, 
                        width, height, 
                        System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
    }

    void Interact() {

    Camera myCam = Camera.main;
    RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
    myCam.targetTexture = rt;
    Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
    myCam.Render();
    RenderTexture.active = rt;
    screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
    myCam.targetTexture = null;
    RenderTexture.active = null; // JC: added to avoid errors
    Destroy(rt);
    byte[] bytes = screenShot.EncodeToPNG();
    string filename = ScreenShotName(resWidth, resHeight);
    System.IO.File.WriteAllBytes(filename, bytes);
    Debug.Log(string.Format("Took screenshot to: {0}", filename));

    }
    }
  • Is it in your cutsom Actions folder?  Just move it out, and the error should disappear.
  • I had it in a CustomScripts subfolder in the project folder generated by AC's wizard.

    I moved it into the root Assets folder and lo: error disappeared.

    ta :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.