コード例 #1
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;
        }
    }
コード例 #2
0
    public override void OnInspectorGUI()
    {
        mFont = target as UIFont;
        NGUIEditorTools.SetLabelWidth(80f);

        GUILayout.Space(6f);

        if (mFont.replacement != null)
        {
            mType        = FontType.Reference;
            mReplacement = mFont.replacement;
        }
        else if (mFont.dynamicFont != null)
        {
            mType = FontType.Dynamic;
        }

        GUI.changed = false;
        GUILayout.BeginHorizontal();
        mType = (FontType)EditorGUILayout.EnumPopup("Font Type", mType);
        NGUIEditorTools.DrawPadding();
        GUILayout.EndHorizontal();

        if (GUI.changed)
        {
            if (mType == FontType.Bitmap)
            {
                OnSelectFont(null);
            }

            if (mType != FontType.Dynamic && mFont.dynamicFont != null)
            {
                mFont.dynamicFont = null;
            }
        }

        if (mType == FontType.Reference)
        {
            ComponentSelector.Draw <UIFont>(mFont.replacement, OnSelectFont, true);

            GUILayout.Space(6f);
            EditorGUILayout.HelpBox("You can have one font simply point to " +
                                    "another one. This is useful if you want to be " +
                                    "able to quickly replace the contents of one " +
                                    "font with another one, for example for " +
                                    "swapping an SD font with an HD one, or " +
                                    "replacing an English font with a Chinese " +
                                    "one. All the labels referencing this font " +
                                    "will update their references to the new one.", MessageType.Info);

            if (mReplacement != mFont && mFont.replacement != mReplacement)
            {
                NGUIEditorTools.RegisterUndo("Font Change", mFont);
                mFont.replacement = mReplacement;
                NGUITools.SetDirty(mFont);
            }
            return;
        }
        else if (mType == FontType.Dynamic)
        {
#if UNITY_3_5
            EditorGUILayout.HelpBox("Dynamic fonts require Unity 4.0 or higher.", MessageType.Error);
#else
            Font fnt = EditorGUILayout.ObjectField("TTF Font", mFont.dynamicFont, typeof(Font), false) as Font;

            if (fnt != mFont.dynamicFont)
            {
                NGUIEditorTools.RegisterUndo("Font change", mFont);
                mFont.dynamicFont = fnt;
            }

            Material mat = EditorGUILayout.ObjectField("Material", mFont.material, typeof(Material), false) as Material;

            if (mFont.material != mat)
            {
                NGUIEditorTools.RegisterUndo("Font Material", mFont);
                mFont.material = mat;
            }

            GUILayout.BeginHorizontal();
            int       size  = EditorGUILayout.IntField("Default Size", mFont.defaultSize, GUILayout.Width(120f));
            FontStyle style = (FontStyle)EditorGUILayout.EnumPopup(mFont.dynamicFontStyle);
            NGUIEditorTools.DrawPadding();
            GUILayout.EndHorizontal();

            if (size != mFont.defaultSize)
            {
                NGUIEditorTools.RegisterUndo("Font change", mFont);
                mFont.defaultSize = size;
            }

            if (style != mFont.dynamicFontStyle)
            {
                NGUIEditorTools.RegisterUndo("Font change", mFont);
                mFont.dynamicFontStyle = style;
            }
#endif
        }
        else
        {
            ComponentSelector.Draw <UIAtlas>(mFont.atlas, OnSelectAtlas, true);

            if (mFont.atlas != null)
            {
                if (mFont.bmFont.isValid)
                {
                    NGUIEditorTools.DrawAdvancedSpriteField(mFont.atlas, mFont.spriteName, SelectSprite, false);
                }
                EditorGUILayout.Space();
            }
            else
            {
                // No atlas specified -- set the material and texture rectangle directly
                Material mat = EditorGUILayout.ObjectField("Material", mFont.material, typeof(Material), false) as Material;

                if (mFont.material != mat)
                {
                    NGUIEditorTools.RegisterUndo("Font Material", mFont);
                    mFont.material = mat;
                }
            }

            // For updating the font's data when importing from an external source, such as the texture packer
            bool resetWidthHeight = false;

            if (mFont.atlas != null || mFont.material != null)
            {
                TextAsset data = EditorGUILayout.ObjectField("Import Data", null, typeof(TextAsset), false) as TextAsset;

                if (data != null)
                {
                    NGUIEditorTools.RegisterUndo("Import Font Data", mFont);
                    BMFontReader.Load(mFont.bmFont, NGUITools.GetHierarchy(mFont.gameObject), data.bytes);
                    mFont.MarkAsChanged();
                    resetWidthHeight = true;
                    Debug.Log("Imported " + mFont.bmFont.glyphCount + " characters");
                }
            }

            if (mFont.bmFont.isValid)
            {
                Texture2D tex = mFont.texture;

                if (tex != null && mFont.atlas == null)
                {
                    // Pixels are easier to work with than UVs
                    Rect pixels = NGUIMath.ConvertToPixels(mFont.uvRect, tex.width, tex.height, false);

                    // Automatically set the width and height of the rectangle to be the original font texture's dimensions
                    if (resetWidthHeight)
                    {
                        pixels.width  = mFont.texWidth;
                        pixels.height = mFont.texHeight;
                    }

                    // Font sprite rectangle
                    pixels = EditorGUILayout.RectField("Pixel Rect", pixels);

                    // Convert the pixel coordinates back to UV coordinates
                    Rect uvRect = NGUIMath.ConvertToTexCoords(pixels, tex.width, tex.height);

                    if (mFont.uvRect != uvRect)
                    {
                        NGUIEditorTools.RegisterUndo("Font Pixel Rect", mFont);
                        mFont.uvRect = uvRect;
                    }
                    //NGUIEditorTools.DrawSeparator();
                    EditorGUILayout.Space();
                }
            }
        }

        // Dynamic fonts don't support emoticons
        if (!mFont.isDynamic && mFont.bmFont.isValid)
        {
            if (mFont.atlas != null)
            {
                if (NGUIEditorTools.DrawHeader("Symbols and Emoticons"))
                {
                    NGUIEditorTools.BeginContents();

                    List <BMSymbol> symbols = mFont.symbols;

                    for (int i = 0; i < symbols.Count;)
                    {
                        BMSymbol sym = symbols[i];

                        GUILayout.BeginHorizontal();
                        GUILayout.Label(sym.sequence, GUILayout.Width(40f));
                        if (NGUIEditorTools.DrawSpriteField(mFont.atlas, sym.spriteName, ChangeSymbolSprite, GUILayout.MinWidth(100f)))
                        {
                            mSelectedSymbol = sym;
                        }

                        if (GUILayout.Button("Edit", GUILayout.Width(40f)))
                        {
                            if (mFont.atlas != null)
                            {
                                NGUISettings.atlas          = mFont.atlas;
                                NGUISettings.selectedSprite = sym.spriteName;
                                NGUIEditorTools.Select(mFont.atlas.gameObject);
                            }
                        }

                        GUI.backgroundColor = Color.red;

                        if (GUILayout.Button("X", GUILayout.Width(22f)))
                        {
                            NGUIEditorTools.RegisterUndo("Remove symbol", mFont);
                            mSymbolSequence = sym.sequence;
                            mSymbolSprite   = sym.spriteName;
                            symbols.Remove(sym);
                            mFont.MarkAsChanged();
                        }
                        GUI.backgroundColor = Color.white;
                        GUILayout.EndHorizontal();
                        GUILayout.Space(4f);
                        ++i;
                    }

                    if (symbols.Count > 0)
                    {
                        GUILayout.Space(6f);
                    }

                    GUILayout.BeginHorizontal();
                    mSymbolSequence = EditorGUILayout.TextField(mSymbolSequence, GUILayout.Width(40f));
                    NGUIEditorTools.DrawSpriteField(mFont.atlas, mSymbolSprite, SelectSymbolSprite);

                    bool isValid = !string.IsNullOrEmpty(mSymbolSequence) && !string.IsNullOrEmpty(mSymbolSprite);
                    GUI.backgroundColor = isValid ? Color.green : Color.grey;

                    if (GUILayout.Button("Add", GUILayout.Width(40f)) && isValid)
                    {
                        NGUIEditorTools.RegisterUndo("Add symbol", mFont);
                        mFont.AddSymbol(mSymbolSequence, mSymbolSprite);
                        mFont.MarkAsChanged();
                        mSymbolSequence = "";
                        mSymbolSprite   = "";
                    }
                    GUI.backgroundColor = Color.white;
                    GUILayout.EndHorizontal();

                    if (symbols.Count == 0)
                    {
                        EditorGUILayout.HelpBox("Want to add an emoticon to your font? In the field above type ':)', choose a sprite, then hit the Add button.", MessageType.Info);
                    }
                    else
                    {
                        GUILayout.Space(4f);
                    }

                    NGUIEditorTools.EndContents();
                }
            }
        }

        if (mFont.bmFont != null && mFont.bmFont.isValid)
        {
            if (NGUIEditorTools.DrawHeader("Modify"))
            {
                NGUIEditorTools.BeginContents();

                UISpriteData sd = mFont.sprite;

                bool disable = (sd != null && (sd.paddingLeft != 0 || sd.paddingBottom != 0));
                EditorGUI.BeginDisabledGroup(disable || mFont.packedFontShader);

                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(20f);
                EditorGUILayout.BeginVertical();

                GUILayout.BeginHorizontal();
                GUILayout.BeginVertical();
                NGUISettings.foregroundColor = EditorGUILayout.ColorField("Foreground", NGUISettings.foregroundColor);
                NGUISettings.backgroundColor = EditorGUILayout.ColorField("Background", NGUISettings.backgroundColor);
                GUILayout.EndVertical();
                mCurve = EditorGUILayout.CurveField("", mCurve, GUILayout.Width(40f), GUILayout.Height(40f));
                GUILayout.EndHorizontal();

                if (GUILayout.Button("Add a Shadow"))
                {
                    ApplyEffect(Effect.Shadow, NGUISettings.foregroundColor, NGUISettings.backgroundColor);
                }
                if (GUILayout.Button("Add a Soft Outline"))
                {
                    ApplyEffect(Effect.Outline, NGUISettings.foregroundColor, NGUISettings.backgroundColor);
                }
                if (GUILayout.Button("Rebalance Colors"))
                {
                    ApplyEffect(Effect.Rebalance, NGUISettings.foregroundColor, NGUISettings.backgroundColor);
                }
                if (GUILayout.Button("Apply Curve to Alpha"))
                {
                    ApplyEffect(Effect.AlphaCurve, NGUISettings.foregroundColor, NGUISettings.backgroundColor);
                }
                if (GUILayout.Button("Apply Curve to Foreground"))
                {
                    ApplyEffect(Effect.ForegroundCurve, NGUISettings.foregroundColor, NGUISettings.backgroundColor);
                }
                if (GUILayout.Button("Apply Curve to Background"))
                {
                    ApplyEffect(Effect.BackgroundCurve, NGUISettings.foregroundColor, NGUISettings.backgroundColor);
                }

                GUILayout.Space(10f);
                if (GUILayout.Button("Add Transparent Border (+1)"))
                {
                    ApplyEffect(Effect.Border, NGUISettings.foregroundColor, NGUISettings.backgroundColor);
                }
                if (GUILayout.Button("Remove Border (-1)"))
                {
                    ApplyEffect(Effect.Crop, NGUISettings.foregroundColor, NGUISettings.backgroundColor);
                }

                EditorGUILayout.EndVertical();
                GUILayout.Space(20f);
                EditorGUILayout.EndHorizontal();

                EditorGUI.EndDisabledGroup();

                if (disable)
                {
                    GUILayout.Space(3f);
                    EditorGUILayout.HelpBox("The sprite used by this font has been trimmed and is not suitable for modification. " +
                                            "Try re-adding this sprite with 'Trim Alpha' disabled.", MessageType.Warning);
                }

                NGUIEditorTools.EndContents();
            }
        }

        // The font must be valid at this point for the rest of the options to show up
        if (mFont.isDynamic || mFont.bmFont.isValid)
        {
            if (mFont.atlas == null)
            {
                mView      = View.Font;
                mUseShader = false;
            }
        }

        // Preview option
        if (!mFont.isDynamic && mFont.atlas != null)
        {
            GUILayout.BeginHorizontal();
            {
                mView = (View)EditorGUILayout.EnumPopup("Preview", mView);
                GUILayout.Label("Shader", GUILayout.Width(45f));
                mUseShader = EditorGUILayout.Toggle(mUseShader, GUILayout.Width(20f));
            }
            GUILayout.EndHorizontal();
        }
    }
