Position GUI Texture in center of screen After Scaling it

When the player clicks a piece of paper, it displays the paper as a Gui Texture in the center of the screen.

I’ve been able to get it to scale to the size of the screen using GUI.matrix, but I’m having terrible difficulty positioning it in the center after that.

Here’s what I have now:

function OnGUI () {
	GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3(1.0*Screen.width/X, 1.0*Screen.height/Y, 1.0));
	if(opened == true){
	     GUI.DrawTexture(Rect((Screen.width/2 - paperTextureWidth/2), 10, paperTextureWidth, paperTextureHeight), paperTexture, ScaleMode.ScaleToFit);
	}
}

The problem is that once the paperTexture has been scaled, it has a new width value that I’d need to use to position it, but I don’t know what that width is, since it depends on the resolution.

You might want to use an actual GUITexture object instead of OnGUI code…if you position it at (.5, .5) then it’s always in the middle of the screen, and it always scales with the resolution, without needing any code.

–Eric

Thank you! That works much better, and I can just activate and deactivate the object when I want it viewed or not.

I know its a little late, but can you please share the code on how you this worked for you?

It’s been forever but I found it.

var targetPaperObject : GameObject;

function Activate () {
    var paperOpened = targetPaperObject.GetComponent("GUITexture");
    paperOpened.enabled = true;
}

You’ll need to create an empty in the scene with the position’s x and y both set to 0.5. Add a GUITexture component and assign it your paper image. Then you can assign it to targetPaperObject in the inspector.

There’s probably an easier way to do this, but I’m a composer, not a programmer, so I don’t know. :wink:

That’s pretty much correct, except GetComponent shouldn’t use quotes. Also you could do it on one line:

    targetPaperObject.GetComponent(GUITexture).enabled = true;

Or, if all you’re using is the GUITexture component, then you don’t need GetComponent:

var targetPaperObject : GUITexture;

function Activate () {
    targetPaperObject.enabled = true;
}

–Eric

can you help me about this code…i want to have a button permanent next and preview…a slideshow…
public class HorizontalTransitionGUI : MonoBehaviour
{
//A 4x4 Matrix
private Matrix4x4 trsMatrix;
//A three dimension vector that will translate GUI coordinate system
private Vector3 positionVec;
//Two booleans to determine which of the GUI buttons have been pressed
private bool next = false;
private bool back = false;

// Use this for initialization
void Start()
{
//Initialize the matrix
trsMatrix = Matrix4x4.identity;
//Initialize the Vector
positionVec = Vector3.zero;
}

// Update is called once per frame
void Update()
{
//If the ‘next’ boolean is true
if(next)
{
//Interpolate the current vector x component until it has the same as value the screen width
positionVec.x = Mathf.SmoothStep(positionVec.x, Screen.width,Time.deltaTime10);
/Make ‘trsMatrix’ a matrix that translates, rotates and scales the GUI.
The position is set to positionVec, the Quaternion is set to identity
and the scale is set to one.
/
trsMatrix.SetTRS(positionVec , Quaternion.identity, Vector3.one);
}
else if(back) //If ‘back is true’
{
//Interpolate the current vector x component until it reaches zero
positionVec.x = Mathf.SmoothStep(positionVec.x, 0,Time.deltaTime
10);
//Make ‘trsMatrix’ a matrix that translates, rotates and scales the GUI.
trsMatrix.SetTRS(positionVec , Quaternion.identity, Vector3.one);
}

}

void OnGUI()
{
//The GUI matrix must changed to the trsMatrix
GUI.matrix = trsMatrix;

//If the button labeled ‘Next’ is pressed
if(GUI.Button(new Rect(Screen.width - 400, 315, 100, 30),“Next”))
{
next = true;
back = false;
}

//The TextArea that appears on the first screen.
GUI.TextArea(new Rect(300,200,Screen.width-600,100), “Click on the ‘Next’ button to change the Text Area.”);

//If the button labeled ‘Back’ is pressed
if(GUI.Button(new Rect(-Screen.width + 300, 315, 100, 30),“Back”))
{
next = false;
back = true;
}

//The TextArea that appears on the second screen
GUI.TextArea(new Rect(-Screen.width + 300,200,Screen.width-600,100), “Click on the ‘Back’ button to return to the previous Text Area.”);

//To reset to GUI matrix, just make it equal to a 4x4 identity matrix
GUI.matrix = Matrix4x4.identity;

}
}