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:
================================================================
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
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.