I have a Gameobject added in unity and i want to set rect transform for it as shown in the image..Like stretch and custom.How is it done?Any script needs to be added for it?
You can get the RectTrasform with two ways.
var rectTransform = transform as RectTransform;
or
var rectTransform = GetComponent<RectTransform>();
and yes, you need to add a script to achieve your goal.
I’m not sure what the exact question is. However if you’re asking “how” to make a gameobject to actually have a rect transform, the gameobject usually has to be a child of a Canvas. Only UI components will have / need a rect transform.
However you can use AddComponent<RectTransform>()
to replace the regular Transform component with a RectTransform. Note AFAIK there is no a way to do the opposite through code. You can remove a RectTransform in the editor through the context menu of the component. Though through code it seems it’s not possible to remove it.