- Home /
RectTransform sizeDelta not really working
So i have two pictures. When i click one it centers ish on screen and enlarges. When i click it again it goes back to its original position and size. I now want it to be so if one is large and i click the other one, the first one minimizes and the other one enlarges. Kinda simple... or at least it should be... The repositioning works fine and the collision box minimizes with the image, but the size does not minimize when i click another picture...
Here is my code:
if (!moved)
{
startPos = curPos;
if(scrollbar != null)
{
scrollbar.GetComponent<ScrollRect>().enabled = false;
scrollbar.GetComponent<ScrollRect>().StopMovement();
}
if(FindLargePictures() != null)
{
FindLargePictures().GetComponent<DumbEnlarge>().MoveBack();
}
gameObject.transform.position = centerPos;
image.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(Screen.width / 1.3f, (Screen.width / 1.3f) * (startSize.y / startSize.x));
gameObject.GetComponent<BoxCollider2D>().size = image.transform.GetComponent<RectTransform>().sizeDelta;
moved = true;
}
else if (moved)
{
if(scrollbar != null)
scrollbar.GetComponent<ScrollRect>().enabled = true;
gameObject.transform.position = startPos;
image.transform.GetComponent<RectTransform>().sizeDelta = startSize;
gameObject.GetComponent<BoxCollider2D>().size = image.transform.GetComponent<RectTransform>().sizeDelta;
moved = false;
}
And here is the two functions i made for it:
public void MoveBack()
{
if (moved)
{
if (scrollbar != null)
scrollbar.GetComponent<ScrollRect>().enabled = true;
gameObject.transform.position = startPos;
image.transform.GetComponent<RectTransform>().sizeDelta = startSize;
gameObject.GetComponent<BoxCollider2D>().size = image.transform.GetComponent<RectTransform>().sizeDelta;
moved = false;
}
}
GameObject FindLargePictures()
{
GameObject[] images = GameObject.FindGameObjectsWithTag("Image");
GameObject returnObject = null;
for (int i = 0; i < images.Length; i++)
{
if (images[i].GetComponent<DumbEnlarge>().moved)
{
returnObject = images[i];
}
}
if (returnObject != null)
return returnObject;
else return null;
}
Your answer
Follow this Question
Related Questions
Changing an image size cancels its anchoring? 1 Answer
Tile Resolution for HD Game 0 Answers
Moving images around 0 Answers
Why do image file size becomes huge on Import?? 2 Answers
Height Value of Rect Transform cannot be changed via script 1 Answer