コード例 #3
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;
        }
    }
コード例 #4
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;
        }
    }
コード例 #5
0
    protected override void DrawProperties()
    {
        SerializedProperty sp = serializedObject.FindProperty("dragHighlight");
        Highlight          ht = sp.boolValue ? Highlight.Press : Highlight.DoNothing;

        GUILayout.BeginHorizontal();
        bool highlight = (Highlight)EditorGUILayout.EnumPopup("Drag Over", ht) == Highlight.Press;

        NGUIEditorTools.DrawPadding();
        GUILayout.EndHorizontal();
        if (sp.boolValue != highlight)
        {
            sp.boolValue = highlight;
        }

        DrawTransition();
        DrawColors();

        UIButton btn = target as UIButton;

        if (btn.tweenTarget != null)
        {
            UISprite   sprite = btn.tweenTarget.GetComponent <UISprite>();
            UI2DSprite s2d    = btn.tweenTarget.GetComponent <UI2DSprite>();

            if (sprite != null)
            {
                if (NGUIEditorTools.DrawHeader("Sprites", "Sprites", false, true))
                {
                    NGUIEditorTools.BeginContents(true);
                    EditorGUI.BeginDisabledGroup(serializedObject.isEditingMultipleObjects);
                    {
                        SerializedObject obj = new SerializedObject(sprite);
                        obj.Update();
                        SerializedProperty atlas = obj.FindProperty("mAtlas");
                        NGUIEditorTools.DrawSpriteField("Normal", obj, atlas, obj.FindProperty("mSpriteName"));
                        obj.ApplyModifiedProperties();

                        NGUIEditorTools.DrawSpriteField("Hover", serializedObject, atlas, serializedObject.FindProperty("hoverSprite"), true);
                        NGUIEditorTools.DrawSpriteField("Pressed", serializedObject, atlas, serializedObject.FindProperty("pressedSprite"), true);
                        NGUIEditorTools.DrawSpriteField("Disabled", serializedObject, atlas, serializedObject.FindProperty("disabledSprite"), true);
                    }
                    EditorGUI.EndDisabledGroup();

                    NGUIEditorTools.DrawProperty("Pixel Snap", serializedObject, "pixelSnap");
                    NGUIEditorTools.EndContents();
                }
            }
            else if (s2d != null)
            {
                if (NGUIEditorTools.DrawHeader("Sprites", "Sprites", false, true))
                {
                    NGUIEditorTools.BeginContents(true);
                    EditorGUI.BeginDisabledGroup(serializedObject.isEditingMultipleObjects);
                    {
                        SerializedObject obj = new SerializedObject(s2d);
                        obj.Update();
                        NGUIEditorTools.DrawProperty("Normal", obj, "mSprite");
                        obj.ApplyModifiedProperties();

                        NGUIEditorTools.DrawProperty("Hover", serializedObject, "hoverSprite2D");
                        NGUIEditorTools.DrawProperty("Pressed", serializedObject, "pressedSprite2D");
                        NGUIEditorTools.DrawProperty("Disabled", serializedObject, "disabledSprite2D");
                    }
                    EditorGUI.EndDisabledGroup();

                    NGUIEditorTools.DrawProperty("Pixel Snap", serializedObject, "pixelSnap");
                    NGUIEditorTools.EndContents();
                }
            }
        }

        UIButton button = target as UIButton;

        NGUIEditorTools.DrawEvents("On Click", button, button.onClick, false);
    }
