hi there. im making a scroll list where i want to show each sprite in the spritesheet (This is working) the only problem is that it changes the scale of the items in the scroll list (see pictures below this post). can some one help me?
this is my script (its connected to the scroll list):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScrollList : MonoBehaviour {
public GameObject spriteOptions;
public int columnCount = 1;
void Start()
{
RectTransform rowRectTransform = spriteOptions.GetComponent<RectTransform>();
RectTransform containerRectTransform = gameObject.GetComponent<RectTransform>();
//calculate the width and height of each child item.
float width = containerRectTransform.rect.width / columnCount;
float ratio = width / rowRectTransform.rect.width;
float height = rowRectTransform.rect.height * ratio;
int rowCount = itemCount / columnCount;
if (itemCount % rowCount > 0)
{
rowCount++;
}
//adjust the height of the container so that it will just barely fit all its children
float scrollHeight = height * rowCount;
containerRectTransform.offsetMin = new Vector2(containerRectTransform.offsetMin.x, -scrollHeight / 2);
containerRectTransform.offsetMax = new Vector2(containerRectTransform.offsetMax.x, scrollHeight / 2);
int j = 0;
for (int i = 0; i < itemCount; i++)
{
//this is used instead of a double for loop because itemCount may not fit perfectly into the rows/columns
if (i % columnCount == 0)
{
j++;
}
//create a new item, name it, and set the parent
GameObject newItem = Instantiate(spriteOptions) as GameObject;
newItem.name = gameObject.name + " item at (" + i + "," + j + ")";
newItem.transform.parent = gameObject.transform;
//move and size the new item
RectTransform rectTransform = newItem.GetComponent<RectTransform>();
float x = -containerRectTransform.rect.width / 2 + width * (i % columnCount);
float y = containerRectTransform.rect.height / 2 - height * j;
rectTransform.offsetMin = new Vector2(x, y);
x = rectTransform.offsetMin.x + width;
y = rectTransform.offsetMin.y + height;
rectTransform.offsetMax = new Vector2(x, y);
}
}
}
the object that its using as a prefab is called the scrollObject (see picture below post)
it has the scale of 1 - 1 - 1
after loading the list every other item has the scale of 0.416 - 0.416 - 0.416
can someone help me?!? thanks alot!
kelvin
(im not english so sorry for any mistakes. if it is missing something please tell me and i will fix it a.s.a.p)


