- Home /
How to set the new Unity UI Rect Transform Anchor Presets via c# script ?
Hi , how can we set the anchor prest to something like .... Top Stretch, as shown in the picture ?
i don't the the default values
Answer by allenallenallen · Jul 16, 2015 at 04:10 AM
You control the anchor point with these values:
Using code, you can control the anchors with:
http://docs.unity3d.com/ScriptReference/RectTransform-anchorMin.html http://docs.unity3d.com/ScriptReference/RectTransform-anchorMax.html http://docs.unity3d.com/ScriptReference/RectTransform-pivot.html
All of them are Vector2.
public RectTransform panelRectTransform;
// Something like this.
void Start()
{
panelRectTransform.anchorMin = new Vector2(1, 0);
panelRectTransform.anchorMax = new Vector2(0, 1);
panelRectTransform.pivot = new Vector2(0.5f, 0.5f);
}
Furthermore, the anchor points and these variable are used the most often so in case you aren't sure what they are:
anchoredPosition controls the positions X and Y positions of the GUI based on the anchor points. sizeDelta controls the width and height of the GUI.
Answer by FlightOfOne · Jan 26, 2018 at 06:42 PM
public void SetAndStretchToParentSize(RectTransform _mRect, RectTransform _parent)
{
_mRect.anchoredPosition = _parent.position;
_mRect.anchorMin = new Vector2(1, 0);
_mRect.anchorMax = new Vector2(0, 1);
_mRect.pivot = new Vector2(0.5f, 0.5f);
_mRect.sizeDelta = _parent.rect.size;
_mRect.transform.SetParent(_parent);
}
In case if anyone wants to extend the RecTransform class:
public static void SetAndStretchToParentSize(this RectTransform _mRect, RectTransform _parent)
{
_mRect.anchoredPosition = _parent.position;
_mRect.anchorMin = new Vector2(1, 0);
_mRect.anchorMax = new Vector2(0, 1);
_mRect.pivot = new Vector2(0.5f, 0.5f);
_mRect.sizeDelta = _parent.rect.size;
_mRect.transform.SetParent(_parent);
}
Answer by alvmoral · Dec 20, 2018 at 07:14 PM
Try this
public Vector2 UIGetElementDimension (GameObject mGameObject) {
Vector2 Res = -Vector2.one;
RectTransform RectTransform = mGameObject.GetComponent<RectTransform>();
if (RectTransform != null) {
Res = RectTransform.sizeDelta; // Read GameObject Dimensions
}
return Res;
}
public void UIMaximizeElement(GameObject mGameObject, GameObject Parent = null, float Left = 0, float Right = 0, float Top = 0, float Bottom = 0) {
RectTransform RectTransform = mGameObject.GetComponent<RectTransform>(); // Get Rect Transform Component from element
if (RectTransform != null) {
Vector2 ParentSize = -Vector2.one;
if (mGameObject.transform.parent != null) // Get Size of Parent
ParentSize = UIGetElementDimension(mGameObject.transform.parent.gameObject);
else if (Parent != null)
ParentSize = UIGetElementDimension(Parent);
RectTransform.anchorMin = new Vector2(0, 0); // Set Location respect to Axes, same thing then doit manually in Anchor Min and Max in inspector of Rect Transform
RectTransform.anchorMax = new Vector2(1, 1);
RectTransform.pivot = new Vector2(0.5f, 0.5f); // Pivot in the Middle
if (ParentSize != -Vector2.one) {
float SizeWidth = ParentSize.x - Left - Right; // Calculate dimensions of Element;
float SizeHeight = ParentSize.y - Top - Bottom;
RectTransform.offsetMin = Vector2.zero;
RectTransform.offsetMax = new Vector2(SizeWidth, SizeHeight); // Set dimensions
RectTransform.anchoredPosition = new Vector2(SizeWidth / 2 + Left, SizeHeight / 2 + Bottom); // Anchored Position set automatically Left, Top, Right and Bottom
}
}
}
Answer by unity_iLxAeHlWxpIIXw · Apr 01, 2019 at 11:21 AM
check the link it will help you https://github.com/CG-Tespy/Unity-RectTransform-Preset-Utils