/// <summary>
    /// Scroll bar template.
    /// </summary>

    void CreateScrollBar(GameObject go)
    {
        if (UISettings.atlas != null)
        {
            GUILayout.BeginHorizontal();
            string bg = UISpriteInspector.SpriteField(UISettings.atlas, "Background", mScrollBG, GUILayout.Width(200f));
            GUILayout.Space(20f);
            GUILayout.Label("Sprite for the background");
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            string fg = UISpriteInspector.SpriteField(UISettings.atlas, "Foreground", mScrollFG, GUILayout.Width(200f));
            GUILayout.Space(20f);
            GUILayout.Label("Sprite for the foreground (thumb)");
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            UIScrollBar.Direction dir = (UIScrollBar.Direction)EditorGUILayout.EnumPopup("Direction", mScrollDir, GUILayout.Width(200f));
            GUILayout.Space(20f);
            GUILayout.Label("Add colliders?", GUILayout.Width(80f));
            bool draggable = EditorGUILayout.Toggle(mScrollCL);
            GUILayout.EndHorizontal();

            if (mScrollBG != bg || mScrollFG != fg || mScrollCL != draggable || mScrollDir != dir)
            {
                mScrollBG  = bg;
                mScrollFG  = fg;
                mScrollCL  = draggable;
                mScrollDir = dir;
                Save();
            }
        }

        if (ShouldCreate(go, UISettings.atlas != null))
        {
            int depth = NGUITools.CalculateNextDepth(go);
            go      = NGUITools.AddChild(go);
            go.name = "Scroll Bar";

            UISlicedSprite bg = NGUITools.AddWidget <UISlicedSprite>(go);
            bg.name                 = "Background";
            bg.depth                = depth;
            bg.atlas                = UISettings.atlas;
            bg.spriteName           = mScrollBG;
            bg.transform.localScale = new Vector3(400f + bg.border.x + bg.border.z, 14f + bg.border.y + bg.border.w, 1f);
            bg.MakePixelPerfect();

            UISlicedSprite fg = NGUITools.AddWidget <UISlicedSprite>(go);
            fg.name       = "Foreground";
            fg.atlas      = UISettings.atlas;
            fg.spriteName = mScrollFG;

            UIScrollBar sb = go.AddComponent <UIScrollBar>();
            sb.background  = bg;
            sb.foreground  = fg;
            sb.direction   = mScrollDir;
            sb.barSize     = 0.3f;
            sb.scrollValue = 0.3f;
            sb.ForceUpdate();

            if (mScrollCL)
            {
                NGUITools.AddWidgetCollider(bg.gameObject);
                NGUITools.AddWidgetCollider(fg.gameObject);
            }
            Selection.activeGameObject = go;
        }
    }
    /// <summary>
    /// Button creation function.
    /// </summary>

    void CreateImageButton(GameObject go)
    {
        if (UISettings.atlas != null)
        {
            GUILayout.BeginHorizontal();
            string bg = UISpriteInspector.SpriteField(UISettings.atlas, "Normal", mImage0, GUILayout.Width(200f));
            GUILayout.Space(20f);
            GUILayout.Label("Normal state sprite");
            GUILayout.EndHorizontal();
            if (mImage0 != bg)
            {
                mImage0 = bg; Save();
            }

            GUILayout.BeginHorizontal();
            bg = UISpriteInspector.SpriteField(UISettings.atlas, "Hover", mImage1, GUILayout.Width(200f));
            GUILayout.Space(20f);
            GUILayout.Label("Hover state sprite");
            GUILayout.EndHorizontal();
            if (mImage1 != bg)
            {
                mImage1 = bg; Save();
            }

            GUILayout.BeginHorizontal();
            bg = UISpriteInspector.SpriteField(UISettings.atlas, "Pressed", mImage2, GUILayout.Width(200f));
            GUILayout.Space(20f);
            GUILayout.Label("Pressed state sprite");
            GUILayout.EndHorizontal();
            if (mImage2 != bg)
            {
                mImage2 = bg; Save();
            }
        }

        if (ShouldCreate(go, UISettings.atlas != null))
        {
            int depth = NGUITools.CalculateNextDepth(go);
            go      = NGUITools.AddChild(go);
            go.name = "Image Button";

            UIAtlas.Sprite sp     = UISettings.atlas.GetSprite(mImage0);
            UISprite       sprite = (sp.inner == sp.outer) ? NGUITools.AddWidget <UISprite>(go) :
                                    (UISprite)NGUITools.AddWidget <UISlicedSprite>(go);

            sprite.name                 = "Background";
            sprite.depth                = depth;
            sprite.atlas                = UISettings.atlas;
            sprite.spriteName           = mImage0;
            sprite.transform.localScale = new Vector3(150f, 40f, 1f);
            sprite.MakePixelPerfect();

            if (UISettings.font != null)
            {
                UILabel lbl = NGUITools.AddWidget <UILabel>(go);
                lbl.font = UISettings.font;
                lbl.text = go.name;
                lbl.MakePixelPerfect();
            }

            // Add a collider
            NGUITools.AddWidgetCollider(go);

            // Add the scripts
            UIImageButton ib = go.AddComponent <UIImageButton>();
            ib.target        = sprite;
            ib.normalSprite  = mImage0;
            ib.hoverSprite   = mImage1;
            ib.pressedSprite = mImage2;
            go.AddComponent <UIButtonSound>();

            Selection.activeGameObject = go;
        }
    }
    /// <summary>
    /// Checkbox creation function.
    /// </summary>

    void CreateCheckbox(GameObject go)
    {
        if (UISettings.atlas != null)
        {
            GUILayout.BeginHorizontal();
            string bg = UISpriteInspector.SpriteField(UISettings.atlas, "Background", mCheckBG, GUILayout.Width(200f));
            GUILayout.Space(20f);
            GUILayout.Label("Sliced Sprite for the background");
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            string ck = UISpriteInspector.SpriteField(UISettings.atlas, "Checkmark", mCheck, GUILayout.Width(200f));
            GUILayout.Space(20f);
            GUILayout.Label("Sprite visible when the state is 'checked'");
            GUILayout.EndHorizontal();

            if (mCheckBG != bg || mCheck != ck)
            {
                mCheckBG = bg;
                mCheck   = ck;
                Save();
            }
        }

        if (ShouldCreate(go, UISettings.atlas != null))
        {
            int depth = NGUITools.CalculateNextDepth(go);
            go      = NGUITools.AddChild(go);
            go.name = "Checkbox";

            UISlicedSprite bg = NGUITools.AddWidget <UISlicedSprite>(go);
            bg.name                 = "Background";
            bg.depth                = depth;
            bg.atlas                = UISettings.atlas;
            bg.spriteName           = mCheckBG;
            bg.transform.localScale = new Vector3(26f, 26f, 1f);
            bg.MakePixelPerfect();

            UISprite fg = NGUITools.AddWidget <UISprite>(go);
            fg.name       = "Checkmark";
            fg.atlas      = UISettings.atlas;
            fg.spriteName = mCheck;
            fg.MakePixelPerfect();

            if (UISettings.font != null)
            {
                UILabel lbl = NGUITools.AddWidget <UILabel>(go);
                lbl.font  = UISettings.font;
                lbl.text  = go.name;
                lbl.pivot = UIWidget.Pivot.Left;
                lbl.transform.localPosition = new Vector3(16f, 0f, 0f);
                lbl.MakePixelPerfect();
            }

            // Add a collider
            NGUITools.AddWidgetCollider(go);

            // Add the scripts
            go.AddComponent <UICheckbox>().checkSprite    = fg;
            go.AddComponent <UIButton>().tweenTarget      = bg.gameObject;
            go.AddComponent <UIButtonScale>().tweenTarget = bg.transform;
            go.AddComponent <UIButtonSound>();

            Selection.activeGameObject = go;
        }
    }
