GUITexture Messing Up On Mobile

Hello guys,

So I have a few GUITextures on the screen which are set at a specific position using GUITexture.pixelInst = Rect(x,y,widht, height);

I’ve tried with different screen sizes while in game the GUITextures adjust properly again using the same code when the screen size changes,
But the problem is when I try to test it on the Mobile(Android 2.2) the GUITextures are not where they are supposed to be.

yes I did search a bit saw that I have to set the localScale to (0,0,0) to make pixelInst work properly but honestly I tried that even if I don’t set it or set it it doesn’t make any difference at all to the position of the GUITextures on Mobile or on PC.

Any ideas??

Thanks!
Cheers!

I’ve never had much luck with the GUITexture object. Trying to get it in the right position is always maddening.

I would way rather use GUI.DrawTexture in an OnGUI function. You get much more control over it. Plus you can use set the Rect position based on Screen.width and Screen.height so that it works on screens of different sizes…

Well okay I guess I forgot to mention this earlier.

What I’m actually doing is Instantiating a Transform that has a GUITexture in it on to the screen. I need to do this because each Texture should have a different name should be click-able(or touch-able on a mobile device) to display certain information about it.

I don’t think I can achieve that with GUI.DrawTexture right??

also I am actually using Rect based on the screen width height hence why the current code does adjust into change in screen sizes while running in Unity Free Aspect but as soon as I change the aspect ratio it messes up;

So I guess the better question would be how can I correct my GUITextures to be at the same position on different aspect ratios??

GUITexture principles:

  1. set the Scale of the object to 0,0,1 (very important)

  2. set the object to world zero.

  3. set the pixel inset width and height to that of the image

  4. set the pixel inset x and y to minus half the width and height

Now, assign your texture to the GUITexture.

If you run it, it will be centered in the lower left corner.

If you move the object to 0.5,0.5,0 then it will be centered on the screen

If you move it to 1,1,0 then it will be centered in the upper right corner

Z) the z position of the object controls GUITexture depth. Objects with higher Z will be on top of objects with lower Z.

And that is the trick. :wink:

Hey MisterB, thanks for taking time for that :slight_smile:

Well I’m doing all that, yeah as I said the GUITextures do adjust to the different screen sizes but yeah not to different aspect ratios which just doesn’t make sense to me :frowning:

Okay let me post the code I guess, though it might not make much sense haha:

var pixelX: float
var pixelY: float

var currentPosition: GUITexture;

var currentPositionPrefab: Transform;

function Update() {

pixelX = (Screen.width/2);
pixelY = (Screen.height/2);

currentPosition.pixelInset = Rect(pixelX, pixelY, 40, 40);

loadCoordinates();

}

function loadCoordinates() {

//all the code to load  break up strings  put them in to arrays  classes goes here, so much of it  no use of posting it.

for (var i = 0; i < someArra.length - 1; i++) {

var clonePosition = Vector3(0,0,0);

currentPositionPrefab = Instantiate(currentPositionPrefab, clonePosition,  Quaternion.identity);

currentPositionPrefab.transform.parent = parentDot;
currentPositionPrefab.name = myData._iUser.userDataName + "User";

var pixelCloneX = ((Screen.width/2)/lon) - ((Screen.width/2)/parseFloat(myData._iUser.userDataLongitude));
var pixelCloneY = ((Screen.height/2)/lat) - ((Screen.height/2)/parseFloat(myData._iUser.userDataLatitude));

var myCoordinates: Vector2 = new Vector2(pixelCloneX, pixelCloneY);
myCoordinates = Vector2(myCoordinates.x * 256 * (zoomLevel), myCoordinates.y * 256 * (zoomLevel-1));

currentPositionPrefab.GetComponent(GUITexture).pixelInset = Rect(pixelX + myCoordinates.x, pixelY + myCoordinates.y, 20, 20);

}

//Just fyi the array doesn't keep going, it gets stopped after a certain amount of data is displayed on screen but I just wanted to show that the code is inside a for loop.

}

Alright so that’s the code, argh I know it’s so messed up complicated, but anwyays, what it does is, I’m making my own coordinates system for a static map on a plane.

I load a static map to a plane, the center of the plane = center of the map = user’s current position = (0,0,0) of my coordinates system.

Now what I do is I grab a few more user’s data from a server load them up then I convert those longitudes latitudes into the positions of my coordinates system. That’s pixelCloneX pixelCloneY.

So anyways yeah, that works perfect, the locations on the map are shown pretty accurately when the play mode is on Free Aspect but as soon I change it to any other aspect of a device or test it on my Android, the locations of the GUITexture mess up :frowning:

So yeah, any ideas still??

Here is some conventional code to reference a GUITexture that is at world zero. It will scale it to the screen size and maintain it’s aspect ratio.

	var screen = Vector2(Screen.width, Screen.height);
	var tex = Vector2(guiTexture.texture.width, guiTexture.texture.height);
	var scale : float = screen.x / tex.x;
	mapSize = tex * scale;
	if(mapSize.y > screen.y){
		scale = screen.y / tex.y;
		mapSize = tex * scale;
	}
	var rect : Rect= new Rect((screen.x - mapSize.x)/2.0, (screen.y - mapSize.y) / 2, mapSize.x, mapSize.y);
	guiTexture.pixelInset = rect;var screen = Vector2(Screen.width, Screen.height);