Hi,
I'm attempting to draw UISprites with static and dynamic dimensions at dynamic positions on the screen base on the game screen resolution. Unfortunately, the sprites don't seem to line up correctly with the Y access. Any help?
using UnityEngine;
using System.Collections;
public class ManageGameScreen : MonoBehaviour
{
// Use this for initialization
void Start()
{
//int MaxPixelX = Screen.currentResolution.width;
//int MaxPixelY = Screen.currentResolution.height;
int MaxPixelX = 992;
int MaxPixelY = 888;
print("screen resolution: " + MaxPixelX + "x" + MaxPixelY);
int HalfPixelX = (MaxPixelX / 2);
int HalfPixelY = (MaxPixelY / 2);
print("half screen resolution: " + HalfPixelX + "x" + HalfPixelY);
UISprite[] screenElements = gameObject.GetComponentsInChildren<UISprite>();
foreach (UISprite sprite in screenElements)
{
if (sprite.gameObject.name.Equals("Sprite - Text Input Field"))
{
print("textInputField");
sprite.pivot = UISprite.Pivot.TopLeft;
int xPos = -HalfPixelX;
int yPos = -(HalfPixelY - 20);
int xDim = MaxPixelX;
int yDim = 20;
print("xPos: " + xPos + "; yPos: " + yPos + "; xDim: " + xDim + "; yDim: " + yDim + ";");
sprite.SetRect(xPos, yPos, xDim, yDim);
}
else if (sprite.gameObject.name.Equals("Sprite - Text Response Box"))
{
print("textResponseBox");
sprite.pivot = UISprite.Pivot.TopLeft;
int xPos = -HalfPixelX;
int yPos = -(HalfPixelY - 140);
int xDim = MaxPixelX;
int yDim = 120;
print("xPos: " + xPos + "; yPos: " + yPos + "; xDim: " + xDim + "; yDim: " + yDim + ";");
sprite.SetRect(xPos, yPos, xDim, yDim);
}
else if (sprite.gameObject.name.Equals("Sprite - Minimap"))
{
print("minimap");
sprite.pivot = UISprite.Pivot.TopLeft;
int xPos = (HalfPixelX - 192);
int yPos = HalfPixelY;
int xDim = 192;
int yDim = 192;
print("xPos: " + xPos + "; yPos: " + yPos + "; xDim: " + xDim + "; yDim: " + yDim + ";");
sprite.SetRect(xPos, yPos, xDim, yDim);
}
else if (sprite.gameObject.name.Equals("Sprite - Text Info Box"))
{
print("textInfoBox");
sprite.pivot = UISprite.Pivot.TopLeft;
int xPos = (HalfPixelX - 192);
int yPos = (HalfPixelY - 192);
int xDim = 192;
int yDim = (MaxPixelY - (192 + 120 + 20));
print("xPos: " + xPos + "; yPos: " + yPos + "; xDim: " + xDim + "; yDim: " + yDim + ";");
sprite.SetRect(xPos, yPos, xDim, yDim);
}
}
}
// Update is called once per frame
void Update()
{
}
}