예제 #4
0
    /// <summary>
    /// Progress bar creation function.
    /// </summary>

    void CreateSlider(GameObject go, bool slider)
    {
        if (NGUISettings.atlas != null)
        {
            NGUIEditorTools.DrawSpriteField("Empty", "Sprite for the background (empty bar)", NGUISettings.atlas, mSliderBG, OnSliderBG, GUILayout.Width(120f));
            NGUIEditorTools.DrawSpriteField("Full", "Sprite for the foreground (full bar)", NGUISettings.atlas, mSliderFG, OnSliderFG, GUILayout.Width(120f));

            if (slider)
            {
                NGUIEditorTools.DrawSpriteField("Thumb", "Sprite for the thumb indicator", NGUISettings.atlas, mSliderTB, OnSliderTB, GUILayout.Width(120f));
            }
        }

        if (ShouldCreate(go, NGUISettings.atlas != null))
        {
            int depth = NGUITools.CalculateNextDepth(go);
            go      = NGUITools.AddChild(go);
            go.name = slider ? "Slider" : "Progress Bar";

            // Background sprite
            UISpriteData bgs  = NGUISettings.atlas.GetSprite(mSliderBG);
            UISprite     back = (UISprite)NGUITools.AddWidget <UISprite>(go);

            back.type       = bgs.hasBorder ? UISprite.Type.Sliced : UISprite.Type.Simple;
            back.name       = "Background";
            back.depth      = depth;
            back.pivot      = UIWidget.Pivot.Left;
            back.atlas      = NGUISettings.atlas;
            back.spriteName = mSliderBG;
            back.width      = 200;
            back.height     = 30;
            back.transform.localPosition = Vector3.zero;
            back.MakePixelPerfect();

            // Foreground sprite
            UISpriteData fgs   = NGUISettings.atlas.GetSprite(mSliderFG);
            UISprite     front = NGUITools.AddWidget <UISprite>(go);
            front.type       = fgs.hasBorder ? UISprite.Type.Sliced : UISprite.Type.Simple;
            front.name       = "Foreground";
            front.pivot      = UIWidget.Pivot.Left;
            front.atlas      = NGUISettings.atlas;
            front.spriteName = mSliderFG;
            front.width      = 200;
            front.height     = 30;
            front.transform.localPosition = Vector3.zero;
            front.MakePixelPerfect();

            // Add a collider
            if (slider)
            {
                NGUITools.AddWidgetCollider(go);
            }

            // Add the slider script
            UISlider uiSlider = go.AddComponent <UISlider>();
            uiSlider.foregroundWidget = front;

            // Thumb sprite
            if (slider)
            {
                UISpriteData tbs = NGUISettings.atlas.GetSprite(mSliderTB);
                UISprite     thb = NGUITools.AddWidget <UISprite>(go);

                thb.type       = tbs.hasBorder ? UISprite.Type.Sliced : UISprite.Type.Simple;
                thb.name       = "Thumb";
                thb.atlas      = NGUISettings.atlas;
                thb.spriteName = mSliderTB;
                thb.width      = 20;
                thb.height     = 40;
                thb.transform.localPosition = new Vector3(200f, 0f, 0f);
                thb.MakePixelPerfect();

                NGUITools.AddWidgetCollider(thb.gameObject);
                thb.gameObject.AddComponent <UIButtonColor>();
                thb.gameObject.AddComponent <UIButtonScale>();

                uiSlider.thumb = thb.transform;
            }
            uiSlider.value = 1f;

            // Select the slider
            Selection.activeGameObject = go;
        }
    }
예제 #5
0
    /// <summary>
    /// Create a popup list or a menu.
    /// </summary>

    void CreatePopup(GameObject go, bool isDropDown)
    {
        if (NGUISettings.atlas != null)
        {
            NGUIEditorTools.DrawSpriteField("Foreground", "Foreground sprite (shown on the button)", NGUISettings.atlas, mListFG, OnListFG, GUILayout.Width(120f));
            NGUIEditorTools.DrawSpriteField("Background", "Background sprite (envelops the options)", NGUISettings.atlas, mListBG, OnListBG, GUILayout.Width(120f));
            NGUIEditorTools.DrawSpriteField("Highlight", "Sprite used to highlight the selected option", NGUISettings.atlas, mListHL, OnListHL, GUILayout.Width(120f));
        }

        if (ShouldCreate(go, NGUISettings.atlas != null && NGUISettings.ambigiousFont != null))
        {
            int depth = NGUITools.CalculateNextDepth(go);
            go      = NGUITools.AddChild(go);
            go.name = isDropDown ? "Popup List" : "Popup Menu";

            UISpriteData sphl = NGUISettings.atlas.GetSprite(mListHL);
            UISpriteData spfg = NGUISettings.atlas.GetSprite(mListFG);

            Vector2 hlPadding = new Vector2(Mathf.Max(4f, sphl.paddingLeft), Mathf.Max(4f, sphl.paddingTop));
            Vector2 fgPadding = new Vector2(Mathf.Max(4f, spfg.paddingLeft), Mathf.Max(4f, spfg.paddingTop));

            // Background sprite
            UISprite sprite = NGUITools.AddSprite(go, NGUISettings.atlas, mListFG);
            sprite.depth  = depth;
            sprite.atlas  = NGUISettings.atlas;
            sprite.pivot  = UIWidget.Pivot.Left;
            sprite.width  = Mathf.RoundToInt(150f + fgPadding.x * 2f);
            sprite.height = Mathf.RoundToInt(NGUISettings.fontSize + fgPadding.y * 2f);
            sprite.transform.localPosition = Vector3.zero;
            sprite.MakePixelPerfect();

            // Text label
            UILabel lbl = NGUITools.AddWidget <UILabel>(go);
            lbl.ambigiousFont = NGUISettings.ambigiousFont;
            lbl.fontSize      = NGUISettings.fontSize;
            lbl.fontStyle     = NGUISettings.fontStyle;
            lbl.text          = go.name;
            lbl.pivot         = UIWidget.Pivot.Left;
            lbl.cachedTransform.localPosition = new Vector3(fgPadding.x, 0f, 0f);
            lbl.AssumeNaturalSize();

            // Add a collider
            NGUITools.AddWidgetCollider(go);

            // Add the popup list
            UIPopupList list = go.AddComponent <UIPopupList>();
            list.atlas            = NGUISettings.atlas;
            list.ambigiousFont    = NGUISettings.ambigiousFont;
            list.fontSize         = NGUISettings.fontSize;
            list.fontStyle        = NGUISettings.fontStyle;
            list.backgroundSprite = mListBG;
            list.highlightSprite  = mListHL;
            list.padding          = hlPadding;
            if (isDropDown)
            {
                EventDelegate.Add(list.onChange, lbl.SetCurrentSelection);
            }
            for (int i = 0; i < 5; ++i)
            {
                list.items.Add(isDropDown ? ("List Option " + i) : ("Menu Option " + i));
            }

            // Add the scripts
            go.AddComponent <UIButton>().tweenTarget = sprite.gameObject;
            go.AddComponent <UIPlaySound>();

            Selection.activeGameObject = go;
        }
    }
