Skip to content

Instantly share code, notes, and snippets.

@rtauziac
Created April 9, 2025 15:47
Show Gist options
  • Save rtauziac/e98e5d47f14e453f44ea824bf0314aaa to your computer and use it in GitHub Desktop.
Save rtauziac/e98e5d47f14e453f44ea824bf0314aaa to your computer and use it in GitHub Desktop.
A script to swap layout group orientation in Unity Editor
// A simple script to drop in the Editor folder.
// It adds a button in the group layout's contextual menu to swap the direction (horizontal/vertical).
// taken from https://discussions.unity.com/t/swap-from-horizontal-to-vertical-layout-group/672104/6
using UnityEngine;
using UnityEditor;
public static class LayoutGroupUtil
{
private const string VerticalLayoutGroupGuid = "59f8146938fff824cb5fd77236b75775";
private const string HorizontalLayoutGroupGuid = "30649d3a9faa99c48a7b1166b86bf2a0";
[MenuItem("CONTEXT/HorizontalLayoutGroup/Convert to VerticalLayoutGroup")]
private static void ConvertToVerticalLayoutGroup(MenuCommand command)
{
Convert(command.context, VerticalLayoutGroupGuid);
}
[MenuItem("CONTEXT/VerticalLayoutGroup/Convert to HorizontalLayoutGroup")]
private static void ConvertToHorizontal(MenuCommand command)
{
Convert(command.context, HorizontalLayoutGroupGuid);
}
private static void Convert(Object context, string guid)
{
MonoBehaviour layoutGroup = (MonoBehaviour)context;
if (!layoutGroup)
{
return;
}
Undo.RecordObject(layoutGroup, "Convert Layout Group");
SerializedObject so = new (layoutGroup);
SerializedProperty scriptProperty = so.FindProperty("m_Script");
so.Update();
scriptProperty.objectReferenceValue = AssetDatabase.LoadAssetAtPath<MonoScript>(AssetDatabase.GUIDToAssetPath(guid));
so.ApplyModifiedProperties();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment