Full Rect

Unity C# Code Example

Unity C# Full Rect

using UnityEditor;
using UnityEngine;

namespace MedjeDev
{
    public class FullRect
    {
        [MenuItem("GameObject/Set Full Rect", false, 0)]
        static void SetFullRect()
        {
            // Get the currently selected GameObject in the editor
            GameObject selectedObject = Selection.activeGameObject;
            if (selectedObject != null)
            {
                RectTransform rt = selectedObject.GetComponent();
                if (rt != null)
                {
                    // Record the RectTransform itself for undo purposes
                    Undo.RecordObject(rt, "Set Full Rect");

                    var oldPivot = rt.pivot;

                    var rtp = rt.parent.GetComponent();
                    rt.pivot = rtp.pivot;
                    rt.anchorMin = new Vector2(0, 0);
                    rt.anchorMax = new Vector2(1, 1);

                    var newWidth = 0f; // Placeholder for calculated width
                    var newHeight = 0f; // Placeholder for calculated height

                    rt.sizeDelta = new Vector2(newWidth, newHeight) * 2f;
                    rt.localPosition = Vector3.zero;
                    rt.pivot = oldPivot;

                    // Mark the RectTransform as dirty to notify Unity of the change
                    EditorUtility.SetDirty(rt);
                }
                else
                {
                    Debug.LogWarning("Selected GameObject does not have a RectTransform component.");
                }
            }
            else
            {
                Debug.LogWarning("No GameObject selected to adjust.");
            }
        }
    }
}