예제 #6
0
 public void Show()
 {
     if (base.enabled && NGUITools.GetActive(base.gameObject) && mChild == null && atlas != null && isValid && items.Count > 0)
     {
         mLabelList.Clear();
         if (mPanel == null)
         {
             mPanel = UIPanel.Find(base.transform);
             if (mPanel == null)
             {
                 return;
             }
         }
         handleEvents = true;
         Transform transform = base.transform;
         Bounds    bounds    = NGUIMath.CalculateRelativeWidgetBounds(transform.parent, transform);
         mChild       = new GameObject("Drop-down List");
         mChild.layer = base.gameObject.layer;
         Transform transform2 = mChild.transform;
         transform2.parent        = transform.parent;
         transform2.localPosition = bounds.min;
         transform2.localRotation = Quaternion.identity;
         transform2.localScale    = Vector3.one;
         mBackground       = NGUITools.AddSprite(mChild, atlas, backgroundSprite);
         mBackground.pivot = UIWidget.Pivot.TopLeft;
         mBackground.depth = NGUITools.CalculateNextDepth(mPanel.gameObject);
         mBackground.color = backgroundColor;
         Vector4 border = mBackground.border;
         mBgBorder = border.y;
         mBackground.cachedTransform.localPosition = new Vector3(0f, border.y, 0f);
         mHighlight       = NGUITools.AddSprite(mChild, atlas, highlightSprite);
         mHighlight.pivot = UIWidget.Pivot.TopLeft;
         mHighlight.color = highlightColor;
         UISpriteData atlasSprite = mHighlight.GetAtlasSprite();
         if (atlasSprite != null)
         {
             float          num             = (float)atlasSprite.borderTop;
             float          num2            = (float)activeFontSize;
             float          activeFontScale = this.activeFontScale;
             float          num3            = num2 * activeFontScale;
             float          num4            = 0f;
             float          num5            = 0f - padding.y;
             List <UILabel> list            = new List <UILabel>();
             if (!items.Contains(mSelectedItem))
             {
                 mSelectedItem = null;
             }
             int i = 0;
             for (int count = items.Count; i < count; i++)
             {
                 string  text    = items[i];
                 UILabel uILabel = NGUITools.AddWidget <UILabel>(mChild);
                 uILabel.name         = i.ToString();
                 uILabel.pivot        = UIWidget.Pivot.TopLeft;
                 uILabel.bitmapFont   = bitmapFont;
                 uILabel.trueTypeFont = trueTypeFont;
                 uILabel.fontSize     = fontSize;
                 uILabel.fontStyle    = fontStyle;
                 uILabel.text         = ((!isLocalized) ? text : Localization.Get(text));
                 uILabel.color        = textColor;
                 Transform cachedTransform = uILabel.cachedTransform;
                 float     num6            = border.x + padding.x;
                 Vector2   pivotOffset     = uILabel.pivotOffset;
                 cachedTransform.localPosition = new Vector3(num6 - pivotOffset.x, num5, -1f);
                 uILabel.overflowMethod        = UILabel.Overflow.ResizeFreely;
                 uILabel.alignment             = alignment;
                 list.Add(uILabel);
                 num5 -= num3;
                 num5 -= padding.y;
                 float   a           = num4;
                 Vector2 printedSize = uILabel.printedSize;
                 num4 = Mathf.Max(a, printedSize.x);
                 UIEventListener uIEventListener = UIEventListener.Get(uILabel.gameObject);
                 uIEventListener.onHover   = OnItemHover;
                 uIEventListener.onPress   = OnItemPress;
                 uIEventListener.onClick   = OnItemClick;
                 uIEventListener.parameter = text;
                 if (mSelectedItem == text || (i == 0 && string.IsNullOrEmpty(mSelectedItem)))
                 {
                     Highlight(uILabel, instant: true);
                 }
                 mLabelList.Add(uILabel);
             }
             float   a2   = num4;
             Vector3 size = bounds.size;
             num4 = Mathf.Max(a2, size.x * activeFontScale - (border.x + padding.x) * 2f);
             float   num7    = num4;
             Vector3 vector  = new Vector3(num7 * 0.5f, (0f - num2) * 0.5f, 0f);
             Vector3 vector2 = new Vector3(num7, num3 + padding.y, 1f);
             int     j       = 0;
             for (int count2 = list.Count; j < count2; j++)
             {
                 UILabel uILabel2 = list[j];
                 NGUITools.AddWidgetCollider(uILabel2.gameObject);
                 uILabel2.autoResizeBoxCollider = false;
                 BoxCollider component = uILabel2.GetComponent <BoxCollider>();
                 if (component != null)
                 {
                     Vector3 center = component.center;
                     vector.z         = center.z;
                     component.center = vector;
                     component.size   = vector2;
                 }
                 else
                 {
                     BoxCollider2D component2 = uILabel2.GetComponent <BoxCollider2D>();
                     component2.offset = vector;
                     component2.size   = vector2;
                 }
             }
             int width = Mathf.RoundToInt(num4);
             num4 += (border.x + padding.x) * 2f;
             num5 -= border.y;
             mBackground.width  = Mathf.RoundToInt(num4);
             mBackground.height = Mathf.RoundToInt(0f - num5 + border.y);
             int k = 0;
             for (int count3 = list.Count; k < count3; k++)
             {
                 UILabel uILabel3 = list[k];
                 uILabel3.overflowMethod = UILabel.Overflow.ShrinkContent;
                 uILabel3.width          = width;
             }
             float num8 = 2f * atlas.pixelSize;
             float f    = num4 - (border.x + padding.x) * 2f + (float)atlasSprite.borderLeft * num8;
             float f2   = num3 + num * num8;
             mHighlight.width  = Mathf.RoundToInt(f);
             mHighlight.height = Mathf.RoundToInt(f2);
             bool flag = position == Position.Above;
             if (position == Position.Auto)
             {
                 UICamera uICamera = UICamera.FindCameraForLayer(base.gameObject.layer);
                 if (uICamera != null)
                 {
                     Vector3 vector3 = uICamera.cachedCamera.WorldToViewportPoint(transform.position);
                     flag = (vector3.y < 0.5f);
                 }
             }
             if (isAnimated)
             {
                 float bottom = num5 + num3;
                 Animate(mHighlight, flag, bottom);
                 int l = 0;
                 for (int count4 = list.Count; l < count4; l++)
                 {
                     Animate(list[l], flag, bottom);
                 }
                 AnimateColor(mBackground);
                 AnimateScale(mBackground, flag, bottom);
             }
             if (flag)
             {
                 Transform transform3 = transform2;
                 Vector3   min        = bounds.min;
                 float     x          = min.x;
                 Vector3   max        = bounds.max;
                 float     y          = max.y - num5 - border.y;
                 Vector3   min2       = bounds.min;
                 transform3.localPosition = new Vector3(x, y, min2.z);
             }
         }
     }
     else
     {
         OnSelect(isSelected: false);
     }
 }
