Hello, i’ve spent much time trying to get texture from camera and place it in GUILayout.Label or GUI.DrawTexture function and i can’t find any working solution for this.
The situation is:
I have camera and script attached to GameObject (Robot).
I’m creating this objects using Instantiate function.
Each object displays it’s own window with params(speed, etc…)
Also i want to display in that window an image from Object’s camera
Here’s a part of script, attached to my GameObject “Robot”:
public class RobotScript : MonoBehaviour {
///
***
///
public Camera robotCam;
Rect windowInfoPosition = new Rect(0, 0, 100, 100);
static int count=0;
int ID;
RenderTexture renderTexture;
Texture2D imageFromCamera = new Texture2D(400, 400);;
///
***
///
void Start () {
ID=count;
count++;
renderTexture = RenderTexture.GetTemporary(400, 400, 32);
///
}
void OnGUI()
{
windowInfoPosition = GUI.Window(ID, windowInfoPosition, OnWindowInfo, "Info", guiStyle);
}
void OnWindowInfo(int windowID)
{
GUILayout.BeginVertical();
GUILayout.Label("Speed: " + this.GetSpeed())
if(GUILayout.Button("Update camera")) //trying to get texture
{
RenderTexture currentRT = RenderTexture.active;
RenderTexture.active = renderTexture;
robotCam.targetTexture = renderTexture;
robotCam.Render();
imageFromCamera.ReadPixels(new Rect(0, 0, 400, 400), 0, 0);
imageFromCamera.Apply();
RenderTexture.active = currentRT;
}
GUILayout.Label(imageFromCamera); //neither this
GUILayout.Label(renderTexture); //nor this works
GUILayout.EndVertical();
}
//////
I’m new here, and i’m confused about how it works.
I will appreciate your help. Thanks!
