private void SetupIndicator()
        {
            //Debug.Log (indicator);
            if (indicator == null && indexes.Count > 0)
            {
                if (indicatorPrefab != null)
                {
                    indicator = SafeOperations.Instantiate(indicatorPrefab, indexes [0].transform);
                }
                else
                {
                    indicator = SafeOperations.NewGameObject("Indicator", indexes [0].transform, typeof(RectTransform), typeof(CanvasRenderer));
                    Image img = indicator.SafeAddComponent <Image> ();
                    img.sprite = knobSprite;
                    img.color  = Color.black;
                }
            }

            if (indicator != null)
            {
                indicator.name  = "Indicator";
                indicator.layer = 5;
                indicator.transform.localPosition = Vector3.zero;
            }

            ResizeIndicator();
        }
        private void ChangeLayoutGroup()
        {
            RectTransform tableRT     = GetComponent <RectTransform> ();
            Vector2       oldRectSize = new Vector2(tableRT.rect.width, tableRT.rect.height);

            float minSize = Mathf.Min(oldRectSize.x, oldRectSize.y);
            float maxSize = Mathf.Max(oldRectSize.x, oldRectSize.y);

            if (m_alignment == Alignment.Vertical)
            {
                if (GetComponent <VerticalLayoutGroup> () == null)
                {
                    HorizontalLayoutGroup hGroup = GetComponent <HorizontalLayoutGroup> ();
                    if (hGroup)
                    {
                        SafeOperations.Destroy(hGroup);
                    }

                    VerticalLayoutGroup layoutGroup = gameObject.SafeAddComponent <VerticalLayoutGroup> ();
                    tableRT.sizeDelta              = new Vector2(minSize, maxSize);
                    layoutGroup.childAlignment     = TextAnchor.MiddleCenter;
                    layoutGroup.childControlWidth  = false;
                    layoutGroup.childControlHeight = false;
                }
            }
            else
            {
                if (GetComponent <HorizontalLayoutGroup> () == null)
                {
                    VerticalLayoutGroup vGroup = GetComponent <VerticalLayoutGroup> ();
                    if (vGroup)
                    {
                        SafeOperations.Destroy(vGroup);
                    }

                    HorizontalLayoutGroup layoutGroup = gameObject.SafeAddComponent <HorizontalLayoutGroup> ();
                    tableRT.sizeDelta              = new Vector2(maxSize, minSize);
                    layoutGroup.childAlignment     = TextAnchor.MiddleCenter;
                    layoutGroup.childControlWidth  = false;
                    layoutGroup.childControlHeight = false;
                }
            }
        }
        private GameObject NewUIObject(string name, GameObject prefab, Transform parent, Sprite sprite, Color color)
        {
            GameObject GO;

            if (prefab != null)
            {
                GO = SafeOperations.Instantiate(prefab, parent);
            }
            else
            {
                GO = SafeOperations.NewGameObject(name, parent, typeof(RectTransform), typeof(CanvasRenderer));
                Image img = GO.SafeAddComponent <Image> ();
                img.sprite = sprite;
                img.color  = color;
                GO.layer   = 5;
            }

            return(GO);
        }
        private void SetupIndexes()
        {
            if (indexes.Count > transform.childCount)
            {
                indexes = indexes.Where(item => item != null).ToList();
            }
            else if (indexes.Count < transform.childCount)
            {
                indexes = new List <GameObject> ();
                foreach (Transform child in transform)
                {
                    indexes.Add(child.gameObject);
                }
            }

            if (transform.childCount < elementCount)
            {
                if (indexes.Count < elementCount)
                {
                    int startIndexCount = indexes.Count;
                    for (int i = startIndexCount; i < elementCount; i++)
                    {
                        GameObject newObject = NewUIObject("Index", indexPrefab, transform, bgSprite, Color.white);
                        indexes.Add(newObject);
                    }
                }
                else
                {
                    for (int i = 0; i < indexes.Count; i++)
                    {
                        if (indexes [i] == null)
                        {
                            indexes [i] = NewUIObject("Index", indexPrefab, transform, bgSprite, Color.white);
                        }
                    }
                }
            }
            else if (transform.childCount > elementCount)
            {
                for (int i = transform.childCount - 1; i >= elementCount; i--)
                {
                    GameObject go = transform.GetChild(i).gameObject;
                    SafeOperations.Destroy(go);
                    if (i < indexes.Count)
                    {
                        indexes.RemoveAt(i);
                    }
                }
            }

            for (int i = 0; i < indexes.Count; i++)
            {
                indexes [i].name = "Index " + (i + 1).ToString();
            }


            //// -- Cleaning up the list --

            //if (indexes.Count > transform.childCount)
            //    indexes = indexes.Where (item => item != null).ToList ();
            //else
            //{
            //    for (int i = transform.childCount - 1; i >= 0; --i)
            //    {
            //        var go = transform.GetChild (i).gameObject;
            //        if (!indexes.Contains (go))
            //            SafeOperations.Destroy (go);
            //    }
            //}

            //if (indexes.Count > elementCount)
            //{
            //    for (int i = transform.childCount - 1; i >= elementCount; i--)
            //    {
            //        GameObject go = transform.GetChild (i).gameObject;
            //        SafeOperations.Destroy (go);
            //        if (i < indexes.Count)
            //            indexes.RemoveAt (i);
            //    }
            //}
            //else
            //{
            //    for (int i = indexes.Count; i < elementCount; i++)
            //    {
            //        GameObject newObject = NewUIObject ("Index", indexPrefab, transform, bgSprite, Color.white);
            //        indexes.Add (newObject);
            //    }
            //}

            //for (int i = 0; i < indexes.Count; i++)
            //    if(indexes[i] != null) indexes [i].name = "Index " + (i + 1).ToString ();

            ResizeIndexes();
            SetupIndicator();
        }