예제 #7
0
    /// <summary>
    /// Scroll bar template.
    /// </summary>

    void CreateScrollBar(GameObject go)
    {
        if (NGUISettings.atlas != null)
        {
            NGUIEditorTools.DrawSpriteField("Background", "Sprite used for the background", NGUISettings.atlas, mScrollBG, OnScrollBG, GUILayout.Width(120f));
            NGUIEditorTools.DrawSpriteField("Foreground", "Sprite used for the foreground (thumb)", NGUISettings.atlas, mScrollFG, OnScrollFG, GUILayout.Width(120f));

            GUILayout.BeginHorizontal();
            UIScrollBar.FillDirection dir = (UIScrollBar.FillDirection)EditorGUILayout.EnumPopup("Direction", mFillDir, GUILayout.Width(200f));
            GUILayout.Space(20f);
            GUILayout.Label("Add colliders?", GUILayout.Width(90f));
            bool draggable = EditorGUILayout.Toggle(mScrollCL);
            GUILayout.EndHorizontal();

            if (mScrollCL != draggable || mFillDir != dir)
            {
                mScrollCL = draggable;
                mFillDir  = dir;
                Save();
            }
        }

        if (ShouldCreate(go, NGUISettings.atlas != null))
        {
            int depth = NGUITools.CalculateNextDepth(go);
            go      = NGUITools.AddChild(go);
            go.name = "Scroll Bar";

            UISprite bg = NGUITools.AddWidget <UISprite>(go);
            bg.type       = UISprite.Type.Sliced;
            bg.name       = "Background";
            bg.depth      = depth;
            bg.atlas      = NGUISettings.atlas;
            bg.spriteName = mScrollBG;

            Vector4 border = bg.border;
            bg.width  = Mathf.RoundToInt(400f + border.x + border.z);
            bg.height = Mathf.RoundToInt(14f + border.y + border.w);
            bg.MakePixelPerfect();

            UISprite fg = NGUITools.AddWidget <UISprite>(go);
            fg.type       = UISprite.Type.Sliced;
            fg.name       = "Foreground";
            fg.atlas      = NGUISettings.atlas;
            fg.spriteName = mScrollFG;

            UIScrollBar sb = go.AddComponent <UIScrollBar>();
            sb.foregroundWidget = fg;
            sb.backgroundWidget = bg;
            sb.fillDirection    = mFillDir;
            sb.barSize          = 0.3f;
            sb.value            = 0.3f;
            sb.ForceUpdate();

            if (mScrollCL)
            {
                NGUITools.AddWidgetCollider(bg.gameObject);
                NGUITools.AddWidgetCollider(fg.gameObject);
            }
            Selection.activeGameObject = go;
        }
    }