コード例 #6
0
    public override void OnInspectorGUI()
    {
        NGUIEditorTools.SetLabelWidth(80f);
        mList = target as UIPopupList;

        ComponentSelector.Draw <UIAtlas>(mList.atlas, OnSelectAtlas);
        ComponentSelectorNew.Draw <Font>(mList.font.TrueTypeFont, OnSelectFont);

        UILabel lbl = EditorGUILayout.ObjectField("Text Label", mList.textLabel, typeof(UILabel), true) as UILabel;

        if (mList.textLabel != lbl)
        {
            RegisterUndo();
            mList.textLabel = lbl;
            if (lbl != null)
            {
                lbl.text = mList.selection;
            }
        }

        if (mList.atlas != null)
        {
            NGUIEditorTools.DrawSpriteField("Background", "Foreground sprite (shown on the button)", mList.atlas, mList.backgroundSprite, (name) =>
            {
                if (name != mList.backgroundSprite)
                {
                    RegisterUndo(); mList.backgroundSprite = name;
                }
            }, GUILayout.Width(120f));

            NGUIEditorTools.DrawSpriteField("Highlight", "Sprite used to highlight the selected option", mList.atlas, mList.highlightSprite, (name) =>
            {
                if (name != mList.highlightSprite)
                {
                    RegisterUndo(); mList.highlightSprite = name;
                }
            }, GUILayout.Width(120f));

            GUILayout.BeginHorizontal();
            GUILayout.Space(6f);
            GUILayout.Label("Options");
            GUILayout.EndHorizontal();

            string text = "";
            foreach (string s in mList.items)
            {
                text += s + "\n";
            }

            GUILayout.Space(-22f);
            GUILayout.BeginHorizontal();
            GUILayout.Space(84f);
            string modified = EditorGUILayout.TextArea(text, GUILayout.Height(100f));
            GUILayout.EndHorizontal();

            if (modified != text)
            {
                RegisterUndo();
                string[] split = modified.Split(new char[] { '\n' }, System.StringSplitOptions.RemoveEmptyEntries);
                mList.items.Clear();
                foreach (string s in split)
                {
                    mList.items.Add(s);
                }

                if (string.IsNullOrEmpty(mList.selection) || !mList.items.Contains(mList.selection))
                {
                    mList.selection = mList.items.Count > 0 ? mList.items[0] : "";
                }
            }

            string sel = NGUIEditorTools.DrawList("Selection", mList.items.ToArray(), mList.selection);

            if (mList.selection != sel)
            {
                RegisterUndo();
                mList.selection = sel;
            }

            float ts = EditorGUILayout.FloatField("Text Scale", mList.textScale);
            Color tc = EditorGUILayout.ColorField("Text Color", mList.textColor);
            Color bc = EditorGUILayout.ColorField("Background", mList.backgroundColor);
            Color hc = EditorGUILayout.ColorField("Highlight", mList.highlightColor);

            GUILayout.BeginHorizontal();
            bool isLocalized = EditorGUILayout.Toggle("Localized", mList.isLocalized, GUILayout.Width(100f));
            bool isAnimated  = EditorGUILayout.Toggle("Animated", mList.isAnimated);
            GUILayout.EndHorizontal();

            if (mList.textScale != ts ||
                mList.textColor != tc ||
                mList.highlightColor != hc ||
                mList.backgroundColor != bc ||
                mList.isLocalized != isLocalized ||
                mList.isAnimated != isAnimated)
            {
                RegisterUndo();
                mList.textScale       = ts;
                mList.textColor       = tc;
                mList.backgroundColor = bc;
                mList.highlightColor  = hc;
                mList.isLocalized     = isLocalized;
                mList.isAnimated      = isAnimated;
            }

            NGUIEditorTools.DrawSeparator();

            GUILayout.BeginHorizontal();
            GUILayout.Space(6f);
            GUILayout.Label("Padding", GUILayout.Width(76f));
            GUILayout.BeginVertical();
            GUILayout.Space(-12f);
            Vector2 padding = EditorGUILayout.Vector2Field("", mList.padding);
            GUILayout.EndVertical();
            GUILayout.EndHorizontal();

            if (mList.padding != padding)
            {
                RegisterUndo();
                mList.padding = padding;
            }

            NGUIEditorTools.SetLabelWidth(100f);

            GameObject go = EditorGUILayout.ObjectField("Event Receiver", mList.eventReceiver,
                                                        typeof(GameObject), true) as GameObject;

            string fn = EditorGUILayout.TextField("Function Name", mList.functionName);

            if (mList.eventReceiver != go || mList.functionName != fn)
            {
                RegisterUndo();
                mList.eventReceiver = go;
                mList.functionName  = fn;
            }
        }
    }
