I have a fairly simple piece of code that I can’t seem to get to work right.
public class Crosshair : MonoBehaviour {
public Texture crosshairTexture;
Rect position;
Camera MyCam;
public Color defaultColor = Color.black;
// Use this for initialization
void Start () {
MyCam = this.gameObject.camera;
}
void OnGUI () {
float WidthRatio = (MyCam.pixelWidth / Screen.width);
float HeightRatio = (MyCam.pixelHeight/Screen.height);
float TextureWidth = crosshairTexture.width * WidthRatio;
float TextureHeight = crosshairTexture.height * HeightRatio;
this.position = new Rect(MyCam.pixelRect.xMin +((MyCam.pixelRect.width - TextureWidth)/2),
Screen.height - (MyCam.pixelRect.yMin +((MyCam.pixelRect.height + TextureHeight)/2)), TextureWidth, TextureHeight);
GUI.color = defaultColor;
GUI.DrawTexture(position, crosshairTexture);
}
}
When I pass in a Texture2D as the crosshairTexture, everything is perfect. When I pass in a renderTexture, nothing gets drawn to the screen.
No matter what I change in the settings for the render texture I either get nothing on the screen, or a black box. If I set the clear mode on the rendertexure Camera to skybox, I can get a VERY faint outline of some clouds.