// Event, gets called when map is builded
    void MapBuildComplete()
    {
        Debug.Log("map build complete");

        StartCoroutine(CountObjects());

        if (!merge)
        {
            buildEnabled = true;
        }

        // Scatter objects after map build is complete
        if (scatterObjects)
        {
            // assign configuration based on selected theme
            var _objectScatterConfig = themes[selectedTheme].objectScatterConfiguration;
            objectScatter.configuration = _objectScatterConfig;

            // Call scatter method
            objectScatter.ScatterProceduralObjects();
        }
    }
    static void ShowRuleBasedObjects(TileWorldObjectScatterConfiguration _config)
    {
        GUI.color = colorGUI2;
        EditorGUILayout.BeginVertical("Box");
        GUI.color = Color.white;
        EditorGUILayout.BeginHorizontal("Box");

        ruleBasedObjectsFoldout = GUILayout.Toggle(ruleBasedObjectsFoldout, "Procedural objects", GUI.skin.GetStyle("foldout"), GUILayout.ExpandWidth(true), GUILayout.Height(18));
        EditorGUILayout.EndHorizontal();


        if (ruleBasedObjectsFoldout)
        {
            Event evt = Event.current;
            if (evt.type != EventType.DragPerform)
            {
                EditorGUILayout.BeginVertical("TextArea");


                for (int i = 0; i < _config.proceduralObjects.Count; i++)
                {
                    EditorGUILayout.BeginHorizontal();

                    if (_config.proceduralObjects[i].isChild)
                    {
                        GUILayout.Label("->", GUILayout.Width(30));
                    }
                    else
                    {
                        EditorGUILayout.BeginHorizontal("Box");

                        GUI.color = colorGUI2;
                        GUILayout.Box(" ", GUILayout.Width(5));
                        GUI.color = Color.white;
                    }



                    if (i > 0)
                    {
                        if (GUILayout.Button(_config.proceduralObjects[i].isChild ? "<<" : ">>", "toolbarButton", GUILayout.Width(20)))
                        {
                            _config.proceduralObjects[i].isChild = !_config.proceduralObjects[i].isChild;
                        }
                    }

                    _config.proceduralObjects[i].active = GUILayout.Toggle(_config.proceduralObjects[i].active, _config.proceduralObjects[i].active ? "on" : "off", GUI.skin.GetStyle("toolbarButton"), GUILayout.Width(22));

                    GUI.enabled = _config.proceduralObjects[i].active;

                    _config.proceduralObjects[i].showPanel = GUILayout.Toggle(_config.proceduralObjects[i].showPanel, (_config.proceduralObjects[i].go != null ? _config.proceduralObjects[i].go.name : "missing game object"), GUI.skin.GetStyle("foldout"), GUILayout.ExpandWidth(true), GUILayout.Height(18));

                    // move up
                    if (i > 0)
                    {
                        GUI.enabled = true;
                    }
                    else
                    {
                        GUI.enabled = false;
                    }

                    if (GUILayout.Button("▲", "toolbarButton", GUILayout.Width(20)))
                    {
                        MoveProceduralObjectsUp(_config, i);
                    }

                    GUI.enabled = true;

                    // move down
                    if (_config.proceduralObjects.Count > 1 && i < _config.proceduralObjects.Count - 1)
                    {
                        GUI.enabled = true;
                    }
                    else
                    {
                        GUI.enabled = false;
                    }
                    if (GUILayout.Button("▼", "toolbarButton", GUILayout.Width(20)))
                    {
                        MoveProceduralObjectsDown(_config, i);
                    }

                    GUI.enabled = true;

                    // duplicate
                    if (GUILayout.Button("+", "toolbarButton", GUILayout.Width(20)))
                    {
                        DuplicateProceduralObject(_config, i);
                    }

                    // delete
                    GUI.color = guiRed;
                    if (GUILayout.Button("x", "toolbarButton", GUILayout.Width(20)))
                    {
                        DeleteProceduralObject(_config, i);
                    }
                    GUI.color = Color.white;

                    EditorGUILayout.EndHorizontal();

                    if (i >= _config.proceduralObjects.Count)
                    {
                        return;
                    }

                    if (!_config.proceduralObjects[i].isChild)
                    {
                        EditorGUILayout.EndHorizontal();
                    }

                    if (_config.proceduralObjects[i].showPanel)
                    {
                        if (!_config.proceduralObjects[i].isChild)
                        {
                            EditorGUI.indentLevel = 2;

                            EditorGUILayout.BeginVertical();
                            _config.proceduralObjects[i].go = EditorGUILayout.ObjectField("gameobject:", _config.proceduralObjects[i].go, typeof(GameObject), false) as GameObject;
                            _config.proceduralObjects[i].placeOnOccupiedCell = EditorGUILayout.Toggle("place on occupied cell:", _config.proceduralObjects[i].placeOnOccupiedCell);

                            if (creator)
                            {
                                if (_config.proceduralObjects[i].selectedLayer < creator.configuration.global.layerCount && placeLayers.Length > 0)
                                {
                                    _config.proceduralObjects[i].selectedLayer = EditorGUILayout.Popup("place on layer:", _config.proceduralObjects[i].selectedLayer, placeLayers);
                                }
                                else
                                {
                                    _config.proceduralObjects[i].selectedLayer = 0;
                                }
                            }
                            else
                            {
                                _config.proceduralObjects[i].selectedLayer = 0;
                            }

                            // select rule
                            EditorGUILayout.BeginVertical("Box");
                            _config.proceduralObjects[i].ruleType = (TileWorldObjectScatterConfiguration.ProceduralObjectConfiguration.RuleTypes)EditorGUILayout.EnumPopup("rule type:", _config.proceduralObjects[i].ruleType);

                            if (_config.proceduralObjects[i].ruleType == TileWorldObjectScatterConfiguration.ProceduralObjectConfiguration.RuleTypes.pattern)
                            {
                                //EditorGUILayout.BeginVertical("Box");
                                EditorGUILayout.HelpBox("Place the object on every Nth tiletype.", MessageType.Info);

                                _config.proceduralObjects[i].everyNTile = EditorGUILayout.IntField(new GUIContent("Nth:", "place the object on every Nth(every, second, third, fourth...) tile"), _config.proceduralObjects[i].everyNTile);

                                if (_config.proceduralObjects[i].everyNTile < 1)
                                {
                                    _config.proceduralObjects[i].everyNTile = 1;
                                }

                                _config.proceduralObjects[i].tileTypes = (TileWorldObjectScatterConfiguration.ProceduralObjectConfiguration.TileTypes)EditorGUILayout.EnumPopup("tiletype:", _config.proceduralObjects[i].tileTypes);
                                _config.proceduralObjects[i].inset     = EditorGUILayout.IntField("inset:", _config.proceduralObjects[i].inset);
                                //EditorGUILayout.EndVertical();
                            }
                            else
                            {
                                //EditorGUILayout.BeginVertical("Box");

                                _config.proceduralObjects[i].weight    = EditorGUILayout.Slider("weight:", _config.proceduralObjects[i].weight, 0, 1);
                                _config.proceduralObjects[i].tileTypes = (TileWorldObjectScatterConfiguration.ProceduralObjectConfiguration.TileTypes)EditorGUILayout.EnumPopup("place on:", _config.proceduralObjects[i].tileTypes);
                                _config.proceduralObjects[i].inset     = EditorGUILayout.IntField("inset:", _config.proceduralObjects[i].inset);
                                //EditorGUILayout.EndVertical();
                            }

                            EditorGUILayout.EndVertical();

                            _config.proceduralObjects[i].offsetPosition = EditorGUILayout.Vector3Field("offset position:", _config.proceduralObjects[i].offsetPosition);
                            _config.proceduralObjects[i].offsetRotation = EditorGUILayout.Vector3Field("offset rotation:", _config.proceduralObjects[i].offsetRotation);

                            // Use random rotation
                            EditorGUILayout.BeginVertical("Box");
                            _config.proceduralObjects[i].useTileRotation = EditorGUILayout.Toggle("use tile rotations:", _config.proceduralObjects[i].useTileRotation);
                            GUI.enabled = !_config.proceduralObjects[i].useTileRotation;
                            _config.proceduralObjects[i].useRandomRotation = EditorGUILayout.Toggle("use random rotation:", _config.proceduralObjects[i].useRandomRotation);
                            GUI.enabled = _config.proceduralObjects[i].useRandomRotation;


                            GUI.enabled = !_config.proceduralObjects[i].useTileRotation && _config.proceduralObjects[i].useRandomRotation;
                            _config.proceduralObjects[i].randomRotationMin = EditorGUILayout.Vector3Field("min random rotation:", _config.proceduralObjects[i].randomRotationMin);
                            _config.proceduralObjects[i].randomRotationMax = EditorGUILayout.Vector3Field("max random rotation:", _config.proceduralObjects[i].randomRotationMax);
                            GUI.enabled = true;

                            EditorGUILayout.EndVertical();

                            EditorGUILayout.BeginVertical("Box");

                            _config.proceduralObjects[i].useRandomScaling = EditorGUILayout.Toggle("use random scaling:", _config.proceduralObjects[i].useRandomScaling);
                            GUI.enabled = _config.proceduralObjects[i].useRandomScaling;
                            _config.proceduralObjects[i].uniformScaling = EditorGUILayout.Toggle("uniform scaling:", _config.proceduralObjects[i].uniformScaling);

                            if (_config.proceduralObjects[i].uniformScaling)
                            {
                                _config.proceduralObjects[i].randomScalingMin.x = EditorGUILayout.FloatField("min random scaling:", _config.proceduralObjects[i].randomScalingMin.x);
                                _config.proceduralObjects[i].randomScalingMax.x = EditorGUILayout.FloatField("max random scaling:", _config.proceduralObjects[i].randomScalingMax.x);


                                _config.proceduralObjects[i].randomScalingMin = new Vector3(_config.proceduralObjects[i].randomScalingMin.x, _config.proceduralObjects[i].randomScalingMin.x, _config.proceduralObjects[i].randomScalingMin.x);
                                _config.proceduralObjects[i].randomScalingMax = new Vector3(_config.proceduralObjects[i].randomScalingMax.x, _config.proceduralObjects[i].randomScalingMax.x, _config.proceduralObjects[i].randomScalingMax.x);
                            }
                            else
                            {
                                _config.proceduralObjects[i].randomScalingMin = EditorGUILayout.Vector3Field("min random scaling: ", _config.proceduralObjects[i].randomScalingMin);
                                _config.proceduralObjects[i].randomScalingMax = EditorGUILayout.Vector3Field("max random scaling: ", _config.proceduralObjects[i].randomScalingMax);
                            }
                            GUI.enabled = true;
                            EditorGUILayout.EndVertical();



                            EditorGUILayout.EndVertical();
                            EditorGUI.indentLevel = 0;
                        }
                        else
                        {
                            ShowChildSpawnProperties(_config.proceduralObjects[i]);
                        }
                    }

                    GUI.enabled = true;
                }

                EditorGUILayout.EndVertical();

                if (twos == null ? GUI.enabled = false : GUI.enabled = true)
                {
                    if (!creator.mergeReady)
                    {
                        EditorGUILayout.HelpBox("Build map before spawning procedural objects", MessageType.Info);
                    }
                }

                if (creator != null)
                {
                    GUI.enabled = creator.mergeReady;
                }
                else
                {
                    GUI.enabled = false;
                }

                if (GUILayout.Button("Spawn procedural objects", GUILayout.Height(30)))
                {
                    twos.ScatterProceduralObjects();
                }

                GUI.enabled = true;
            }

            EditorGUILayout.EndVertical();
        }
        else
        {
            EditorGUILayout.EndVertical();
        }
    }