コード例 #7
0
    public override void OnInspectorGUI()
    {
        mFont = target as UIFont;
        NGUIEditorTools.SetLabelWidth(80f);

        GUILayout.Space(6f);

        if (mFont.replacement != null)
        {
            mType        = FontType.Reference;
            mReplacement = mFont.replacement;
        }
        else if (mFont.dynamicFont != null)
        {
            mType = FontType.Dynamic;
        }

        GUILayout.BeginHorizontal();
        FontType fontType = (FontType)EditorGUILayout.EnumPopup("Font Type", mType);

        GUILayout.Space(18f);
        GUILayout.EndHorizontal();

        if (mType != fontType)
        {
            if (fontType == FontType.Normal)
            {
                OnSelectFont(null);
            }
            else
            {
                mType = fontType;
            }

            if (mType != FontType.Dynamic && mFont.dynamicFont != null)
            {
                mFont.dynamicFont = null;
            }
        }

        if (mType == FontType.Reference)
        {
            ComponentSelector.Draw <UIFont>(mFont.replacement, OnSelectFont, true);

            GUILayout.Space(6f);
            EditorGUILayout.HelpBox("You can have one font simply point to " +
                                    "another one. This is useful if you want to be " +
                                    "able to quickly replace the contents of one " +
                                    "font with another one, for example for " +
                                    "swapping an SD font with an HD one, or " +
                                    "replacing an English font with a Chinese " +
                                    "one. All the labels referencing this font " +
                                    "will update their references to the new one.", MessageType.Info);

            if (mReplacement != mFont && mFont.replacement != mReplacement)
            {
                NGUIEditorTools.RegisterUndo("Font Change", mFont);
                mFont.replacement = mReplacement;
                UnityEditor.EditorUtility.SetDirty(mFont);
            }
            return;
        }
        else if (mType == FontType.Dynamic)
        {
#if UNITY_3_5
            EditorGUILayout.HelpBox("Dynamic fonts require Unity 4.0 or higher.", MessageType.Error);
#else
            Font fnt = EditorGUILayout.ObjectField("TTF Font", mFont.dynamicFont, typeof(Font), false) as Font;

            if (fnt != mFont.dynamicFont)
            {
                NGUIEditorTools.RegisterUndo("Font change", mFont);
                mFont.dynamicFont = fnt;
            }

            Material mat = EditorGUILayout.ObjectField("Material", mFont.material, typeof(Material), false) as Material;

            if (mFont.material != mat)
            {
                NGUIEditorTools.RegisterUndo("Font Material", mFont);
                mFont.material = mat;
            }

            GUILayout.BeginHorizontal();
            int       size  = EditorGUILayout.IntField("Default Size", mFont.defaultSize, GUILayout.Width(120f));
            FontStyle style = (FontStyle)EditorGUILayout.EnumPopup(mFont.dynamicFontStyle);
            GUILayout.Space(18f);
            GUILayout.EndHorizontal();

            if (size != mFont.defaultSize)
            {
                NGUIEditorTools.RegisterUndo("Font change", mFont);
                mFont.defaultSize = size;
            }

            if (style != mFont.dynamicFontStyle)
            {
                NGUIEditorTools.RegisterUndo("Font change", mFont);
                mFont.dynamicFontStyle = style;
            }
#endif
        }
        else
        {
            NGUIEditorTools.DrawSeparator();

            ComponentSelector.Draw <UIAtlas>(mFont.atlas, OnSelectAtlas, true);

            if (mFont.atlas != null)
            {
                if (mFont.bmFont.isValid)
                {
                    NGUIEditorTools.DrawAdvancedSpriteField(mFont.atlas, mFont.spriteName, SelectSprite, false);
                }
                EditorGUILayout.Space();
            }
            else
            {
                // No atlas specified -- set the material and texture rectangle directly
                Material mat = EditorGUILayout.ObjectField("Material", mFont.material, typeof(Material), false) as Material;

                if (mFont.material != mat)
                {
                    NGUIEditorTools.RegisterUndo("Font Material", mFont);
                    mFont.material = mat;
                }
            }

            // For updating the font's data when importing from an external source, such as the texture packer
            bool resetWidthHeight = false;

            if (mFont.atlas != null || mFont.material != null)
            {
                TextAsset data = EditorGUILayout.ObjectField("Import Data", null, typeof(TextAsset), false) as TextAsset;

                if (data != null)
                {
                    NGUIEditorTools.RegisterUndo("Import Font Data", mFont);
                    BMFontReader.Load(mFont.bmFont, NGUITools.GetHierarchy(mFont.gameObject), data.bytes);
                    mFont.MarkAsDirty();
                    resetWidthHeight = true;
                    Debug.Log("Imported " + mFont.bmFont.glyphCount + " characters");
                }
            }

            if (mFont.bmFont.isValid)
            {
                Color     green = new Color(0.4f, 1f, 0f, 1f);
                Texture2D tex   = mFont.texture;

                if (tex != null)
                {
                    if (mFont.atlas == null)
                    {
                        // Pixels are easier to work with than UVs
                        Rect pixels = NGUIMath.ConvertToPixels(mFont.uvRect, tex.width, tex.height, false);

                        // Automatically set the width and height of the rectangle to be the original font texture's dimensions
                        if (resetWidthHeight)
                        {
                            pixels.width  = mFont.texWidth;
                            pixels.height = mFont.texHeight;
                        }

                        // Font sprite rectangle
                        GUI.backgroundColor = green;
                        pixels = EditorGUILayout.RectField("Pixel Rect", pixels);
                        GUI.backgroundColor = Color.white;

                        // Create a button that can make the coordinates pixel-perfect on click
                        GUILayout.BeginHorizontal();
                        {
                            Rect corrected = NGUIMath.MakePixelPerfect(pixels);

                            if (corrected == pixels)
                            {
                                GUI.color = Color.grey;
                                GUILayout.Button("Make Pixel-Perfect");
                                GUI.color = Color.white;
                            }
                            else if (GUILayout.Button("Make Pixel-Perfect"))
                            {
                                pixels      = corrected;
                                GUI.changed = true;
                            }
                        }
                        GUILayout.EndHorizontal();

                        // Convert the pixel coordinates back to UV coordinates
                        Rect uvRect = NGUIMath.ConvertToTexCoords(pixels, tex.width, tex.height);

                        if (mFont.uvRect != uvRect)
                        {
                            NGUIEditorTools.RegisterUndo("Font Pixel Rect", mFont);
                            mFont.uvRect = uvRect;
                        }
                        //NGUIEditorTools.DrawSeparator();
                        EditorGUILayout.Space();
                    }
                }
            }
        }

        // The font must be valid at this point for the rest of the options to show up
        if (mFont.isDynamic || mFont.bmFont.isValid)
        {
            // Font spacing
            GUILayout.BeginHorizontal();
            {
                NGUIEditorTools.SetLabelWidth(0f);
                GUILayout.Label("Spacing", GUILayout.Width(60f));
                GUILayout.Label("X", GUILayout.Width(12f));
                int x = EditorGUILayout.IntField(mFont.horizontalSpacing);
                GUILayout.Label("Y", GUILayout.Width(12f));
                int y = EditorGUILayout.IntField(mFont.verticalSpacing);
                GUILayout.Space(18f);
                NGUIEditorTools.SetLabelWidth(80f);

                if (mFont.horizontalSpacing != x || mFont.verticalSpacing != y)
                {
                    NGUIEditorTools.RegisterUndo("Font Spacing", mFont);
                    mFont.horizontalSpacing = x;
                    mFont.verticalSpacing   = y;
                }
            }
            GUILayout.EndHorizontal();

            if (mFont.atlas == null)
            {
                mView      = View.Font;
                mUseShader = false;

                float pixelSize = EditorGUILayout.FloatField("Pixel Size", mFont.pixelSize, GUILayout.Width(120f));

                if (pixelSize != mFont.pixelSize)
                {
                    NGUIEditorTools.RegisterUndo("Font Change", mFont);
                    mFont.pixelSize = pixelSize;
                }
            }
            EditorGUILayout.Space();
        }

        // Preview option
        if (!mFont.isDynamic && mFont.atlas != null)
        {
            GUILayout.BeginHorizontal();
            {
                mView = (View)EditorGUILayout.EnumPopup("Preview", mView);
                GUILayout.Label("Shader", GUILayout.Width(45f));
                mUseShader = EditorGUILayout.Toggle(mUseShader, GUILayout.Width(20f));
            }
            GUILayout.EndHorizontal();
        }

        // Dynamic fonts don't support emoticons
        if (!mFont.isDynamic && mFont.bmFont.isValid)
        {
            if (mFont.atlas != null)
            {
                if (NGUIEditorTools.DrawHeader("Symbols and Emoticons"))
                {
                    NGUIEditorTools.BeginContents();

                    List <BMSymbol> symbols = mFont.symbols;

                    for (int i = 0; i < symbols.Count;)
                    {
                        BMSymbol sym = symbols[i];

                        GUILayout.BeginHorizontal();
                        GUILayout.Label(sym.sequence, GUILayout.Width(40f));
                        if (NGUIEditorTools.DrawSpriteField(mFont.atlas, sym.spriteName, ChangeSymbolSprite))
                        {
                            mSelectedSymbol = sym;
                        }

                        if (GUILayout.Button("Edit", GUILayout.Width(40f)))
                        {
                            if (mFont.atlas != null)
                            {
                                NGUISettings.selectedSprite = sym.spriteName;
                                NGUIEditorTools.Select(mFont.atlas.gameObject);
                            }
                        }

                        GUI.backgroundColor = Color.red;

                        if (GUILayout.Button("X", GUILayout.Width(22f)))
                        {
                            NGUIEditorTools.RegisterUndo("Remove symbol", mFont);
                            mSymbolSequence = sym.sequence;
                            mSymbolSprite   = sym.spriteName;
                            symbols.Remove(sym);
                            mFont.MarkAsDirty();
                        }
                        GUI.backgroundColor = Color.white;
                        GUILayout.EndHorizontal();
                        GUILayout.Space(4f);
                        ++i;
                    }

                    if (symbols.Count > 0)
                    {
                        GUILayout.Space(6f);
                    }

                    GUILayout.BeginHorizontal();
                    mSymbolSequence = EditorGUILayout.TextField(mSymbolSequence, GUILayout.Width(40f));
                    NGUIEditorTools.DrawSpriteField(mFont.atlas, mSymbolSprite, SelectSymbolSprite);

                    bool isValid = !string.IsNullOrEmpty(mSymbolSequence) && !string.IsNullOrEmpty(mSymbolSprite);
                    GUI.backgroundColor = isValid ? Color.green : Color.grey;

                    if (GUILayout.Button("Add", GUILayout.Width(40f)) && isValid)
                    {
                        NGUIEditorTools.RegisterUndo("Add symbol", mFont);
                        mFont.AddSymbol(mSymbolSequence, mSymbolSprite);
                        mFont.MarkAsDirty();
                        mSymbolSequence = "";
                        mSymbolSprite   = "";
                    }
                    GUI.backgroundColor = Color.white;
                    GUILayout.EndHorizontal();

                    if (symbols.Count == 0)
                    {
                        EditorGUILayout.HelpBox("Want to add an emoticon to your font? In the field above type ':)', choose a sprite, then hit the Add button.", MessageType.Info);
                    }
                    else
                    {
                        GUILayout.Space(4f);
                    }

                    NGUIEditorTools.EndContents();
                }
            }
        }
    }