- Home /
GUI.DrawTexture Center on screen
Hello, I am trying to use GUI.DrawTexture to display a fish in the center of the screen. (Because why not) I try to center it with Screen.height/2 and same for the width. It appears the rectangle with the texture is off to the right and down a bit. The rectangle starts in the upper left hand corner. How can I make it so the rectangle is centered on any screen size? Thanks!`using UnityEngine; using System.Collections;
public class FishDisplay : MonoBehaviour {
public Texture fish;
void OnGUI() {
GUI.DrawTexture (new Rect (Screen.width / 2, Screen.height / 2, 600, 600), fish);
}
}`
Comment
Best Answer
Answer by zaid87 · Jun 18, 2015 at 07:23 AM
Try this:
void OnGUI() {
float width = 600;
float height = 600;
GUI.DrawTexture (new Rect ((Screen.width / 2) - (width/2), (Screen.height / 2) - (height/2), width, height), fish);
}
You need to adjust it based on the width/size of the image as well since the Position is at the image's top-left corner.