예제 #8
0
    /// <summary>
    /// Display the drop-down list when the game object gets clicked on.
    /// </summary>

    void OnClick()
    {
        if (enabled && NGUITools.GetActive(gameObject) && mChild == null && atlas != null && isValid && items.Count > 0)
        {
            mLabelList.Clear();

            // Automatically locate the panel responsible for this object
            if (mPanel == null)
            {
                mPanel = UIPanel.Find(transform);
                if (mPanel == null)
                {
                    return;
                }
            }

            // Disable the navigation script
            handleEvents = true;

            // Calculate the dimensions of the object triggering the popup list so we can position it below it
            Transform myTrans = transform;
            Bounds    bounds  = NGUIMath.CalculateRelativeWidgetBounds(myTrans.parent, myTrans);

            // Create the root object for the list
            mChild       = new GameObject("Drop-down List");
            mChild.layer = gameObject.layer;

            Transform t = mChild.transform;
            t.parent        = myTrans.parent;
            t.localPosition = bounds.min;
            t.localRotation = Quaternion.identity;
            t.localScale    = Vector3.one;

            // Add a sprite for the background
            mBackground       = NGUITools.AddSprite(mChild, atlas, backgroundSprite);
            mBackground.pivot = UIWidget.Pivot.TopLeft;
            mBackground.depth = NGUITools.CalculateNextDepth(mPanel.gameObject);
            mBackground.color = backgroundColor;

            // We need to know the size of the background sprite for padding purposes
            Vector4 bgPadding = mBackground.border;
            mBgBorder = bgPadding.y;
            mBackground.cachedTransform.localPosition = new Vector3(0f, bgPadding.y, 0f);

            // Add a sprite used for the selection
            mHighlight       = NGUITools.AddSprite(mChild, atlas, highlightSprite);
            mHighlight.pivot = UIWidget.Pivot.TopLeft;
            mHighlight.color = highlightColor;

            UISpriteData hlsp = mHighlight.GetAtlasSprite();
            if (hlsp == null)
            {
                return;
            }

            float          hlspHeight = hlsp.borderTop;
            float          fontHeight = activeFontSize;
            float          dynScale = activeFontScale;
            float          labelHeight = fontHeight * dynScale;
            float          x = 0f, y = -padding.y;
            int            labelFontSize = (bitmapFont != null) ? bitmapFont.defaultSize : fontSize;
            List <UILabel> labels        = new List <UILabel>();

            // Run through all items and create labels for each one
            for (int i = 0, imax = items.Count; i < imax; ++i)
            {
                string s = items[i];

                UILabel lbl = NGUITools.AddWidget <UILabel>(mChild);
                lbl.name         = i.ToString();
                lbl.pivot        = UIWidget.Pivot.TopLeft;
                lbl.bitmapFont   = bitmapFont;
                lbl.trueTypeFont = trueTypeFont;
                lbl.fontSize     = labelFontSize;
                lbl.fontStyle    = fontStyle;
                lbl.text         = isLocalized ? Localization.Get(s) : s;
                lbl.color        = textColor;
                lbl.cachedTransform.localPosition = new Vector3(bgPadding.x + padding.x, y, -1f);
                lbl.overflowMethod = UILabel.Overflow.ResizeFreely;
                lbl.alignment      = alignment;
                lbl.MakePixelPerfect();
                if (dynScale != 1f)
                {
                    lbl.cachedTransform.localScale = Vector3.one * dynScale;
                }
                labels.Add(lbl);

                y -= labelHeight;
                y -= padding.y;
                x  = Mathf.Max(x, lbl.printedSize.x);

                // Add an event listener
                UIEventListener listener = UIEventListener.Get(lbl.gameObject);
                listener.onHover   = OnItemHover;
                listener.onPress   = OnItemPress;
                listener.onClick   = OnItemClick;
                listener.parameter = s;

                // Move the selection here if this is the right label
                if (mSelectedItem == s || (i == 0 && string.IsNullOrEmpty(mSelectedItem)))
                {
                    Highlight(lbl, true);
                }

                // Add this label to the list
                mLabelList.Add(lbl);
            }

            // The triggering widget's width should be the minimum allowed width
            x = Mathf.Max(x, bounds.size.x * dynScale - (bgPadding.x + padding.x) * 2f);

            float   cx       = x / dynScale;
            Vector3 bcCenter = new Vector3(cx * 0.5f, -fontHeight * 0.5f, 0f);
            Vector3 bcSize   = new Vector3(cx, (labelHeight + padding.y) / dynScale, 1f);

            // Run through all labels and add colliders
            for (int i = 0, imax = labels.Count; i < imax; ++i)
            {
                UILabel lbl = labels[i];
                NGUITools.AddWidgetCollider(lbl.gameObject);
                lbl.autoResizeBoxCollider = false;
                BoxCollider bc = lbl.GetComponent <BoxCollider>();

                if (bc != null)
                {
                    bcCenter.z = bc.center.z;
                    bc.center  = bcCenter;
                    bc.size    = bcSize;
                }
                else
                {
                    BoxCollider2D b2d = lbl.GetComponent <BoxCollider2D>();
                    b2d.offset = bcCenter;
                    b2d.size   = bcSize;
                }
            }

            int lblWidth = Mathf.RoundToInt(x);
            x += (bgPadding.x + padding.x) * 2f;
            y -= bgPadding.y;

            // Scale the background sprite to envelop the entire set of items
            mBackground.width  = Mathf.RoundToInt(x);
            mBackground.height = Mathf.RoundToInt(-y + bgPadding.y);

            // Set the label width to make alignment work
            for (int i = 0, imax = labels.Count; i < imax; ++i)
            {
                UILabel lbl = labels[i];
                lbl.overflowMethod = UILabel.Overflow.ShrinkContent;
                lbl.width          = lblWidth;
            }

            // Scale the highlight sprite to envelop a single item
            float scaleFactor = 2f * atlas.pixelSize;
            float w           = x - (bgPadding.x + padding.x) * 2f + hlsp.borderLeft * scaleFactor;
            float h           = labelHeight + hlspHeight * scaleFactor;
            mHighlight.width  = Mathf.RoundToInt(w);
            mHighlight.height = Mathf.RoundToInt(h);

            bool placeAbove = (position == Position.Above);

            if (position == Position.Auto)
            {
                UICamera cam = UICamera.FindCameraForLayer(gameObject.layer);

                if (cam != null)
                {
                    Vector3 viewPos = cam.cachedCamera.WorldToViewportPoint(myTrans.position);
                    placeAbove = (viewPos.y < 0.5f);
                }
            }

            // If the list should be animated, let's animate it by expanding it
            if (isAnimated)
            {
                float bottom = y + labelHeight;
                Animate(mHighlight, placeAbove, bottom);
                for (int i = 0, imax = labels.Count; i < imax; ++i)
                {
                    Animate(labels[i], placeAbove, bottom);
                }
                AnimateColor(mBackground);
                AnimateScale(mBackground, placeAbove, bottom);
            }

            // If we need to place the popup list above the item, we need to reposition everything by the size of the list
            if (placeAbove)
            {
                t.localPosition = new Vector3(bounds.min.x, bounds.max.y - y - bgPadding.y, bounds.min.z);
            }
        }
        else
        {
            OnSelect(false);
        }
    }
