/// <summary>
    /// Font selection callback.
    /// </summary>

    void OnSelectFont(MonoBehaviour obj)
    {
        if (mLabel != null)
        {
            NGUIEditorTools.RegisterUndo("Font Selection", mLabel);
            bool resize = (mLabel.font == null);
            mLabel.font = obj as NGUIFont;
            if (resize)
            {
                mLabel.MakePixelPerfect();
            }
        }
    }
    /// <summary>
    /// Checkbox creation function.
    /// </summary>

    void CreateCheckbox(GameObject go)
    {
        if (NGUISettings.atlas != null)
        {
            NGUIEditorTools.SpriteField("Background", "Sprite used for the background", NGUISettings.atlas, mCheckBG, OnCheckBG);
            NGUIEditorTools.SpriteField("Checkmark", "Sprite used for the checkmark", NGUISettings.atlas, mCheck, OnCheck);
        }

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

            NGUISprite bg = NGUITools.AddWidget <NGUISprite>(go);
            bg.type                 = NGUISprite.Type.Sliced;
            bg.name                 = "Background";
            bg.depth                = depth;
            bg.atlas                = NGUISettings.atlas;
            bg.spriteName           = mCheckBG;
            bg.transform.localScale = new Vector3(26f, 26f, 1f);
            bg.MakePixelPerfect();

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

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

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

            // Add the scripts
            go.AddComponent <NGUICheckbox>().checkSprite    = fg;
            go.AddComponent <NGUIButton>().tweenTarget      = bg.gameObject;
            go.AddComponent <NGUIButtonScale>().tweenTarget = bg.transform;
            go.AddComponent <NGUIButtonSound>();

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

    void CreateImageButton(GameObject go)
    {
        if (NGUISettings.atlas != null)
        {
            NGUIEditorTools.SpriteField("Normal", "Normal state sprite", NGUISettings.atlas, mImage0, OnImage0);
            NGUIEditorTools.SpriteField("Hover", "Hover state sprite", NGUISettings.atlas, mImage1, OnImage1);
            NGUIEditorTools.SpriteField("Pressed", "Pressed state sprite", NGUISettings.atlas, mImage2, OnImage2);
            NGUIEditorTools.SpriteField("Disabled", "Disabled state sprite", NGUISettings.atlas, mImage3, OnImage3);
        }

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

            NGUIAtlas.Sprite sp     = NGUISettings.atlas.GetSprite(mImage0);
            NGUISprite       sprite = NGUITools.AddWidget <NGUISprite>(go);
            sprite.type                 = (sp.inner == sp.outer) ? NGUISprite.Type.Simple : NGUISprite.Type.Sliced;
            sprite.name                 = "Background";
            sprite.depth                = depth;
            sprite.atlas                = NGUISettings.atlas;
            sprite.spriteName           = mImage0;
            sprite.transform.localScale = new Vector3(150f, 40f, 1f);
            sprite.MakePixelPerfect();

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

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

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

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

    void CreateInput(GameObject go)
    {
        if (NGUISettings.atlas != null)
        {
            NGUIEditorTools.SpriteField("Background", "Sliced Sprite for the background", NGUISettings.atlas, mInputBG, OnInputBG);
        }

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

            float padding = 3f;

            NGUISprite bg = NGUITools.AddWidget <NGUISprite>(go);
            bg.type                    = NGUISprite.Type.Sliced;
            bg.name                    = "Background";
            bg.depth                   = depth;
            bg.atlas                   = NGUISettings.atlas;
            bg.spriteName              = mInputBG;
            bg.pivot                   = NGUIWidget.Pivot.Left;
            bg.transform.localScale    = new Vector3(400f, NGUISettings.font.size + padding * 2f, 1f);
            bg.transform.localPosition = Vector3.zero;
            bg.MakePixelPerfect();

            NGUILabel lbl = NGUITools.AddWidget <NGUILabel>(go);
            lbl.font  = NGUISettings.font;
            lbl.pivot = NGUIWidget.Pivot.Left;
            lbl.transform.localPosition = new Vector3(padding, 0f, 0f);
            lbl.multiLine       = false;
            lbl.supportEncoding = false;
            lbl.lineWidth       = Mathf.RoundToInt(400f - padding * 2f);
            lbl.text            = "You can type here";
            lbl.MakePixelPerfect();

            // Add a collider to the background
            NGUITools.AddWidgetCollider(go);

            // Add an input script to the background and have it point to the label
            NGUIInput input = go.AddComponent <NGUIInput>();
            input.label = lbl;

            // Update the selection
            Selection.activeGameObject = go;
        }
    }
    /// <summary>
    /// Button creation function.
    /// </summary>

    void CreateButton(GameObject go)
    {
        if (NGUISettings.atlas != null)
        {
            NGUIEditorTools.SpriteField("Background", "Sliced Sprite for the background", NGUISettings.atlas, mButton, OnButton);
        }

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

            NGUISprite bg = NGUITools.AddWidget <NGUISprite>(go);
            bg.type                 = NGUISprite.Type.Sliced;
            bg.name                 = "Background";
            bg.depth                = depth;
            bg.atlas                = NGUISettings.atlas;
            bg.spriteName           = mButton;
            bg.transform.localScale = new Vector3(150f, 40f, 1f);
            bg.MakePixelPerfect();

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

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

            // Add the scripts
            go.AddComponent <NGUIButton>().tweenTarget = bg.gameObject;
            go.AddComponent <NGUIButtonScale>();
            go.AddComponent <NGUIButtonOffset>();
            go.AddComponent <NGUIButtonSound>();

            Selection.activeGameObject = go;
        }
    }
