Exemple #1
0
 void OnHover(bool isOver)
 {
     if (isEnabled && target != null && !m_isActive)
     {
         target.spriteName = isOver ? hoverSprite : normalSprite;
         target.MakePixelPerfect();
     }
 }
Exemple #2
0
    /// <summary>
    /// Atlas selection callback.
    /// </summary>

    void OnSelectAtlas(MonoBehaviour obj)
    {
        if (mSprite != null)
        {
            NGUIEditorTools.RegisterUndo("Atlas Selection", mSprite);
            bool resize = (mSprite.atlas == null);
            mSprite.atlas = obj as NGUIAtlas;
            if (resize)
            {
                mSprite.MakePixelPerfect();
            }
            EditorUtility.SetDirty(mSprite.gameObject);
        }
    }
Exemple #3
0
    /// <summary>
    /// Update the value of the scroll bar.
    /// </summary>

    public void ForceUpdate()
    {
        mIsDirty = false;

        if (mBG != null && mFG != null)
        {
            mSize   = Mathf.Clamp01(mSize);
            mScroll = Mathf.Clamp01(mScroll);

            Vector4 bg = mBG.border;
            Vector4 fg = mFG.border;

            // Space available for the background
            Vector2 bgs = new Vector2(
                Mathf.Max(0f, mBG.cachedTransform.localScale.x - bg.x - bg.z),
                Mathf.Max(0f, mBG.cachedTransform.localScale.y - bg.y - bg.w));

            float val = mInverted ? 1f - mScroll : mScroll;

            if (mDir == Direction.Horizontal)
            {
                Vector2 fgs = new Vector2(bgs.x * mSize, bgs.y);

                mFG.pivot = NGUIWidget.Pivot.Left;
                mBG.pivot = NGUIWidget.Pivot.Left;
                mBG.cachedTransform.localPosition = Vector3.zero;
                mFG.cachedTransform.localPosition = new Vector3(bg.x - fg.x + (bgs.x - fgs.x) * val, 0f, 0f);
                mFG.cachedTransform.localScale    = new Vector3(fgs.x + fg.x + fg.z, fgs.y + fg.y + fg.w, 1f);
                if (val < 0.999f && val > 0.001f)
                {
                    mFG.MakePixelPerfect();
                }
            }
            else
            {
                Vector2 fgs = new Vector2(bgs.x, bgs.y * mSize);

                mFG.pivot = NGUIWidget.Pivot.Top;
                mBG.pivot = NGUIWidget.Pivot.Top;
                mBG.cachedTransform.localPosition = Vector3.zero;
                mFG.cachedTransform.localPosition = new Vector3(0f, -bg.y + fg.y - (bgs.y - fgs.y) * val, 0f);
                mFG.cachedTransform.localScale    = new Vector3(fgs.x + fg.x + fg.z, fgs.y + fg.y + fg.w, 1f);
                if (val < 0.999f && val > 0.001f)
                {
                    mFG.MakePixelPerfect();
                }
            }
        }
    }
    /// <summary>
    /// Sprite creation function.
    /// </summary>

    void CreateSprite(GameObject go, string field)
    {
        if (NGUISettings.atlas != null)
        {
            NGUIEditorTools.SpriteField("Sprite", "Sprite that will be created", NGUISettings.atlas, field, OnSprite);

            if (!string.IsNullOrEmpty(field))
            {
                GUILayout.BeginHorizontal();
                NGUISettings.pivot = (NGUIWidget.Pivot)EditorGUILayout.EnumPopup("Pivot", NGUISettings.pivot, GUILayout.Width(200f));
                GUILayout.Space(20f);
                GUILayout.Label("Initial pivot point used by the sprite");
                GUILayout.EndHorizontal();
            }
        }

        if (ShouldCreate(go, NGUISettings.atlas != null))
        {
            NGUISprite sprite = NGUITools.AddWidget <NGUISprite>(go);
            sprite.name       = sprite.name + " (" + field + ")";
            sprite.atlas      = NGUISettings.atlas;
            sprite.spriteName = field;
            sprite.pivot      = NGUISettings.pivot;
            sprite.MakePixelPerfect();
            Selection.activeGameObject = sprite.gameObject;
        }
    }
    /// <summary>
    /// Scroll bar template.
    /// </summary>

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

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

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

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

            NGUISprite bg = NGUITools.AddWidget <NGUISprite>(go);
            bg.type                 = NGUISprite.Type.Sliced;
            bg.name                 = "Background";
            bg.depth                = depth;
            bg.atlas                = NGUISettings.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();

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

            NGUIScrollBar sb = go.AddComponent <NGUIScrollBar>();
            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;
        }
    }