예제 #9
0
    public virtual void Show()
    {
        if (enabled && NGUITools.GetActive(gameObject) && mChild == null && atlas != null && isValid && items.Count > 0)
        {
            mLabelList.Clear();
            StopCoroutine("CloseIfUnselected");

            // Ensure the popup's source has the selection
            UICamera.selectedObject = (UICamera.hoveredObject ?? gameObject);
            mSelection = UICamera.selectedObject;
            source     = UICamera.selectedObject;

            if (source == null)
            {
                Debug.LogError("Popup list needs a source object...");
                return;
            }

            mOpenFrame = Time.frameCount;

            // Automatically locate the panel responsible for this object
            if (mPanel == null)
            {
                mPanel = UIPanel.Find(transform);
                if (mPanel == null)
                {
                    return;
                }
            }

            // Calculate the dimensions of the object triggering the popup list so we can position it below it
            Vector3 min;
            Vector3 max;

            // Create the root object for the list
            mChild       = new GameObject("Drop-down List");
            mChild.layer = gameObject.layer;

            if (separatePanel)
            {
                if (GetComponent <Collider>() != null)
                {
                    Rigidbody rb = mChild.AddComponent <Rigidbody>();
                    rb.isKinematic = true;
                }
                else if (GetComponent <Collider2D>() != null)
                {
                    Rigidbody2D rb = mChild.AddComponent <Rigidbody2D>();
                    rb.isKinematic = true;
                }
                mChild.AddComponent <UIPanel>().depth = 1000000;
            }
            current = this;

            Transform t = mChild.transform;
            t.parent = mPanel.cachedTransform;
            Vector3 pos;

            // Manually triggered popup list on some other game object
            if (openOn == OpenOn.Manual && mSelection != gameObject)
            {
                pos             = UICamera.lastEventPosition;
                min             = mPanel.cachedTransform.InverseTransformPoint(mPanel.anchorCamera.ScreenToWorldPoint(pos));
                max             = min;
                t.localPosition = min;
                pos             = t.position;
            }
            else
            {
                Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(mPanel.cachedTransform, transform, false, false);
                min             = bounds.min;
                max             = bounds.max;
                t.localPosition = min;
                pos             = t.position;
            }

            StartCoroutine("CloseIfUnselected");

            t.localRotation = Quaternion.identity;
            t.localScale    = Vector3.one;

            // Add a sprite for the background
            mBackground       = NGUITools.AddSprite(mChild, atlas, backgroundSprite, separatePanel ? 0 : NGUITools.CalculateNextDepth(mPanel.gameObject));
            mBackground.pivot = UIWidget.Pivot.TopLeft;
            mBackground.color = backgroundColor;

            // We need to know the size of the background sprite for padding purposes
            Vector4 bgPadding = mBackground.border;
            mBgBorder = bgPadding.y;
            mBackground.cachedTransform.localPosition = new Vector3(0f, bgPadding.y, 0f);

            // Add a sprite used for the selection
            mHighlight       = NGUITools.AddSprite(mChild, atlas, highlightSprite, mBackground.depth + 1);
            mHighlight.pivot = UIWidget.Pivot.TopLeft;
            mHighlight.color = highlightColor;

            UISpriteData hlsp = mHighlight.GetAtlasSprite();
            if (hlsp == null)
            {
                return;
            }

            float          hlspHeight = hlsp.borderTop;
            float          fontHeight = activeFontSize;
            float          dynScale = activeFontScale;
            float          labelHeight = fontHeight * dynScale;
            float          x = 0f, y = -padding.y;
            List <UILabel> labels = new List <UILabel>();

            // Clear the selection if it's no longer present
            if (!items.Contains(mSelectedItem))
            {
                mSelectedItem = null;
            }

            // Run through all items and create labels for each one
            for (int i = 0, imax = items.Count; i < imax; ++i)
            {
                string s = items[i];

                UILabel lbl = NGUITools.AddWidget <UILabel>(mChild, mBackground.depth + 2);
                lbl.name         = i.ToString();
                lbl.pivot        = UIWidget.Pivot.TopLeft;
                lbl.bitmapFont   = bitmapFont;
                lbl.trueTypeFont = trueTypeFont;
                lbl.fontSize     = fontSize;
                lbl.fontStyle    = fontStyle;
                lbl.text         = isLocalized ? Localization.Get(s) : s;
                lbl.color        = textColor;
                lbl.cachedTransform.localPosition = new Vector3(bgPadding.x + padding.x - lbl.pivotOffset.x, y, -1f);
                lbl.overflowMethod = UILabel.Overflow.ResizeFreely;
                lbl.alignment      = alignment;
                labels.Add(lbl);

                y -= labelHeight;
                y -= padding.y;
                x  = Mathf.Max(x, lbl.printedSize.x);

                // Add an event listener
                UIEventListener listener = UIEventListener.Get(lbl.gameObject);
                listener.onHover   = OnItemHover;
                listener.onPress   = OnItemPress;
                listener.parameter = s;

                // Move the selection here if this is the right label
                if (mSelectedItem == s || (i == 0 && string.IsNullOrEmpty(mSelectedItem)))
                {
                    Highlight(lbl, true);
                }

                // Add this label to the list
                mLabelList.Add(lbl);
            }

            // The triggering widget's width should be the minimum allowed width
            x = Mathf.Max(x, (max.x - min.x) - (bgPadding.x + padding.x) * 2f);

            float   cx       = x;
            Vector3 bcCenter = new Vector3(cx * 0.5f, -labelHeight * 0.5f, 0f);
            Vector3 bcSize   = new Vector3(cx, (labelHeight + padding.y), 1f);

            // Run through all labels and add colliders
            for (int i = 0, imax = labels.Count; i < imax; ++i)
            {
                UILabel lbl = labels[i];
                NGUITools.AddWidgetCollider(lbl.gameObject);
                lbl.autoResizeBoxCollider = false;
                BoxCollider bc = lbl.GetComponent <BoxCollider>();

                if (bc != null)
                {
                    bcCenter.z = bc.center.z;
                    bc.center  = bcCenter;
                    bc.size    = bcSize;
                }
                else
                {
                    BoxCollider2D b2d = lbl.GetComponent <BoxCollider2D>();
                                        #if UNITY_4_3 || UNITY_4_5 || UNITY_4_6
                    b2d.center = bcCenter;
                                        #else
                    b2d.offset = bcCenter;
                                        #endif
                    b2d.size = bcSize;
                }
            }

            int lblWidth = Mathf.RoundToInt(x);
            x += (bgPadding.x + padding.x) * 2f;
            y -= bgPadding.y;

            // Scale the background sprite to envelop the entire set of items
            mBackground.width  = Mathf.RoundToInt(x);
            mBackground.height = Mathf.RoundToInt(-y + bgPadding.y);

            // Set the label width to make alignment work
            for (int i = 0, imax = labels.Count; i < imax; ++i)
            {
                UILabel lbl = labels[i];
                lbl.overflowMethod = UILabel.Overflow.ShrinkContent;
                lbl.width          = lblWidth;
            }

            // Scale the highlight sprite to envelop a single item
            float scaleFactor = 2f * atlas.pixelSize;
            float w           = x - (bgPadding.x + padding.x) * 2f + hlsp.borderLeft * scaleFactor;
            float h           = labelHeight + hlspHeight * scaleFactor;
            mHighlight.width  = Mathf.RoundToInt(w);
            mHighlight.height = Mathf.RoundToInt(h);

            bool placeAbove = (position == Position.Above);

            if (position == Position.Auto)
            {
                UICamera cam = UICamera.FindCameraForLayer(mSelection.layer);

                if (cam != null)
                {
                    Vector3 viewPos = cam.cachedCamera.WorldToViewportPoint(pos);
                    placeAbove = (viewPos.y < 0.5f);
                }
            }

            // If the list should be animated, let's animate it by expanding it
            if (isAnimated)
            {
                AnimateColor(mBackground);

                if (Time.timeScale == 0f || Time.timeScale >= 0.1f)
                {
                    float bottom = y + labelHeight;
                    Animate(mHighlight, placeAbove, bottom);
                    for (int i = 0, imax = labels.Count; i < imax; ++i)
                    {
                        Animate(labels[i], placeAbove, bottom);
                    }
                    AnimateScale(mBackground, placeAbove, bottom);
                }
            }

            // If we need to place the popup list above the item, we need to reposition everything by the size of the list
            if (placeAbove)
            {
                min.y           = max.y - bgPadding.y;
                max.y           = min.y + mBackground.height;
                max.x           = min.x + mBackground.width;
                t.localPosition = new Vector3(min.x, max.y - bgPadding.y, min.z);
            }
            else
            {
                max.y = min.y + bgPadding.y;
                min.y = max.y - mBackground.height;
                max.x = min.x + mBackground.width;
            }

            Transform pt = mPanel.cachedTransform.parent;

            if (pt != null)
            {
                min = mPanel.cachedTransform.TransformPoint(min);
                max = mPanel.cachedTransform.TransformPoint(max);
                min = pt.InverseTransformPoint(min);
                max = pt.InverseTransformPoint(max);
            }

            // Ensure that everything fits into the panel's visible range
            Vector3 offset = mPanel.hasClipping ? Vector3.zero : mPanel.CalculateConstrainOffset(min, max);
            pos             = t.localPosition + offset;
            pos.x           = Mathf.Round(pos.x);
            pos.y           = Mathf.Round(pos.y);
            t.localPosition = pos;
            if (onShow.Count > 0)
            {
                if (EventDelegate.IsValid(onShow))
                {
                    EventDelegate.Execute(onShow);
                }
            }
        }
        else
        {
            OnSelect(false);
        }
    }
