void OnEnable()
    {
        m_LevelLayout = target as LevelLayout;

        var assets = AssetDatabase.FindAssets("t:LevelRoomGroup");

        foreach (var a in assets)
        {
            string         path    = AssetDatabase.GUIDToAssetPath(a);
            LevelRoomGroup palette = AssetDatabase.LoadAssetAtPath <LevelRoomGroup>(path);

            m_AvailablesPalettes.Add(palette);
        }

        m_PieceProperty = serializedObject.FindProperty("rooms");

        m_HighlightMaterial = new Material(Shader.Find("Hidden/Internal-Colored"));

        m_HighlightMaterial.color = new Color32(255, 238, 0, 255);
        m_HighlightMaterial.SetInt("ZTest", (int)UnityEngine.Rendering.CompareFunction.Always);

        EditorApplication.playModeStateChanged += PlayModeChange;
    }
    public override void OnInspectorGUI()
    {
        bool editing = GUILayout.Toggle(m_EditingLayout, "Editing Layout", "Button");

        if (editing != m_EditingLayout)
        {
            if (!editing)
            {//disabled editing, cleanup
                if (m_CurrentInstance != null)
                {
                    DestroyImmediate(m_CurrentInstance.gameObject);
                }
                m_CurrentGroup    = null;
                m_CurrentInstance = null;
                m_SelectedRoom    = null;
            }
            else
            {
                if (!SceneView.lastActiveSceneView.drawGizmos)
                {
                    if (EditorUtility.DisplayDialog("Warning", "Gizmos are globally disabled, which prevents the layout editing tools from working. Do you want to re-enable Gizmos?", "Yes", "No"))
                    {
                        SceneView.lastActiveSceneView.drawGizmos = true;
                    }
                    else
                    {
                        editing = false;
                    }
                }
            }

            m_EditingLayout = editing;
        }

        if (m_EditingLayout)
        {
            EditorGUILayout.HelpBox("Press R to change which door the room use to connect to other room", MessageType.Info);

            EditorGUILayout.BeginHorizontal();

            int editingMode = GUILayout.Toolbar(m_EditingMode, new[] { "Add", "Remove" }, GUILayout.Width(120));
            if (editingMode != m_EditingMode)
            {
                if (editingMode == 1)
                {
                    if (m_CurrentInstance != null)
                    {
                        DestroyImmediate(m_CurrentInstance.gameObject);
                    }

                    m_SelectedRoom = null;
                }

                m_EditingMode = editingMode;
            }

            if (m_CurrentInstance != null)
            {
                EditorGUILayout.LabelField("Flip : ", GUILayout.Width(32));
                EditorGUI.BeginChangeCheck();

                bool flipX = GUILayout.Toggle(m_CurrentScale.x < 0, "X", "button", GUILayout.Width(32));
                bool flipY = GUILayout.Toggle(m_CurrentScale.y < 0, "Y", "button", GUILayout.Width(32));
                bool flipZ = GUILayout.Toggle(m_CurrentScale.z < 0, "Z", "button", GUILayout.Width(32));

                GUILayout.FlexibleSpace();

                if (EditorGUI.EndChangeCheck())
                {
                    m_CurrentScale = new Vector3(flipX ? -1 : 1, flipY ? -1 : 1, flipZ ? -1 : 1);
                    m_CurrentInstance.transform.localScale = m_CurrentScale;
                }
            }

            EditorGUILayout.EndHorizontal();

            //we repaint all scene view to be sure they get a notification so they can "steal" focus in edit mode
            SceneView.RepaintAll();
        }

        GUILayout.BeginHorizontal();

        GUILayout.BeginVertical();
        GUILayout.Label("Group");
        GUILayout.BeginScrollView(m_PaletteSelectionScroll);

        foreach (var p in m_AvailablesPalettes)
        {
            GUI.enabled = m_CurrentGroup != p;
            if (GUILayout.Button(p.name))
            {
                Debug.Log($"clicked on {p.name}");
                if (!m_EditingLayout)
                {
                    if (!SceneView.lastActiveSceneView.drawGizmos)
                    {
                        if (EditorUtility.DisplayDialog("Warning", "Gizmos are globally disabled, which prevent the layout editing tool to work. Do you want to re-enable Gizmos?", "Yes", "No"))
                        {
                            SceneView.lastActiveSceneView.drawGizmos = true;
                            m_EditingLayout = true;
                        }
                    }
                    else
                    {
                        m_EditingLayout = true;
                    }
                }

                if (m_EditingLayout)
                {
                    m_CurrentGroup = p;
                }
            }
        }

        GUI.enabled = true;

        GUILayout.EndScrollView();
        GUILayout.EndVertical();

        m_PaletteSelectionScroll = GUILayout.BeginScrollView(m_PaletteSelectionScroll, GUILayout.Width(72 * 3));
        GUILayout.BeginVertical();

        if (m_CurrentGroup != null)
        {
            bool horizontalOpen = false;

            for (int i = 0; i < m_CurrentGroup.levelPart.Length; ++i)
            {
                LevelRoom part = m_CurrentGroup.levelPart[i];

                if (i % 3 == 0 && i != 0)
                {
                    GUILayout.EndHorizontal();
                    horizontalOpen = false;
                }

                if (!horizontalOpen)
                {
                    GUILayout.BeginHorizontal();
                    horizontalOpen = true;
                }

                Texture2D preview = AssetPreview.GetAssetPreview(part.gameObject);

                GUI.enabled = part != m_SelectedRoom;
                if (GUILayout.Button(preview, GUILayout.Width(64), GUILayout.Height(64)))
                {
                    m_SelectedRoom = part;

                    if (m_CurrentInstance != null)
                    {
                        DestroyImmediate(m_CurrentInstance.gameObject);
                    }

                    m_CurrentInstance = Instantiate(m_SelectedRoom, m_LevelLayout.transform);
                    m_CurrentInstance.gameObject.isStatic = false;
                    m_CurrentInstance.gameObject.tag      = "EditorOnly";
                    m_CurrentInstance.name = "TempInstance";
                    m_CurrentUsedExit      = 0;

                    m_CurrentInstance.transform.localScale = m_CurrentScale;

                    m_EditingMode = 0;
                }
            }

            if (horizontalOpen)
            {
                GUILayout.EndHorizontal();
            }
        }

        GUI.enabled = true;
        GUILayout.EndVertical();
        GUILayout.EndScrollView();

        GUILayout.EndHorizontal();
    }