Exemple #6
0
 void OnNormal(string spriteName)
 {
     NGUIEditorTools.RegisterUndo("Image Button Change", mButton, mButton.gameObject, mSprite);
     mButton.normalSprite = spriteName;
     mSprite.spriteName   = spriteName;
     mSprite.MakePixelPerfect();
     if (mButton.collider == null || (mButton.collider is BoxCollider))
     {
         NGUITools.AddWidgetCollider(mButton.gameObject);
     }
     Repaint();
 }
    /// <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>
    /// Advance the sprite animation process.
    /// </summary>

    void Update()
    {
        if (mActive && mSpriteNames.Count > 1 && Application.isPlaying && mFPS > 0f)
        {
            mDelta += Time.deltaTime;
            float rate = 1f / mFPS;

            if (rate < mDelta)
            {
                mDelta = (rate > 0f) ? mDelta - rate : 0f;
                if (++mIndex >= mSpriteNames.Count)
                {
                    mIndex  = 0;
                    mActive = loop;
                }

                if (mActive)
                {
                    mSprite.spriteName = mSpriteNames[mIndex];
                    mSprite.MakePixelPerfect();
                }
            }
        }
    }
    /// <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;
        }
    }
Exemple #12
0
    static public void AddSprite()
    {
        GameObject go = NGUIEditorTools.SelectedRoot(true);

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

            NGUISprite sprite = NGUITools.AddWidget <NGUISprite>(go);
            sprite.name  = "Sprite";
            sprite.atlas = NGUISettings.atlas;

            if (sprite.atlas != null)
            {
                string           sn = EditorPrefs.GetString("NGUI Sprite", "");
                NGUIAtlas.Sprite sp = sprite.atlas.GetSprite(sn);

                if (sp != null)
                {
                    sprite.spriteName = sn;
                    if (sp.inner != sp.outer)
                    {
                        sprite.type = NGUISprite.Type.Sliced;
                    }
                }
            }
            sprite.pivot = NGUISettings.pivot;
            sprite.cachedTransform.localScale = new Vector3(100f, 100f, 1f);
            sprite.MakePixelPerfect();
            Selection.activeGameObject = sprite.gameObject;
        }
        else
        {
            Debug.Log("You must select a game object first.");
        }
    }
