Hi,
I’m modifying some GUI controls on-the-fly using some scripting; like positioning a GUI element relative to the screen size and using some offsets defined as public variables accessible from the inspector.
The problem is, as weird as it sounds to me, sometimes the game preview displays this GUI element with the apropriate offsets, sometimes it just ignores it (and some other public inspector variables) and only applies them when I clic on the scene, for a very small amount of time.
Has this ever happened to anyone?
Can you post the code that is causing the trouble? Is the problem happening with Unity or Unity iPhone?
Plain Unity, this is the code:
//obtenemos la imagen
imagen = (GUITexture)GameObject.Find("imagen").GetComponent("GUITexture");
//obtenemos posicion y tamaño de la textura en pantalla
Rect pixelInset = imagen.pixelInset;
//Recolocamos en esquina superior izquierda mas los offsets
pixelInset.x = 0 + offsetX;
pixelInset.y = Screen.height - pixelInset.height + offsetY;
imagen.pixelInset = pixelInset;
//Obtenemos los 5 labels
GUIText[] labels = new GUIText[5];
for (int i = 0; i < 5; i++)
{
labels[i] = (GUIText)GameObject.Find("label" + (i + 1)).GetComponent("GUIText");
//Cambiamos el texto
labels[i].text = features[i].getName();
//Recolocacion en el centro del pentagono
Vector2 pixelOffset = new Vector2(imagen.pixelInset.x, imagen.pixelInset.y);
pixelOffset.y += imagen.pixelInset.height / 2 + offsetLabels[i].x;
pixelOffset.x += imagen.pixelInset.width / 2 + offsetLabels[i].y;
labels[i].pixelOffset = pixelOffset;
}
I use it to position a GUITexture in a certain part of the screen (top left with some variable offsets) and then position some GUIText in relation to that, using again some public offsets.
I somehow solved the problem putting that code into the Update() function instead of OnGUI() function.
I also tried to put it in the Start() function but it seemed to ignore the contect of the public variables as well.