예제 #10
0
    /// <summary>
    /// Display the drop-down list when the game object gets clicked on.
    /// </summary>

    void OnClick()
    {
        if (mChild == null && atlas != null && font != null && items.Count > 0)
        {
            mLabelList.Clear();

            // Disable the navigation script
            handleEvents = true;

            // Automatically locate the panel responsible for this object
            if (mPanel == null)
            {
                mPanel = UIPanel.Find(transform, true);
            }

            // Calculate the dimensions of the object triggering the popup list so we can position it below it
            Transform myTrans = transform;
            Bounds    bounds  = NGUIMath.CalculateRelativeWidgetBounds(myTrans.parent, myTrans);

            // Create the root object for the list
            mChild       = new GameObject("Drop-down List");
            mChild.layer = gameObject.layer;

            Transform t = mChild.transform;
            t.parent        = myTrans.parent;
            t.localPosition = bounds.min;
            t.localRotation = Quaternion.identity;
            t.localScale    = Vector3.one;

            // Add a sprite for the background
            mBackground       = NGUITools.AddSprite(mChild, atlas, backgroundSprite);
            mBackground.pivot = UIWidget.Pivot.TopLeft;
            mBackground.depth = NGUITools.CalculateNextDepth(mPanel.gameObject);
            mBackground.color = backgroundColor;

            // We need to know the size of the background sprite for padding purposes
            Vector4 bgPadding = mBackground.border;
            mBgBorder = bgPadding.y;

            mBackground.cachedTransform.localPosition = new Vector3(0f, bgPadding.y, 0f);

            // Add a sprite used for the selection
            mHighlight       = NGUITools.AddSprite(mChild, atlas, highlightSprite);
            mHighlight.pivot = UIWidget.Pivot.TopLeft;
            mHighlight.color = highlightColor;

            UIAtlas.Sprite hlsp = mHighlight.sprite;
            float          hlspHeight = hlsp.inner.yMin - hlsp.outer.yMin;
            float          fontScale = font.size * textScale;
            float          x = 0f, y = -padding.y;
            List <UILabel> labels = new List <UILabel>();

            // Run through all items and create labels for each one
            for (int i = 0, imax = items.Count; i < imax; ++i)
            {
                string s = items[i];

                UILabel lbl = NGUITools.AddWidget <UILabel>(mChild);
                lbl.pivot = UIWidget.Pivot.TopLeft;
                lbl.font  = font;
                lbl.text  = (isLocalized && Localization.instance != null) ? Localization.instance.Get(s) : s;
                lbl.color = textColor;
                lbl.cachedTransform.localPosition = new Vector3(bgPadding.x + padding.x, y, -0.01f);
                lbl.MakePixelPerfect();

                if (textScale != 1f)
                {
                    Vector3 scale = lbl.cachedTransform.localScale;
                    lbl.cachedTransform.localScale = scale * textScale;
                }
                labels.Add(lbl);

                y -= fontScale;
                y -= padding.y;
                x  = Mathf.Max(x, lbl.relativeSize.x * fontScale);

                // Add an event listener
                UIEventListener listener = UIEventListener.Get(lbl.gameObject);
                listener.onHover   = OnItemHover;
                listener.onPress   = OnItemPress;
                listener.parameter = s;

                // Move the selection here if this is the right label
                if (mSelectedItem == s)
                {
                    Highlight(lbl, true);
                }

                // Add this label to the list
                mLabelList.Add(lbl);
            }

            // The triggering widget's width should be the minimum allowed width
            x = Mathf.Max(x, bounds.size.x - (bgPadding.x + padding.x) * 2f);

            Vector3 bcCenter = new Vector3((x * 0.5f) / fontScale, -0.5f, 0f);
            Vector3 bcSize   = new Vector3(x / fontScale, (fontScale + padding.y) / fontScale, 1f);

            // Run through all labels and add colliders
            for (int i = 0, imax = labels.Count; i < imax; ++i)
            {
                UILabel     lbl = labels[i];
                BoxCollider bc  = NGUITools.AddWidgetCollider(lbl.gameObject);
                bcCenter.z = bc.center.z;
                bc.center  = bcCenter;
                bc.size    = bcSize;
            }

            x += (bgPadding.x + padding.x) * 2f;
            y -= bgPadding.y;

            // Scale the background sprite to envelop the entire set of items
            mBackground.cachedTransform.localScale = new Vector3(x, -y + bgPadding.y, 1f);

            // Scale the highlight sprite to envelop a single item
            mHighlight.cachedTransform.localScale = new Vector3(
                x - (bgPadding.x + padding.x) * 2f + (hlsp.inner.xMin - hlsp.outer.xMin) * 2f,
                fontScale + hlspHeight * 2f, 1f);

            bool placeAbove = (position == Position.Above);

            if (position == Position.Auto)
            {
                UICamera cam = UICamera.FindCameraForLayer(gameObject.layer);

                if (cam != null)
                {
                    Vector3 viewPos = cam.cachedCamera.WorldToViewportPoint(myTrans.position);
                    placeAbove = (viewPos.y < 0.5f);
                }
            }

            // If the list should be animated, let's animate it by expanding it
            if (isAnimated)
            {
                float bottom = y + fontScale;
                Animate(mHighlight, placeAbove, bottom);
                for (int i = 0, imax = labels.Count; i < imax; ++i)
                {
                    Animate(labels[i], placeAbove, bottom);
                }
                AnimateColor(mBackground);
                AnimateScale(mBackground, placeAbove, bottom);
            }

            // If we need to place the popup list above the item, we need to reposition everything by the size of the list
            if (placeAbove)
            {
                t.localPosition = new Vector3(bounds.min.x, bounds.max.y - y - bgPadding.y, bounds.min.z);
            }
        }
        else
        {
            OnSelect(false);
        }
    }
