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…
I created an Image object inside of a UI Canvas. I’m trying to make a vertical health bar using it, so I need to be able to modify the bar’s height at runtime. The new Image object’s height/width is adjusted during development time using its RectTransform component, as you see in the screenshot. But how to modify that at runtime?
I currently have the Image object dragged into a GameObject variable of my main game script and have tried stuff like this (using CSharp):
public GameObject healthbar;
healthbar.GetComponent().rect.Set(0,0,100, 300);
which doesn’t work. And healthbar.GetComponent().rect.y is GET only so cannot be changed at runtime.
I’ve also tried:
healthbar.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.
My guess is that the rect property of RectTransform is giving you a copy back so Set() is being called on the copy. Try assigning rect to some local variable, Set()ing that, and then re-assigning back to the RectTransform.
I think the solution you want is you want to push the image resolution to the rect transforms width and height?
public RectTransform rt;
public Image image;
public void SomeFunction()
{
rt.sizeDelta = new Vector2 (image.sprite.rect.width,image.sprite.rect.height);
}
No, no, no. NEVER use that method to set the size of a RectTransform, it is compeltely wrong. It will sometimes, because of coincidence have a side-effect of setting the size.
(PS: I hate that someone at Unity chose such a bizarre naming scheme for the API, and deleted/blocked the obvious “size” - but that’s what we have to deal with :))
This is completely wrong, you were too lazy to read the thread (which explains IN DETAIL why your post is wrong). Don’t post stuff you don’t understand on threads you didn’t read.
All you’ve done is post an advert for a YouTube video - that is itself badly incorrect.