For the life of me, I can’t figure out how to set the width of a UI Image through code. I’ve looked through the Image and RectTransform interfaces, but I’m not seeing it. Anyone know how?
I don’t know how to set rectTransform’s width programmatically either, it seems to be read-only. Would setting the scale instead work for you?
float newscale = 2f;
rectTransform.localScale = new Vector3(newscale, newscale, newscale);
Yeah, I noticed in the comment it said it was calculated… so I did end up using the localScale and it worked fine for what I was doing.
I can’t figure out how to change most of the new GUI components through script right now. For instance how the heck do I access the Image component? If we can change the width of the Rect Transform in the editor there should be a way to modify the same property the editor is modifying.
EDIT - maybe it has something to do with RectTransformUtility
You mustn’t use scale with RectTransform if you want to change width/height;
rectTransform.sizeDelta = new Vector2(width, rectTransform.sizeDelta.y);
You can get to the Image object by using GetComponent(). It’s just a script, but it lives in the UnityEngine.UI namespace.
Thanks ortin. That’s a very confusing name.