Exemple #1
0
 /// <summary>
 ///		Allows to set all rect transform values at once.
 /// </summary>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="pivotAndAnchor"></param>
 public void SetRTransformValues(float x, float y, float width, float height, Vector2 pivotAndAnchor)
 {
     RTransform.anchoredPosition = new Vector2(x, y);
     RTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
     RTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
     Pivot = AnchorMin = AnchorMax = pivotAndAnchor;
 }
        public void UpdateUI()
        {
            Clear();

            int buttonObjectCount = Mathf.Min(m_pageCount, m_maxPageBtnCount);

            // the rect width is prev btn width + next btn width + page btn width * page count
            float width = GetWidth(m_previousButton) + GetWidth(m_nextButton) + (GetWidth(m_pageButton) * buttonObjectCount);

            RTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);

            // calculate the range of the shown buttons
            int idealOffsetMin = Mathf.Max(0, m_selectedPage - m_maxPageBtnCount);
            int idealOffsetMax = Mathf.Max(0, Mathf.Min(m_pageCount - m_maxPageBtnCount, m_selectedPage - 1));

            if (idealOffsetMin - 1 >= m_offset)
            {
                m_offset = idealOffsetMin;
            }
            else if (idealOffsetMax + 1 <= m_offset)
            {
                m_offset = idealOffsetMax;
            }

            // create page buttons
            SetText(m_pageButton, (1 + m_offset).ToString());
            SetOnClick(m_pageButton, 1 + m_offset);
            for (int pageNumber = 2; pageNumber <= buttonObjectCount; pageNumber++)
            {
                Button        pageBtn    = (Button)Instantiate(m_pageButton);
                RectTransform rTransform = pageBtn.GetComponent <RectTransform>();
                rTransform.SetParent(PageButtonTransform.parent, true);
                rTransform.localScale    = PageButtonTransform.localScale;
                rTransform.localPosition = PageButtonTransform.localPosition + Vector3.right * (pageNumber - 1) * GetWidth(m_pageButton);
                SetText(pageBtn, (pageNumber + m_offset).ToString());
                SetOnClick(pageBtn, pageNumber + m_offset);
                m_pageButtons.Add(pageBtn);
            }
            // highlight selected button
            for (int i = 0; i < m_pageButtons.Count; i++)
            {
                int pageNumber = i + 1 + m_offset;
                m_pageButtons[i].enabled = pageNumber != m_selectedPage;
            }
            // correct position of next button
            if (m_nextButton != null)
            {
                m_nextButton.GetComponent <RectTransform>().localPosition = PageButtonTransform.localPosition + Vector3.right * buttonObjectCount * GetWidth(m_pageButton);
            }
        }
Exemple #3
0
 /// <summary>
 ///		Sets the width and height of the view.
 /// </summary>
 /// <param name="width"></param>
 /// <param name="height"></param>
 public void SetSize(float width, float height)
 {
     RTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
     RTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
 }
Exemple #4
0
 public void SetTextures(Texture2D[] p_textures, int p_selectedIndex)
 {
     if (m_texturePrefab != null)
     {
         m_textures = p_textures;
         // delete all existing texture instances
         Destroy(m_selectionInstance);
         for (int i = 0; i < m_instances.Length; i++)
         {
             Destroy(m_instances[i]);
         }
         // create new texture prefab instances
         m_instances = new GameObject[p_textures.Length];
         float maxX = 0;
         for (int i = 0; i < p_textures.Length; i++)
         {
             // instantiate texture prefab
             m_instances[i] = (GameObject)Instantiate(m_texturePrefab);
             RectTransform instanceTransform = m_instances[i].GetComponent <RectTransform>();
             m_elementSize = instanceTransform.rect.width;
             SetRectTransformPosition(instanceTransform, i, m_elementSize);
             // apply texture
             RawImage image = TryFindComponent <RawImage>(m_instances[i]);
             if (image != null)
             {
                 image.texture = p_textures[i];
             }
             else
             {
                 Debug.LogError("uMyGUI_TexturePicker: SetTextures: TexturePrefab must have a RawImage component attached (can be in children).");
             }
             // link button callback
             if (m_buttonCallback != null)
             {
                 Button btn = TryFindComponent <Button>(m_instances[i]);
                 if (btn != null)
                 {
                     int indexCopy = i;
                     btn.onClick.AddListener(() => { m_buttonCallback(indexCopy); });
                 }
             }
             // calculate max x position to resize this rect transform
             maxX = instanceTransform.anchoredPosition.x + m_elementSize;
             // add selection prefab if needed
             if (i == p_selectedIndex)
             {
                 if (m_selectionPrefab != null)
                 {
                     // instantiate selection prefab
                     m_selectionInstance = (GameObject)Instantiate(m_selectionPrefab);
                     SetRectTransformPosition(m_selectionInstance.GetComponent <RectTransform>(), i, m_elementSize);
                 }
                 else
                 {
                     Debug.LogError("uMyGUI_TexturePicker: SetTextures: you have passed a non negative selection index '" + p_selectedIndex + "', but the SelectionPrefab was not provided in the inspector or via script!");
                 }
             }
         }
         // resize rect transform (e.g. to allow scrolling if scroll rect is the parent)
         RTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, maxX - RTransform.rect.xMin + m_offsetEnd);
     }
     else
     {
         Debug.LogError("uMyGUI_TexturePicker: SetTextures: you must provide the TexturePrefab in the inspector or via script!");
     }
 }