protected void OnEnable()
 {
     selectionBox       = (Texture2D)EditorGUIUtility.Load("PrefabBrush/Selection-box.png");
     m_SerializedObject = new SerializedObject(target);
     m_Prefabs          = m_SerializedObject.FindProperty("m_prefabs");
     m_actPrefab        = m_SerializedObject.FindProperty("activePrefab");
     selGridIndex       = -1;
     prefabInsert       = PrefabBlock.Empty;
 }
 public void InsertBlock(PrefabBlock block)
 {
     m_prefabs.Add(block);
 }
    public override void OnPaintInspectorGUI()
    {
        m_SerializedObject.UpdateIfRequiredOrScript();
        scrollPos = EditorGUILayout.BeginScrollView(scrollPos);

        var     style       = GetGUIStyle();
        Vector2 itemsMargin = new Vector2(style.margin.horizontal / 2, style.margin.vertical / 2);
        int     rowLength   = Mathf.Max(Mathf.FloorToInt(Screen.width / ButtonWidth), 1);

        selGridIndex = GUILayout.SelectionGrid(
            selGridIndex,
            GetGUIContentFromItems(),
            rowLength,
            GetGUIStyle()
            );
        if (selGridIndex > -1)
        {
            m_actPrefab.objectReferenceValue = GetSelectedItem(selGridIndex);
            if (Event.current.clickCount == 2)
            {
                EditorGUIUtility.PingObject(m_actPrefab.objectReferenceValue);
            }
            var gridRect = GUILayoutUtility.GetLastRect();

            Rect selRect = new Rect(
                gridRect.xMin + ((ButtonWidth + itemsMargin.x) * (selGridIndex % rowLength)),
                gridRect.yMin + ((ButtonHeight + itemsMargin.y) * (selGridIndex / rowLength)),
                ButtonWidth,
                ButtonHeight);


            GUI.DrawTexture(selRect, selectionBox);
            if (DrawDeleteButton(selRect, 20))
            {
                prefabBrush.RemoveBlockAt(selGridIndex);
                selGridIndex = -1;
            }
        }

        GUILayout.EndScrollView();

        if (toggleInsert = EditorGUILayout.Foldout(toggleInsert, "Add new tile", true, EditorStyles.foldout))
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.BeginVertical();
            EditorGUIUtility.labelWidth = 50;
            prefabInsert.prefab         = (GameObject)EditorGUILayout.ObjectField("Prefab", prefabInsert.prefab, typeof(GameObject), false);
            prefabInsert.name           = (string)EditorGUILayout.TextField("Name", prefabInsert.name);
            prefabInsert.display        = (Texture2D)EditorGUILayout.ObjectField("Display", prefabInsert.display, typeof(Texture2D), false);
            EditorGUIUtility.labelWidth = 0;                     //Reset labelWidth to default value
            EditorGUILayout.EndVertical();

            EditorGUI.BeginDisabledGroup(prefabInsert.prefab == null);
            var addBtn = GUILayout.Button("+", GUILayout.Width(20f), GUILayout.Height(20f));
            EditorGUI.EndDisabledGroup();
            if (addBtn)
            {
                if (prefabInsert.name == "")
                {
                    prefabInsert.name = prefabInsert.prefab.name;
                }
                if (prefabInsert.display == null)
                {
                    prefabInsert.display = AssetPreview.GetAssetPreview(prefabInsert.prefab);
                }
                prefabBrush.InsertBlock(prefabInsert);
                prefabInsert = PrefabBlock.Empty;
            }
            EditorGUILayout.EndHorizontal();
        }
        m_SerializedObject.ApplyModifiedPropertiesWithoutUndo();
    }