Author Topic: Offset Child Inside UIGrid  (Read 1876 times)

Ace

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 24
    • View Profile
Offset Child Inside UIGrid
« on: November 23, 2014, 12:49:14 PM »
Greetings!

I am trying to fix the position of objects inside a UIGrid.

Right now I have a deck object in top corner. When I add cards to it, I want to offset those cards by width/2 and height/2 so they are top left added to the list (instead of center added as they are currently):



However when I offset the position of the children when I add them, I think those changes get overriden by having to call Reposition() (and nothing appears if I don't call that).

Here is my code:
  1. //Puts cards at the top of the screen
  2.         public static void dealFirstHand()
  3.         {
  4.                 deck = GameObject.FindGameObjectWithTag("deck");
  5.                 for(int i =0;i<6;i++)
  6.                 {
  7.                         GameObject card =  NGUITools.AddChild(deck,Resources.Load("Prefabs/UI/CardSlot") as GameObject);
  8.                         assignRandomCard(card.GetComponent<UI_Controller_CardSlot>().cardArt);
  9.                         UIPanel display = card.transform.GetComponentInChildren<UIPanel>();
  10.                         card.transform.parent = deck.transform;
  11.                         card.transform.localScale = new Vector3(1f,1f,1f);
  12.                         card.transform.position = new Vector3(display.width/2,display.height/2,0);
  13.                         playerCards.Add(card.GetComponent<UI_Controller_CardSlot>().cardArt,false);
  14.                         card.GetComponent<UI_Controller_CardSlot>().cardArt.activate();
  15.                 }
  16.                
  17.                 deck.GetComponent<UIGrid>().Reposition();
  18.         }
  19.  

Can you advise how I can properly get it so cards are added so that my offset values apply? I verified card.transform.position = new Vector3(display.width/2,display.height/2,0); gives the correct values so can you tell me where I can put this to get it to register? Do I need to loop through the cards again and reposition them manually? (That seems very inefficient)

The object being added is center pivoted, and I want that to stay center pivtoted. I just want the children added to be offset by width/2 and height/2.

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Offset Child Inside UIGrid
« Reply #1 on: November 25, 2014, 01:09:23 AM »
Grid simply changes the position of transforms underneath it. If you want there to be an offset, then do just that. Instead of just having your items structured like this:

Sprite (centered pivot)
- Another sprite
- Label, etc

You would instead have something like this:

Widget (top left pivot likely would be best -- can also be a simple game object)
- Sprite
- Another sprite
- Label, etc

Ace

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 24
    • View Profile
Re: Offset Child Inside UIGrid
« Reply #2 on: November 25, 2014, 01:39:29 AM »
Was hoping to do it programatically but Ill make that change.

Thanks