ソースコード
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
namespace Kogane.Internal
{
internal static class SetPreferredSizeMenuItem
{
[MenuItem( "CONTEXT/RectTransform/Set Preferred Size" )]
private static void SetPreferredSize( MenuCommand menuCommand )
{
var rectTransform = ( RectTransform )menuCommand.context;
Undo.RecordObject( rectTransform, "Set Preferred Size" );
SetSizeWithCurrentAnchors( RectTransform.Axis.Horizontal );
SetSizeWithCurrentAnchors( RectTransform.Axis.Vertical );
void SetSizeWithCurrentAnchors( RectTransform.Axis axis )
{
rectTransform.SetSizeWithCurrentAnchors
(
axis: axis,
size: LayoutUtility.GetPreferredSize( rectTransform, ( int )axis )
);
}
}
}
}