Exemple #13
0
    /// <summary>
    /// Force-localize the widget.
    /// </summary>

    public void Localize()
    {
        Localization loc = Localization.instance;
        NGUIWidget   w   = GetComponent <NGUIWidget>();
        NGUILabel    lbl = w as NGUILabel;
        NGUISprite   sp  = w as NGUISprite;

        // If no localization key has been specified, use the label's text as the key
        if (string.IsNullOrEmpty(mLanguage) && string.IsNullOrEmpty(key) && lbl != null)
        {
            key = lbl.text;
        }

        // If we still don't have a key, leave the value as blank
        string val = string.IsNullOrEmpty(key) ? "" : loc.Get(key);

        if (lbl != null)
        {
            // If this is a label used by input, we should localize its default value instead
            NGUIInput input = NGUITools.FindInParents <NGUIInput>(lbl.gameObject);
            if (input != null && input.label == lbl)
            {
                input.defaultText = val;
            }
            else
            {
                lbl.text = val;
            }
        }
        else if (sp != null)
        {
            sp.spriteName = val;
            sp.MakePixelPerfect();
        }
        mLanguage = loc.currentLanguage;
    }
    /// <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;
        }
    }
    /// <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
            NGUIAtlas.Sprite bgs  = NGUISettings.atlas.GetSprite(mSliderBG);
            NGUISprite       back = (NGUISprite)NGUITools.AddWidget <NGUISprite>(go);

            back.type                    = (bgs.inner == bgs.outer) ? NGUISprite.Type.Simple : NGUISprite.Type.Sliced;
            back.name                    = "Background";
            back.depth                   = depth;
            back.pivot                   = NGUIWidget.Pivot.Left;
            back.atlas                   = NGUISettings.atlas;
            back.spriteName              = mSliderBG;
            back.transform.localScale    = new Vector3(200f, 30f, 1f);
            back.transform.localPosition = Vector3.zero;
            back.MakePixelPerfect();

            // Foreground sprite
            NGUIAtlas.Sprite fgs   = NGUISettings.atlas.GetSprite(mSliderFG);
            NGUISprite       front = NGUITools.AddWidget <NGUISprite>(go);
            front.type                    = (fgs.inner == fgs.outer) ? NGUISprite.Type.Filled : NGUISprite.Type.Sliced;
            front.name                    = "Foreground";
            front.pivot                   = NGUIWidget.Pivot.Left;
            front.atlas                   = NGUISettings.atlas;
            front.spriteName              = mSliderFG;
            front.transform.localScale    = new Vector3(200f, 30f, 1f);
            front.transform.localPosition = Vector3.zero;
            front.MakePixelPerfect();

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

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

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

                thb.type       = (tbs.inner == tbs.outer) ? NGUISprite.Type.Simple : NGUISprite.Type.Sliced;
                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 <NGUIButtonColor>();
                thb.gameObject.AddComponent <NGUIButtonScale>();

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

            // Select the slider
            Selection.activeGameObject = go;
        }
    }
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

    void OnGUI()
    {
        EditorGUIUtility.LookLikeControls(80f);

        if (mAtlas == null)
        {
            GUILayout.Label("No Atlas selected.", "LODLevelNotifyText");
        }
        else
        {
            bool close = false;
            GUILayout.Label(mAtlas.name + " Sprites", "LODLevelNotifyText");
            NGUIEditorTools.DrawSeparator();

            GUILayout.BeginHorizontal();
            GUILayout.Space(84f);

            string before = NGUISettings.partialSprite;
            string after  = EditorGUILayout.TextField("", before, "SearchTextField");
            NGUISettings.partialSprite = after;

            if (GUILayout.Button("", "SearchCancelButton", GUILayout.Width(18f)))
            {
                NGUISettings.partialSprite = "";
                GUIUtility.keyboardControl = 0;
            }
            GUILayout.Space(84f);
            GUILayout.EndHorizontal();

            Texture2D tex = mAtlas.texture as Texture2D;

            if (tex == null)
            {
                GUILayout.Label("The atlas doesn't have a texture to work with");
                return;
            }

            BetterList <string> sprites = mAtlas.GetListOfSprites(NGUISettings.partialSprite);

            float size    = 80f;
            float padded  = size + 10f;
            int   columns = Mathf.FloorToInt(Screen.width / padded);
            if (columns < 1)
            {
                columns = 1;
            }

            int  offset = 0;
            Rect rect   = new Rect(10f, 0, size, size);

            GUILayout.Space(10f);
            mPos = GUILayout.BeginScrollView(mPos);

            while (offset < sprites.size)
            {
                GUILayout.BeginHorizontal();
                {
                    int col = 0;
                    rect.x = 10f;

                    for (; offset < sprites.size; ++offset)
                    {
                        NGUIAtlas.Sprite sprite = mAtlas.GetSprite(sprites[offset]);
                        if (sprite == null)
                        {
                            continue;
                        }

                        // Button comes first
                        if (GUI.Button(rect, ""))
                        {
                            float delta = Time.realtimeSinceStartup - mClickTime;
                            mClickTime = Time.realtimeSinceStartup;

                            if (spriteName != sprite.name)
                            {
                                if (mSprite != null)
                                {
                                    NGUIEditorTools.RegisterUndo("Atlas Selection", mSprite);
                                    mSprite.spriteName = sprite.name;
                                    mSprite.MakePixelPerfect();
                                    EditorUtility.SetDirty(mSprite.gameObject);
                                }
                                else if (mCallback != null)
                                {
                                    mName = sprite.name;
                                    mCallback(sprite.name);
                                }
                            }
                            else if (delta < 0.5f)
                            {
                                close = true;
                            }
                        }

                        if (Event.current.type == EventType.Repaint)
                        {
                            // On top of the button we have a checkboard grid
                            NGUIEditorTools.DrawTiledTexture(rect, NGUIEditorTools.backdropTexture);

                            Rect uv = sprite.outer;
                            if (mAtlas.coordinates == NGUIAtlas.Coordinates.Pixels)
                            {
                                uv = NGUIMath.ConvertToTexCoords(uv, tex.width, tex.height);
                            }

                            // Calculate the texture's scale that's needed to display the sprite in the clipped area
                            float scaleX = rect.width / uv.width;
                            float scaleY = rect.height / uv.height;

                            // Stretch the sprite so that it will appear proper
                            float aspect   = (scaleY / scaleX) / ((float)tex.height / tex.width);
                            Rect  clipRect = rect;

                            if (aspect != 1f)
                            {
                                if (aspect < 1f)
                                {
                                    // The sprite is taller than it is wider
                                    float padding = size * (1f - aspect) * 0.5f;
                                    clipRect.xMin += padding;
                                    clipRect.xMax -= padding;
                                }
                                else
                                {
                                    // The sprite is wider than it is taller
                                    float padding = size * (1f - 1f / aspect) * 0.5f;
                                    clipRect.yMin += padding;
                                    clipRect.yMax -= padding;
                                }
                            }

                            GUI.DrawTextureWithTexCoords(clipRect, tex, uv);

                            // Draw the selection
                            if (spriteName == sprite.name)
                            {
                                NGUIEditorTools.DrawOutline(rect, new Color(0.4f, 1f, 0f, 1f));
                            }
                        }

                        if (++col >= columns)
                        {
                            ++offset;
                            break;
                        }
                        rect.x += padded;
                    }
                }
                GUILayout.EndHorizontal();
                GUILayout.Space(padded);
                rect.y += padded;
            }
            GUILayout.EndScrollView();
            if (close)
            {
                Close();
            }
        }
    }
