Hello, I'm using the new UI system from Unity 4.6 beta...
Tried different codes, looked at the docs and searched around but can't find the answer…
For example. I have an image and I want to change width and height during runtime.
public GameObject image4;
image4.GetComponent<RectTransform>().rect.Set(0,0,100, 300);
which doesn't work. And image4.GetComponent().rect.y is GET only so cannot be changed at runtime.
I've also tried:
image4.transform.localScale.y = 15;
which doesn't work either.
what is the proper way to change size at runtime? You can give me an example in either JS or C#, doesn't matter.
When changing parent (eg after Instantiate), things get messy, I found resetting a few parameters really helped (spent quite a bit of time trying to figure this out):
Hope that helps someone else :)
try to use something like that [in C#]:
One day researching. I found an extension that can help us to deal with new UI system. u can improve this extension if you want.
The source code I found from here: http://orbcreation.com
Hey Friend Just Try This To Change Position and Width of an UI gameObject
if you want to use instance of an object use This
leftButton.GetComponent<RectTransform>().anchoredPosition = new Vector2(-125, -36f); rightButton.GetComponent<RectTransform>().sizeDelta = new Vector2(x, y);
And if want to put script on a UI object then try this to change height and width
You normally don't want to modify directly those values, even more if they come from a prefab you have already setup with the correct coords, if you just want to add it correctly under canvas (or other) but it "changes position" do this:
Passing
false
on set parent will avoid the rect transform being changed in weird forms when instantiating the prefab.Actuallly playing with following can help: GetComponent.sizeDelta = new Vector(new_size.x,new_size.y);
I successfully achieved Zooming in new GUI by setting this property.
Here is my own code for scaling: