//void RasterizeTexture(Rect tmpRect, Texture texture)
    //{

    //    Rect txCords = new Rect(0, 0, 1, 1);

    //    if (tmpRect.xMax > grid.rect.xMax)
    //    {
    //        float tmpRectWidth = tmpRect.width;
    //        tmpRect.width *= ((Mathf.Min(tmpRect.xMax, grid.rect.xMax) - Mathf.Max(tmpRect.xMin, grid.rect.xMin)) / tmpRect.width);
    //        txCords = new Rect(0,
    //            0.0f,
    //            (tmpRect.width / tmpRectWidth),
    //            1);
    //    }
    //     if (tmpRect.xMin < grid.rect.xMin)
    //    {

    //        float tmpRectWidth = tmpRect.width;
    //        // float tmpRectXmin = tmpRect.xMin;
    //         tmpRect.xMin = grid.rect.xMin;


    //        tmpRect.width *= ((Mathf.Min(tmpRect.xMax, grid.rect.xMax) - Mathf.Max(tmpRect.xMin, grid.rect.xMin)) / tmpRect.width);
    //        txCords = new Rect(1 - ((Mathf.Min(tmpRect.xMax, grid.rect.xMax) - Mathf.Max(tmpRect.xMin, grid.rect.xMin)) / tmpRectWidth),
    //                           0.0f,
    //                           tmpRect.width / tmpRectWidth,
    //                           1);

    //    }


    //    if (tmpRect.yMax > grid.rect.yMax)
    //    {
    //        float tmpRectHeight = tmpRect.height;
    //        tmpRect.height *= ((Mathf.Min(tmpRect.yMax, grid.rect.yMax) - Mathf.Max(tmpRect.yMin, grid.rect.yMin)) / tmpRect.height);
    //        txCords = new Rect(txCords.xMin,
    //            1 - ((Mathf.Min(tmpRect.yMax, grid.rect.yMax) - Mathf.Max(tmpRect.yMin, grid.rect.yMin)) / tmpRectHeight),
    //            txCords.width,
    //            (tmpRect.height / tmpRectHeight));
    //    }

    //    if (tmpRect.yMin < grid.rect.yMin)
    //    {

    //        float tmpRectHeight = tmpRect.height;
    //        // float tmpRectYmin = tmpRect.yMin;
    //        tmpRect.yMin = grid.rect.yMin;


    //        tmpRect.height *= ((Mathf.Min(tmpRect.yMax, grid.rect.yMax) - Mathf.Max(tmpRect.yMin, grid.rect.yMin)) / tmpRect.height);
    //        txCords = new Rect(txCords.xMin,
    //                           0,
    //                           txCords.width,
    //                           tmpRect.height / tmpRectHeight);

    //    }
    //    if (!(tmpRect.xMin > grid.rect.xMax || tmpRect.xMax < grid.rect.xMin))
    //        if (!(tmpRect.yMin > grid.rect.yMax || tmpRect.yMax < grid.rect.yMin))
    //            GUI.DrawTextureWithTexCoords(tmpRect, texture, txCords);
    //}
    void OnGUI()
    {
        DeleteItem();
        ResizeSelectedTexture();
        if (GUI.Button(new Rect(160, 10, 50, 20), "Save"))
        {
            SaveChunk();
        }
        if (GUI.Button(new Rect(160, 35, 90, 20), "Moving COC"))
        {
            movingCenterOfChunk = true;
            autoAdjustCOC       = false;
        }
        if (GUI.Button(new Rect(120, 65, 50, 20), "COC"))
        {
            movingCenterOfChunk = false;
            autoAdjustCOC       = true;
        }
        if (GUI.Button(new Rect(215, 10, 50, 20), "Delete"))
        {
            DeleteChunk();
        }
        AddItem();
        if (dynamicSelection.objects.Count == 0)
        {
            Object_MouseSelector();
        }

        string temp = GUI.TextField(new Rect(10, 10, 140, 20), chunk.Name);

        if (chunk.Name != temp ||
            Canvas.width != grid.contentWidth ||
            Canvas.height != grid.contentHeight)
        {
            chunk.Name       = temp;
            noChangesApplied = false;
        }

        //GUI.Label(new Rect(250, 40, 100, 20), "Patch Height:");
        //grid.patchHeight = int.Parse(GUI.TextField(new Rect(325, 40, 80, 20),
        //grid.patchHeight.ToString()));
        //GUI.Label(new Rect(430, 40, 100, 20), "Patch Width:");
        //grid.patchWidth = int.Parse(GUI.TextField(new Rect(500, 40, 80, 20),
        //                                          grid.patchWidth.ToString()));


        float tmpGridScale = gridScale;

        GUI.Label(new Rect(590, 520, 120, 20), "gridScale:");
        gridScale = float.Parse(GUI.TextField(new Rect(655, 520, 50, 20), gridScale.ToString()));
        gridScale = GUI.VerticalScrollbar(gridScalerRect, gridScale, 0.05f, 4, 0.1f);
        if (tmpGridScale != gridScale)
        {
            grid.patchWidth  = grid.basePatchWidth * gridScale;
            grid.patchHeight = grid.basePatchHeight * gridScale;
            tmpGridScale     = gridScale;
        }
        GUI.Label(new Rect(250, 40, 100, 20), "Patch Height:");

        grid.basePatchHeight = float.Parse(GUI.TextField(new Rect(325, 40, 80, 20),
                                                         (grid.basePatchHeight).ToString()));
        //grid.patchHeight = 40 * gridScale;
        GUI.Label(new Rect(430, 40, 100, 20), "Patch Width:");

        grid.basePatchWidth = float.Parse(GUI.TextField(new Rect(500, 40, 80, 20),
                                                        (grid.basePatchWidth).ToString()));

        GUI.Label(new Rect(280, 10, 100, 20), "Chunk Height:");
        Canvas.height = int.Parse(GUI.TextField(new Rect(340, 10, 80, 20),
                                                Canvas.height.ToString()));
        if (Canvas.height < 1)
        {
            Canvas.height = 400;
        }

        GUI.Label(new Rect(440, 10, 100, 20), "Chunk Width:");
        Canvas.width = int.Parse(GUI.TextField(new Rect(500, 10, 80, 20),
                                               Canvas.width.ToString()));
        if (Canvas.width < 1)
        {
            Canvas.width = 800;
        }


        if (noChangesApplied)
        {
            GUI.DrawTexture(new Rect(115, 40, 20, 20), Resources.Load <Texture>("Checked"));
        }
        else
        {
            GUI.DrawTexture(new Rect(115, 40, 20, 20), Resources.Load <Texture>("NotChecked"));
        }

        if (GUI.Button(new Rect(10, 40, 100, 20), "Commit"))
        {
            CommitChanges();
        }
        if (selectedBlock != null)
        {
            float max = Mathf.Max(selectedBlock.width, selectedBlock.height);
            GUI.DrawTexture(new Rect(585, 10, 60 * (selectedBlock.width / max), 60 * (selectedBlock.height / max)), selectedBlock);
            GUI.Label(new Rect(585, 73, 100, 20), selectedBlock.width + " X " + selectedBlock.height);
        }
        if (GUI.Button(new Rect(10, 63, 100, 20), "Clear"))
        {
            foreach (var item in chunk.objects)
            {
                deletedItems.Add(item.texture + "." + item.id.ToString());
            }
            chunk.objects.RemoveAll(x => { return(true); });

            RefreshEditor();
        }
        if (GUI.Button(new Rect(270, 63, 80, 20), "Pick Scale"))
        {
            PickScale();
        }

        if (GUI.Button(new Rect(180, 63, 85, 20), "Pick Texture"))
        {
            PickTexture();
        }

        ChunkItemList.EventHandler();
        SavedChunksList.EventHandler();
        TextureList.EventHandler();

        hScroll = GUI.HorizontalScrollbar(new Rect(215, 490, window.position.xMax - window.position.xMin - 235, 10), hScroll, grid.basePatchWidth / gridScale, 0, Canvas.width - grid.rect.width);
        vScroll = GUI.VerticalScrollbar(new Rect(window.position.xMax - window.position.xMin - 20, 90, 10, 400), vScroll, grid.basePatchHeight / gridScale, 0, Canvas.height - grid.rect.height);

        vScroll = vScroll < 0 ? 0 : vScroll;
        hScroll = hScroll < 0 ? 0 : hScroll;

        grid.xMin = -hScroll * gridScale;
        grid.yMin = -vScroll * gridScale;
        for (int i = 0; i < layers.Length; i++)
        {
            foreach (var item in chunk.objects)
            {
                if (item.position.depth != ((i + 1) * 0.5f))
                {
                    continue;
                }
                if (dynamicSelection.objects.Contains(item))
                {
                    continue;
                }
                if (item.position.depth == ((activeLayer + 1) * 0.5f))
                {
                    Rect tmpRect = new Rect((((item.position.left - grid.rect.left - hScroll) * gridScale) + grid.rect.left),
                                            (((item.position.top - grid.rect.top - vScroll) * gridScale) + grid.rect.top),
                                            item.position.width * gridScale,
                                            item.position.height * gridScale);
                    //if (!(tmpRect.xMin < grid.rect.xMin ||
                    //     tmpRect.yMin < grid.rect.yMin ||
                    //     tmpRect.xMax > grid.rect.xMax ||
                    //     tmpRect.yMax > grid.rect.yMax))
                    //    GUI.DrawTexture(tmpRect, Resources.Load<Texture>("Objects/" + item.texture));
                    RTNew(tmpRect, Resources.Load <Texture>("Objects/" + item.texture));
                }

                if (GetActiveLayer().Contains((int)((item.position.depth) / 0.5f)))
                {
                    Rect tmpRect = new Rect((((item.position.left - grid.rect.left - hScroll) * gridScale) + grid.rect.left),
                                            (((item.position.top - grid.rect.top - vScroll) * gridScale) + grid.rect.top),
                                            item.position.width * gridScale,
                                            item.position.height * gridScale);
                    //if (!(tmpRect.xMin < grid.rect.xMin ||
                    //     tmpRect.yMin < grid.rect.yMin ||
                    //     tmpRect.xMax > grid.rect.xMax ||
                    //     tmpRect.yMax > grid.rect.yMax))
                    //    GUI.DrawTexture(tmpRect, Resources.Load<Texture>("Objects/" + item.texture));
                    RTNew(tmpRect, Resources.Load <Texture>("Objects/" + item.texture));
                }
            }
        }
        Rect tmpR = new Rect();

        //if(movingCenterOfChunk)
        tmpR = new Rect((chunk.centerOfChunk.left - 25 - (int)hScroll - grid.rect.left) * gridScale + grid.rect.left,
                        (chunk.centerOfChunk.top - 25 - (int)vScroll - grid.rect.top) * gridScale + grid.rect.top,
                        50 * gridScale,
                        50 * gridScale);
        if (autoAdjustCOC)
        {
            tmpR = new Rect((chunk.CenterOfChunk.left - 25 - (int)hScroll - grid.rect.left) * gridScale + grid.rect.left,
                            (chunk.CenterOfChunk.top - 25 - (int)vScroll - grid.rect.top) * gridScale + grid.rect.top,
                            50 * gridScale,
                            50 * gridScale);
        }
        //if (!(tmpR.xMin < grid.rect.xMin ||
        //     tmpR.yMin < grid.rect.yMin ||
        //     tmpR.xMax > grid.rect.xMax ||
        //     tmpR.yMax > grid.rect.yMax))

        //GUI.DrawTexture(tmpR, Resources.Load<Texture>("plus"));
        RTNew(tmpR, Resources.Load <Texture>("plus"));
        foreach (var item in dynamicSelection.objects)
        {
            if (GetActiveLayer().Contains((int)((item.position.depth) / 0.5f)))
            {
                Rect tmpRect = new Rect(((item.position.left - hScroll - grid.rect.left) * gridScale) + grid.rect.left + (dynamicSelection.boundry.xMin - dynamicSelection.firstPos.x),
                                        ((item.position.top - vScroll - grid.rect.top) * gridScale) + grid.rect.top + (dynamicSelection.boundry.yMin - dynamicSelection.firstPos.y),
                                        item.position.width * gridScale,
                                        item.position.height * gridScale);
                //if (!(tmpRect.xMin < grid.rect.xMin ||
                //     tmpRect.yMin < grid.rect.yMin ||
                //     tmpRect.xMax > grid.rect.xMax ||
                //     tmpRect.yMax > grid.rect.yMax))
                //    GUI.DrawTexture(tmpRect, Resources.Load<Texture>("Objects/" + item.texture));
                RTNew(tmpRect, Resources.Load <Texture>("Objects/" + item.texture));
            }
        }



        Rect tmpR2 = new Rect(((objSelectorGrid.position.left - hScroll - grid.rect.left) * gridScale) + grid.rect.left,
                              ((objSelectorGrid.position.top - grid.rect.top - vScroll) * gridScale) + grid.rect.top,
                              objSelectorGrid.position.width * gridScale,
                              objSelectorGrid.position.height * gridScale);

        //if (!(tmpR2.xMin < grid.rect.xMin ||
        //     tmpR2.yMin < grid.rect.yMin ||
        //     tmpR2.xMax > grid.rect.xMax ||
        //     tmpR2.yMax > grid.rect.yMax))

        //    GUI.DrawTexture(tmpR2, Resources.Load<Texture>(objSelectorGrid.texture));
        RTNew(tmpR2, Resources.Load <Texture>(objSelectorGrid.texture));
        GUI.DrawTexture(new Rect(225, 430, 50, 50), Resources.Load <Texture>("Coordinate"));
        MovingCOC();

        List <int> selectedLayers = GetActiveLayer();
        int        tempLayerNum   = GUI.SelectionGrid(new Rect(215, 520, 340, 60), activeLayer, layersName, 5);

        activeLayer = tempLayerNum;

        for (int i = 0; i < layers.Length; i++)
        {
            if (i == activeLayer)
            {
                layers[i]     = true;
                layersName[i] = "#L" + (i + 1).ToString();
                continue;
            }
            if (!Event.current.control)
            {
                layersName[i] = "L" + (i + 1).ToString();
                layers[i]     = false;
            }
        }
        grid.rect.xMax = window.position.xMax - window.position.xMin - 25;
        grid.Draw();
        AssociativeSeletion();
        window.Repaint();
    }