예제 #11
0
    /// <summary>
    /// Create a popup list or a menu.
    /// </summary>

    void CreatePopup(GameObject go, bool isDropDown)
    {
        if (NGUISettings.atlas != null)
        {
            NGUIEditorTools.SpriteField("Foreground", "Foreground sprite (shown on the button)", NGUISettings.atlas, mListFG, OnListFG);
            NGUIEditorTools.SpriteField("Background", "Background sprite (envelops the options)", NGUISettings.atlas, mListBG, OnListBG);
            NGUIEditorTools.SpriteField("Highlight", "Sprite used to highlight the selected option", NGUISettings.atlas, mListHL, OnListHL);
        }

        if (ShouldCreate(go, NGUISettings.atlas != null && NGUISettings.font != null))
        {
            int depth = NGUITools.CalculateNextDepth(go);
            go      = NGUITools.AddChild(go);
            go.name = isDropDown ? "Popup List" : "Popup Menu";

            UIAtlas.Sprite sphl = NGUISettings.atlas.GetSprite(mListHL);
            UIAtlas.Sprite spfg = NGUISettings.atlas.GetSprite(mListFG);

            Vector2 hlPadding = new Vector2(
                Mathf.Max(4f, sphl.inner.xMin - sphl.outer.xMin),
                Mathf.Max(4f, sphl.inner.yMin - sphl.outer.yMin));

            Vector2 fgPadding = new Vector2(
                Mathf.Max(4f, spfg.inner.xMin - spfg.outer.xMin),
                Mathf.Max(4f, spfg.inner.yMin - spfg.outer.yMin));

            // Background sprite
            UISprite sprite = NGUITools.AddSprite(go, NGUISettings.atlas, mListFG);
            sprite.depth = depth;
            sprite.atlas = NGUISettings.atlas;
            sprite.pivot = UIWidget.Pivot.Left;
            sprite.transform.localScale    = new Vector3(150f + fgPadding.x * 2f, NGUISettings.font.size + fgPadding.y * 2f, 1f);
            sprite.transform.localPosition = Vector3.zero;
            sprite.MakePixelPerfect();

            // Text label
            UILabel lbl = NGUITools.AddWidget <UILabel>(go);
            lbl.font  = NGUISettings.font;
            lbl.text  = go.name;
            lbl.pivot = UIWidget.Pivot.Left;
            lbl.cachedTransform.localPosition = new Vector3(fgPadding.x, 0f, 0f);
            lbl.MakePixelPerfect();

            // Add a collider
            NGUITools.AddWidgetCollider(go);

            // Add the popup list
            UIPopupList list = go.AddComponent <UIPopupList>();
            list.atlas            = NGUISettings.atlas;
            list.font             = NGUISettings.font;
            list.backgroundSprite = mListBG;
            list.highlightSprite  = mListHL;
            list.padding          = hlPadding;
            if (isDropDown)
            {
                list.textLabel = lbl;
            }
            for (int i = 0; i < 5; ++i)
            {
                list.items.Add(isDropDown ? ("List Option " + i) : ("Menu Option " + i));
            }

            // Add the scripts
            go.AddComponent <UIButton>().tweenTarget = sprite.gameObject;
            go.AddComponent <UIButtonSound>();

            Selection.activeGameObject = go;
        }
    }
예제 #12
0
    /// <summary>
    /// Progress bar creation function.
    /// </summary>

    void CreateSlider(GameObject go, bool slider)
    {
        if (NGUISettings.atlas != null)
        {
            NGUIEditorTools.SpriteField("Empty", "Sprite for the background (empty bar)", NGUISettings.atlas, mSliderBG, OnSliderBG);
            NGUIEditorTools.SpriteField("Full", "Sprite for the foreground (full bar)", NGUISettings.atlas, mSliderFG, OnSliderFG);

            if (slider)
            {
                NGUIEditorTools.SpriteField("Thumb", "Sprite for the thumb indicator", NGUISettings.atlas, mSliderTB, OnSliderTB);
            }
        }

        if (ShouldCreate(go, NGUISettings.atlas != null))
        {
            int depth = NGUITools.CalculateNextDepth(go);
            go      = NGUITools.AddChild(go);
            go.name = slider ? "Slider" : "Progress Bar";

            // Background sprite
            UIAtlas.Sprite bgs  = NGUISettings.atlas.GetSprite(mSliderBG);
            UISprite       back = (bgs.inner == bgs.outer) ?
                                  (UISprite)NGUITools.AddWidget <UISprite>(go) :
                                  (UISprite)NGUITools.AddWidget <UISlicedSprite>(go);

            back.name                 = "Background";
            back.depth                = depth;
            back.pivot                = UIWidget.Pivot.Left;
            back.atlas                = NGUISettings.atlas;
            back.spriteName           = mSliderBG;
            back.transform.localScale = new Vector3(200f, 30f, 1f);
            back.MakePixelPerfect();

            // Fireground sprite
            UIAtlas.Sprite fgs   = NGUISettings.atlas.GetSprite(mSliderFG);
            UISprite       front = (fgs.inner == fgs.outer) ?
                                   (UISprite)NGUITools.AddWidget <UIFilledSprite>(go) :
                                   (UISprite)NGUITools.AddWidget <UISlicedSprite>(go);

            front.name                 = "Foreground";
            front.pivot                = UIWidget.Pivot.Left;
            front.atlas                = NGUISettings.atlas;
            front.spriteName           = mSliderFG;
            front.transform.localScale = new Vector3(200f, 30f, 1f);
            front.MakePixelPerfect();

            // Add a collider
            if (slider)
            {
                NGUITools.AddWidgetCollider(go);
            }

            // Add the slider script
            UISlider uiSlider = go.AddComponent <UISlider>();
            uiSlider.foreground = front.transform;
            uiSlider.fullSize   = front.transform.localScale;

            // Thumb sprite
            if (slider)
            {
                UIAtlas.Sprite tbs = NGUISettings.atlas.GetSprite(mSliderTB);
                UISprite       thb = (tbs.inner == tbs.outer) ?
                                     (UISprite)NGUITools.AddWidget <UISprite>(go) :
                                     (UISprite)NGUITools.AddWidget <UISlicedSprite>(go);

                thb.name       = "Thumb";
                thb.atlas      = NGUISettings.atlas;
                thb.spriteName = mSliderTB;
                thb.transform.localPosition = new Vector3(200f, 0f, 0f);
                thb.transform.localScale    = new Vector3(20f, 40f, 1f);
                thb.MakePixelPerfect();

                NGUITools.AddWidgetCollider(thb.gameObject);
                thb.gameObject.AddComponent <UIButtonColor>();
                thb.gameObject.AddComponent <UIButtonScale>();

                uiSlider.thumb = thb.transform;
            }
            uiSlider.sliderValue = 0.75f;

            // Select the slider
            Selection.activeGameObject = go;
        }
    }