Esempio n. 6
0
    static public void AddLabel()
    {
        GameObject go = NGUIEditorTools.SelectedRoot(true);

        if (go != null)
        {
            Undo.RegisterSceneUndo("Add a Label");

            NGUILabel lbl = NGUITools.AddWidget <NGUILabel>(go);
            lbl.name  = "Label";
            lbl.font  = NGUISettings.font;
            lbl.text  = "New Label";
            lbl.pivot = NGUISettings.pivot;
            lbl.cachedTransform.localScale = new Vector3(100f, 100f, 1f);
            lbl.MakePixelPerfect();
            Selection.activeGameObject = lbl.gameObject;
        }
        else
        {
            Debug.Log("You must select a game object first.");
        }
    }
    /// <summary>
    /// Label creation function.
    /// </summary>

    void CreateLabel(GameObject go)
    {
        GUILayout.BeginHorizontal();
        Color c = EditorGUILayout.ColorField("Color", mColor, GUILayout.Width(220f));

        GUILayout.Label("Color tint the label will start with");
        GUILayout.EndHorizontal();

        if (mColor != c)
        {
            mColor = c;
            Save();
        }

        if (ShouldCreate(go, NGUISettings.font != null))
        {
            NGUILabel lbl = NGUITools.AddWidget <NGUILabel>(go);
            lbl.font  = NGUISettings.font;
            lbl.text  = "New Label";
            lbl.color = mColor;
            lbl.MakePixelPerfect();
            Selection.activeGameObject = lbl.gameObject;
        }
    }
    /// <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";

            NGUIAtlas.Sprite sphl = NGUISettings.atlas.GetSprite(mListHL);
            NGUIAtlas.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
            NGUISprite sprite = NGUITools.AddSprite(go, NGUISettings.atlas, mListFG);
            sprite.depth = depth;
            sprite.atlas = NGUISettings.atlas;
            sprite.pivot = NGUIWidget.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
            NGUILabel lbl = NGUITools.AddWidget <NGUILabel>(go);
            lbl.font  = NGUISettings.font;
            lbl.text  = go.name;
            lbl.pivot = NGUIWidget.Pivot.Left;
            lbl.cachedTransform.localPosition = new Vector3(fgPadding.x, 0f, 0f);
            lbl.MakePixelPerfect();

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

            // Add the popup list
            NGUIPopupList list = go.AddComponent <NGUIPopupList>();
            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 <NGUIButton>().tweenTarget = sprite.gameObject;
            go.AddComponent <NGUIButtonSound>();

            Selection.activeGameObject = go;
        }
    }
Esempio n. 9
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 = NGUIPanel.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 = NGUIWidget.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 = NGUIWidget.Pivot.TopLeft;
            mHighlight.color = highlightColor;

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

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

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

                NGUILabel lbl = NGUITools.AddWidget <NGUILabel>(mChild);
                lbl.pivot = NGUIWidget.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
                NGUIEventListener listener = NGUIEventListener.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)
            {
                NGUILabel   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)
            {
                NGUICamera cam = NGUICamera.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);
        }
    }