No change :
With a Top Left GridPivot :
With a Top Left GridPivot and Top Left Scrollview content origin, same thing.
Actually, with only one element, the GridPivot is absolutly useless (I tried all the Pivot). The problem seems linked to the grid size.
Frankly, I'm amazed to realize how hard it is to implement a simple scrollable grid, populated on the fly. Here is my exact setup (I can share the project with you by private message if you want, it may be easier this way, but I would like to avoid this as much as possible);
My hierachy :

PlaceList :

Grid :

The script attached to this "Screen" (root UIPanel). This script takes a JSON containing a list of places, and instantiate a prefab for each place (filling some TextField).
It also manages the resizing of the Grid cell width and height, and the grid elements, in case of resolution change, as I want my grid cell to take a certain amount of space of any resolution (i.e my cell width is the screen width divided by 2.2).
using UnityEngine;
using System.Collections;
using Newtonsoft.Json;
using System.Collections.Generic;
public class PlacesScreen : MonoBehaviour
{
// Used to check if the screen resolution changed
// TODO : Find a better solution than this one
public float ScreenStartWidth;
public float ScreenStartHeight;
List<Place> places;
UIScrollView PlacesList;
UIGrid PlacesGrid;
GameObject FileList;
// Hardcoded for testing purposes only
string json = @"[]";
// Use this for initialization
void Start()
{
places = JsonConvert.DeserializeObject<List<Place>>(json);
PlacesList = gameObject.GetComponentInChildren<UIScrollView>() as UIScrollView;
PlacesGrid = gameObject.GetComponentInChildren<UIGrid>() as UIGrid;
FileList = GameObject.Find("Grid");
ScreenStartHeight = PlacesList.gameObject.GetComponent<UIPanel>().height;
ScreenStartWidth = PlacesList.gameObject.GetComponent<UIPanel>().width;
ResizeGrid();
if (PlacesGrid != null && FileList != null && PlacesList != null)
{
BetterList<Transform> childList = PlacesGrid.GetChildList();
// Addind the places from the json to the graphic list
foreach (Place place in places)
{
Debug.Log("Adding a place");
GameObject placeFile = Resources.Load("prefabs/File") as GameObject;
placeFile.GetComponent<UISprite>().width = (int)PlacesGrid.cellWidth;
placeFile.transform.Find("ContentPanel").gameObject.transform.Find("PlaceName").gameObject.GetComponent<UILabel>().text = place.Name;
placeFile.transform.Find("ContentPanel").gameObject.transform.Find("CityName").gameObject.GetComponent<UILabel>().text = place.City;
placeFile.transform.Find("ContentPanel").gameObject.transform.Find("TypeName").gameObject.GetComponent<UILabel>().text = place.Type;
NGUITools.AddChild(FileList, placeFile);
childList.Add(placeFile.transform);
}
// Small hack to get a nicely presented Grid even with only 1 element
// TODO : Redo this ...
if (childList.size == 1)
{
}
}
}
// Update is called once per frame
void Update()
{
if (ScreenStartHeight != PlacesList.gameObject.GetComponent<UIPanel>().height || ScreenStartWidth != PlacesList.gameObject.GetComponent<UIPanel>().width)
{
// PlacesLisit UIPanel size is changing when the List is dragged, but in this case we don't want to call a Resize()
if(!PlacesList.isDragging)
{
ResizeGrid();
ScreenStartHeight = PlacesList.gameObject.GetComponent<UIPanel>().height;
ScreenStartWidth = PlacesList.gameObject.GetComponent<UIPanel>().width;
}
}
}
/// <summary>
/// Resize the grid considering the screen width
/// </summary>
void ResizeGrid()
{
Debug.Log("ResizeGrid Called");
// Resizing the Grid cells' size.
PlacesGrid.cellWidth = (float)(PlacesList.gameObject.GetComponent<UIPanel>().width / 2.2);
PlacesGrid.cellHeight = (float)(PlacesGrid.cellWidth / 1.244);
foreach (Transform place in PlacesGrid.GetChildList())
{
place.gameObject.GetComponent<UISprite>().width = (int)PlacesGrid.cellWidth;
}
PlacesGrid.Reposition();
PlacesList.ResetPosition();
}
}
As I said
here I have another problem. The ResetPosition() function of the Scrollview is not working if called right after the grid resizing. If I "Reset clipping position" in the editor while in play mode, it's working perfectly (and putting the ResetPosition() in the Update() is working too. I'm not the only one having this problem).
Any help is appreciated, because I really start to get crazy : those are minor bugs, but still.