Author Topic: UISprite: help configuring, for using SetRect properly  (Read 2341 times)

Fractalbase

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
UISprite: help configuring, for using SetRect properly
« on: June 17, 2014, 05:57:28 PM »
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()
    {
   }

}


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UISprite: help configuring, for using SetRect properly
« Reply #1 on: June 18, 2014, 04:59:51 PM »
Why are you doing any of this? Look at the layout system that comes with NGUI. It's a much easier way of accomplishing what you're trying to do, and it doesn't involve any coding.

http://www.tasharen.com/forum/index.php?topic=7013.0

Fractalbase

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: UISprite: help configuring, for using SetRect properly
« Reply #2 on: June 21, 2014, 06:45:52 AM »
Thanks, that is helpful!