Author Topic: Sizing and positioning within Clipped Panel  (Read 4394 times)

rookie_coder

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 14
    • View Profile
Sizing and positioning within Clipped Panel
« on: March 20, 2014, 10:12:38 AM »
I am using a Clipped Panel, which for now is anchored to UIRoot, as a container for my cubes in my Match 3 game I am working on. I am having issues constraining my cubes to be within the Panel. I want them to be sort of anchored to the Clipped Panel container so that they can resize along with the Panel when I deploy to mobile. I attached a script - GameManager.cs - to the Panel. I have also set the transform for the cubes to the parent transform. Below is the relevant code. How do I get the grid of cubes to be anchored to the Panel and size according to the UI?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameManager : MonoBehaviour
{
    public GameObject[] Cubes;
    public GameObject panel;

    void Start() {

    }
    void Update() {

        for (int i = 0; i < Cubes.Length + 1; i++) {
           
            for (int j = 0; j < Cubes.Length + 1; j++) {
               
                int index = Random.Range(0, Cubes.Length);

                GameObject myCube = Instantiate(Cubes[index], new Vector3(i, j, 0), Quaternion.identity) as GameObject;
                myCube.transform.parent = panel.transform;
            }
        }
    }
}

Thanks in advance

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sizing and positioning within Clipped Panel
« Reply #1 on: March 20, 2014, 01:03:38 PM »
First, never use Instantiate. Use NGUITools.AddChild. Look inside the function to find out why.

Second, why are you trying to resize the content? Why not just set UIRoot to be Fixed Size instead?

rookie_coder

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Sizing and positioning within Clipped Panel
« Reply #2 on: March 20, 2014, 02:40:28 PM »
I now use NGUITools.AddChild. Couple of issues remaining.

1. The cubes within the Panel were too small. I copied the NGUITools.AddChild locally to debug. I ended up having to do this to get to a reasonable size:

cube.localScale = Vector3.one * 100;

2. The first cube is starting somewhere near the Center of the Clipped Panel. I need it to start at Left-Top. The Clipped Panel is fully anchored to the UIRoot on all sides with no gaps. I ran some debug statements.

Debug.Log("Panel x is: " + panel.transform.position.x) ... etc. They gave me (0, 0).

cube.localPosition = Vector3.zero; ..... also doesn't change the position. I would change the position manually as a last resort. I would prefer to go with the top-left corner of the anchored position of the Panel.

Thanks

rookie_coder

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Sizing and positioning within Clipped Panel
« Reply #3 on: March 20, 2014, 04:15:30 PM »
I tried this and it moved the first cube to the bottom left of the panel:

cube.position = nguiCamera.ViewportToWorldPoint(panel.transform.position);

Shouldn't it have moved to top left since that would be the Panel position?

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sizing and positioning within Clipped Panel
« Reply #4 on: March 21, 2014, 02:27:36 PM »
1. panel.transform.position is in World coordinates. The function you're using takes Viewport coordinates. The two have nothing to do with each other.
2. Cubes are 3D objects. I didn't realize you were doing that. I thought you mean sprites. You should not mix 3D objects with NGUI's hierarchy. You will never get clipping to work. NGUI displays flat textures/sprites, not 3D objects.