- Home /
Cannot resize UI Panel
That's the code (it's pretty self-explanatory):
bool showMenu;
GameObject overLay; //this is the GameObject (the panel) with the RectTransform attached to it
void Start()
{
showMenu = false;
overLay = GameObject.Find ("Overlay");//That's the name of the panel
overLay.SetActive (false); // I want to activate only when I click the button (this works)
}
void Update () {
var getLeft = overLay.GetComponent<RectTransform> (); //getting the recttransform from the Panel
if (showMenu == true) {
overLay.SetActive (true); //activating the panel
var transP = getLeft.sizeDelta;
transP = new Vector2 (0,0); //this part of the code is supposed to resize the Panel but it does absolutely nothing , no matter what value I put in
}
else if (showMenu == false) {
Debug.Log ("do stuff here");
}
}
public void activateMenu(){ //Function that sets the showMenu to true once I click a button
showMenu = true;
}
What I'm trying to do here is get the panel gameobject and then get the panel recttransform. I use the recttransform to resize the panel but anything I try does nothing.
transP
equals Vector2.zero
and not the panel sizeDelta. You have to pass the value to getLeft.sizeDelta
.
getLeft.sizeDelta = transP;
Answer by tablekorner · Aug 23, 2017 at 06:26 PM
Setting transP to the new Vector2 won't affect the actual sizeDelta. transP isn't referencing the sizeDelta of the RectTransform, it's just storing the same value as it. Perhaps changing the if statement to this:
if (showMenu == true)
{
overLay.SetActive(true); //activating the panel
getLeft.sizeDelta = new Vector2(0, 0);
}
Ok , never$$anonymous$$d , this works. I am sorry to the user that commented before and told me that (I was setting it to 0 so nothing was changing).
Is there any way to make this slowly increase in size? I thought about something like this
if (show$$anonymous$$enu == true)
{
overLay.SetActive(true); //activating the panel
getLeft.sizeDelta = new Vector2(0+1, 0);
}
This doesn't really work though. It only increases the size by 1 only once.
For sure you can. Just create a variable, float, int, whatever kind you want at the start. Set it to 0 or whatever starting value you want. Then increment it if show$$anonymous$$enu is true.
bool show$$anonymous$$enu;
GameObject overLay;
float inc = 0; // or an int
void Start ()
{
show$$anonymous$$enu = false;
overLay = GameObject.Find("Overlay");
overLay.SetActive(false);
}
void Update ()
{
var getLeft = overLay.GetComponent<RectTransform>();
if (show$$anonymous$$enu == true)
{
inc += 0.75F; //or inc++;
overLay.SetActive(true);
getLeft.sizeDelta = new Vector2(inc, inc);
}
else if (show$$anonymous$$enu == false)
{
Debug.Log("do stuff here");
}
}
Now you can also stop it from incrementing when it's at the size you want, if that's what you want to happen:
by changing
if (show$$anonymous$$enu == true)
to
if (show$$anonymous$$enu == true && inc <= 175) // Or whatever value in place of 175 you want.
And if you want to size it different height, and width, just use to increment variables.
Thanks , it works but there is one problem. I want that the starting size is 0 but it starts at full size and then increases (while I would like to start at 0 and then increase)
Your answer
Follow this Question
Related Questions
How to animate UI rect transform which works for all resolutions 1 Answer
How do I scale the Xmax value of a RectTransform 3 Answers
RectTransform returning incorrect rect bounds 0 Answers
Converting to "Rect Transform" permanently deleted part of my project 0 Answers
Resizing UI Image but with minimum width 2 Answers