Exemplo n.º 1
0
    public void RepositionScrollableGrid(UIGridExt grid_)
    {
        //REPOSITION ITEMS GRID
        if (grid_ != null)
        {
            grid_.hideInactive = true;
            grid_.Reposition();

            UIDraggablePanelExt draggablePanel  = NGUITools.FindInParents <UIDraggablePanelExt>(grid_.gameObject) as UIDraggablePanelExt;
            UIPanel             scrollViewPanel = NGUITools.FindInParents <UIPanel>(grid_.gameObject) as UIPanel;

            if (scrollViewPanel != null && draggablePanel != null)
            {
                float panelWidth  = scrollViewPanel.clipRange.z;
                float contentSize = grid_.cellWidth * grid_.GetCells().Count;

                if (contentSize <= panelWidth)
                {
                    draggablePanel.relativePositionOnReset = new Vector2(0.5f, 0.5f);
                }
                else
                {
                    draggablePanel.relativePositionOnReset = new Vector2(0.0f, 0.5f);
                }

                draggablePanel.ResetPosition();
            }
        }
    }
Exemplo n.º 2
0
    public void InitializeGridWithAtlasAndRectsSafely(UIGridExt grid_, Texture2DAtlas atlas_, List <Rect> uvs_, Rect bgUv_)
    {
        if (uvs_ == null)
        {
            uvs_ = new List <Rect>();
        }

        int countOfItemsForDisplay_ = uvs_.Count;

        if (countOfItemsForDisplay_ == 0 && grid_ != null)
        {
            NGUITools.SetActive(grid_.gameObject, false);
        }
        else if (grid_ != null)
        {
            NGUITools.SetActive(grid_.gameObject, true);
            grid_.hideInactive = false;
            List <GameObject> existedItemCells_ = grid_.GetSortedCells();

            if (existedItemCells_.Count > 0)
            {
                //instantiate required cells
                for (int i = existedItemCells_.Count; i < countOfItemsForDisplay_; i++)
                {
                    grid_.AddCell(existedItemCells_[0]);
                }

                //destory unwanted cells
                for (int i = countOfItemsForDisplay_; i < existedItemCells_.Count; i++)
                {
                    grid_.RemoveCell(existedItemCells_[i]);
                }

                //setup all cells
                existedItemCells_ = grid_.GetSortedCells();
                for (int i = 0; i < countOfItemsForDisplay_; i++)
                {
                    GameObject cellGo_ = existedItemCells_[i];
                    NGUITools.SetActive(cellGo_, true);
                    UICountableItemCell cell_ = cellGo_.GetComponent <UICountableItemCell>();
                    if (cell_ != null)
                    {
                        if (atlas_ != null && i < uvs_.Count && cell_.iconTexture != null)
                        {
                            cell_.iconTexture.mainTexture = null;
                            cell_.iconTexture.material    = atlas_.AtlasMaterial;
                            cell_.iconTexture.uvRect      = uvs_[i];
                            cell_.iconTexture.ScaleToFit();
                        }
                        else if (cell_.iconTexture != null)
                        {
                            NGUITools.SetActive(cell_.iconTexture.gameObject, false);
                        }

                        if (atlas_ != null && cell_.backgroundTexture != null & bgUv_.width != 0f && bgUv_.height != 0f)
                        {
                            cell_.backgroundTexture.mainTexture = null;
                            cell_.backgroundTexture.material    = atlas_.AtlasMaterial;
                            cell_.backgroundTexture.uvRect      = bgUv_;
                            cell_.backgroundTexture.ScaleToFit();
                        }
                        else if (cell_.backgroundTexture != null)
                        {
                            NGUITools.SetActive(cell_.backgroundTexture.gameObject, false);
                        }

                        //deactive cell count label, because it is not required
                        if (cell_.countLabel != null)
                        {
                            NGUITools.SetActive(cell_.countLabel.gameObject, false);
                        }
                    }
                }
            }
        }
    }