Exemple #17
0
    public void createSymblos()
    {
        if (Application.isPlaying)
        {
            if (mSymblosVerts.Count <= 0)
            {
                return;
            }
            if (label == null)
            {
                return;
            }
            if (label.font.dynamicSymbolsFont == null)
            {
                return;
            }

            //float fontSize = label.font.size;
            float fontSize    = label.transform.localScale.x;
            float relateScale = label.transform.localScale.x / label.font.size;

            float lineFillLengthPixel = label.LineWidthBase * fontSize;
            float xLeftSide           = 0;
            float yTopSide            = 0;  //note:this is not "Top". center Y of Top Line...
            //count the left of label's Fill rect x.
            if (label.pivot == NGUIWidget.Pivot.Left || label.pivot == NGUIWidget.Pivot.TopLeft || label.pivot == NGUIWidget.Pivot.BottomLeft)
            {
                xLeftSide = label.transform.localPosition.x;
            }
            else if (label.pivot == NGUIWidget.Pivot.Right || label.pivot == NGUIWidget.Pivot.TopRight || label.pivot == NGUIWidget.Pivot.BottomRight)
            {
                //chiuan fixe When label has maxLength,and pivot == TopRight...
                float tLength = label.lineWidth > 0 ? label.lineWidth : lineFillLengthPixel;
                xLeftSide = label.transform.localPosition.x - tLength;
                //Debug.Log(label.transform.localPosition.x + " - " + tLength + " = " + xLeftSide);
            }
            else
            {
                float tLength = label.lineWidth > 0 ? label.lineWidth : lineFillLengthPixel;
                xLeftSide = label.transform.localPosition.x - tLength / 2f;
            }
            //start count yTopSide
            if (label.pivot == NGUIWidget.Pivot.TopLeft || label.pivot == NGUIWidget.Pivot.TopRight || label.pivot == NGUIWidget.Pivot.Top)
            {
                yTopSide = label.transform.localPosition.y - fontSize * 0.5f;
            }
            else if (label.pivot == NGUIWidget.Pivot.BottomLeft || label.pivot == NGUIWidget.Pivot.BottomRight || label.pivot == NGUIWidget.Pivot.Bottom)
            {
                yTopSide = label.transform.localPosition.y + label.relativeSize.y * fontSize - fontSize * 0.5f;
            }
            else
            {
                yTopSide = label.transform.localPosition.y + label.relativeSize.y * 0.5f * fontSize - fontSize * 0.5f;
            }

            //Debug.Log("xLeftSide = " + xLeftSide);
            //Debug.Log("yTopSide = " + yTopSide);
            //Debug.LogError("lfp: " + lineFillLengthPixel);
            //Debug.LogError("label x: " + label.transform.localPosition.x);

            float lineSeed = 0;
            if (label.lineWidth > 0 && label.pivot != NGUIWidget.Pivot.Right && label.pivot != NGUIWidget.Pivot.TopRight && label.pivot != NGUIWidget.Pivot.BottomRight)
            {
                lineSeed = label.lineWidth * 1.0f / fontSize;
                //Debug.LogWarning("line seed = " + lineSeed);
            }

            for (int i = 0; i < mSymblosVerts.Count; i++)
            {
                GameObject go = new GameObject("emoji" + i);
                go.transform.parent = label.transform.parent;
                go.layer            = label.gameObject.layer;

                NGUISprite emoji = NGUITools.AddSprite(go, label.font.dynamicSymbolsFont.atlas, mSymblosNames[i]);
                //Debug.LogError("Adding emoji sprite @" + XUIActiveBase.logObjName(go) + " label text: " + label.text);
                go.transform.localScale = Vector3.one;                 //why is new parent?
                emoji.pivot             = NGUIWidget.Pivot.Left;
                emoji.MakePixelPerfect();
                emoji.transform.localPosition = Vector3.zero;                 //reset to zero of parent.
                float y = emoji.transform.localScale.y / emoji.transform.localScale.x;
                emoji.transform.localScale = new Vector3(fontSize, fontSize * y, 1f);
                //
                go.transform.localPosition = new Vector3(
                    xLeftSide + fontSize * mSymblosVerts[i].x,
                    yTopSide + (fontSize * mSymblosVerts[i].y + offsetOfY - 6.104168f - 2f),
                    label.transform.localPosition.z - 1f);
                sprites.Add(go);
            }
        }
    }