はじめに
上記の記事の手順を踏むことで ContentSizeFitter の Inspector に
HorizontalFit と VerticalFit を同時に変更するボタンを追加できます
ソースコード
using UnityEngine; using UnityEngine.UI; namespace UnityEditor.UI { [CustomEditor(typeof(ContentSizeFitter), true)] [CanEditMultipleObjects] /// <summary> /// Custom Editor for the ContentSizeFitter Component. /// Extend this class to write a custom editor for an ContentSizeFitter-derived component. /// </summary> public class ContentSizeFitterEditor : SelfControllerEditor { SerializedProperty m_HorizontalFit; SerializedProperty m_VerticalFit; protected virtual void OnEnable() { m_HorizontalFit = serializedObject.FindProperty("m_HorizontalFit"); m_VerticalFit = serializedObject.FindProperty("m_VerticalFit"); } public override void OnInspectorGUI() { serializedObject.Update(); EditorGUILayout.PropertyField(m_HorizontalFit, true); EditorGUILayout.PropertyField(m_VerticalFit, true); var fitter = target as ContentSizeFitter; using ( new EditorGUILayout.HorizontalScope() ) { if ( GUILayout.Button( "Unconstrained" ) ) { Undo.RecordObject( fitter, "Unconstrained" ); fitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained; fitter.verticalFit = ContentSizeFitter.FitMode.Unconstrained; } if ( GUILayout.Button( "Min Size" ) ) { Undo.RecordObject( fitter, "Min Size" ); fitter.horizontalFit = ContentSizeFitter.FitMode.MinSize; fitter.verticalFit = ContentSizeFitter.FitMode.MinSize; } if ( GUILayout.Button( "Preferred Size" ) ) { Undo.RecordObject( fitter, "Preferred Size" ); fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; } } serializedObject.ApplyModifiedProperties(); base.OnInspectorGUI(); } } }
ContentSizeFitterEditor.cs に上記のコードを上書きで貼り付けることで
ContentSizeFitter の Inspector に
HorizontalFit と VerticalFit を同時に変更するボタンを追加できます