Example #1
0
    /// <summary>
    /// Integer vector field.
    /// </summary>

    static public IntVector IntPair(string prefix, string leftCaption, string rightCaption, int x, int y)
    {
        GUILayout.BeginHorizontal();

        if (string.IsNullOrEmpty(prefix))
        {
            GUILayout.Space(82f);
        }
        else
        {
            GUILayout.Label(prefix, GUILayout.Width(74f));
        }

        RebasedEditorGUIUtility.LookLikeControls(48f);

        IntVector retVal;

        retVal.x = EditorGUILayout.IntField(leftCaption, x, GUILayout.MinWidth(30f));
        retVal.y = EditorGUILayout.IntField(rightCaption, y, GUILayout.MinWidth(30f));

        RebasedEditorGUIUtility.LookLikeControls(80f);

        GUILayout.EndHorizontal();
        return(retVal);
    }
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

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

        GUILayout.Label("Create a new UI with the following parameters:");
        NGUIEditorTools.DrawSeparator();

        GUILayout.BeginHorizontal();
        NGUISettings.layer = EditorGUILayout.LayerField("Layer", NGUISettings.layer, GUILayout.Width(200f));
        GUILayout.Space(20f);
        GUILayout.Label("This is the layer your UI will reside on");
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        camType = (CameraType)EditorGUILayout.EnumPopup("Camera", camType, GUILayout.Width(200f));
        GUILayout.Space(20f);
        GUILayout.Label("Should this UI have a camera?");
        GUILayout.EndHorizontal();

        NGUIEditorTools.DrawSeparator();
        GUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("When ready,");
        bool create = GUILayout.Button("Create Your UI", GUILayout.Width(120f));

        GUILayout.EndHorizontal();

        if (create)
        {
            CreateNewUI();
        }
    }
    public override void OnInspectorGUI()
    {
        RebasedEditorGUIUtility.LookLikeControls(80f);
        mButton = target as UIImageButton;
        mSprite = EditorGUILayout.ObjectField("Sprite", mButton.target, typeof(UISprite), true) as UISprite;

        if (mButton.target != mSprite)
        {
            NGUIEditorTools.RegisterUndo("Image Button Change", mButton);
            mButton.target = mSprite;
            if (mSprite != null)
            {
                mSprite.spriteName = mButton.normalSprite;
            }
        }

        if (mSprite != null)
        {
            ComponentSelector.Draw <UIAtlas>(mSprite.atlas, OnSelectAtlas);

            if (mSprite.atlas != null)
            {
                NGUIEditorTools.SpriteField("Normal", mSprite.atlas, mButton.normalSprite, OnNormal);
                NGUIEditorTools.SpriteField("Hover", mSprite.atlas, mButton.hoverSprite, OnHover);
                NGUIEditorTools.SpriteField("Pressed", mSprite.atlas, mButton.pressedSprite, OnPressed);
                NGUIEditorTools.SpriteField("Disabled", mSprite.atlas, mButton.disabledSprite, OnDisabled);
            }
        }
    }
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        NGUIEditorTools.DrawSeparator();
        RebasedEditorGUIUtility.LookLikeControls(80f);
        UISpriteAnimation anim = target as UISpriteAnimation;

        int fps = EditorGUILayout.IntField("Framerate", anim.framesPerSecond);

        fps = Mathf.Clamp(fps, 0, 60);

        if (anim.framesPerSecond != fps)
        {
            NGUIEditorTools.RegisterUndo("Sprite Animation Change", anim);
            anim.framesPerSecond = fps;
            EditorUtility.SetDirty(anim);
        }

        string namePrefix = EditorGUILayout.TextField("Name Prefix", (anim.namePrefix != null) ? anim.namePrefix : "");

        if (anim.namePrefix != namePrefix)
        {
            NGUIEditorTools.RegisterUndo("Sprite Animation Change", anim);
            anim.namePrefix = namePrefix;
            EditorUtility.SetDirty(anim);
        }

        bool loop = EditorGUILayout.Toggle("Loop", anim.loop);

        if (anim.loop != loop)
        {
            NGUIEditorTools.RegisterUndo("Sprite Animation Change", anim);
            anim.loop = loop;
            EditorUtility.SetDirty(anim);
        }
    }
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

    void OnGUI()
    {
        RebasedEditorGUIUtility.LookLikeControls(80f);
        GUILayout.Label("Recently used components", "LODLevelNotifyText");
        NGUIEditorTools.DrawSeparator();

        if (mObjects.Length == 0)
        {
            EditorGUILayout.HelpBox("No recently used " + mType.ToString() + " components found.\nTry drag & dropping one instead, or creating a new one.", MessageType.Info);

            bool isDone = false;

            EditorGUILayout.Space();
            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();

            if (mType == typeof(UIFont))
            {
                if (GUILayout.Button("Open the Font Maker", GUILayout.Width(150f)))
                {
                    EditorWindow.GetWindow <UIFontMaker>(false, "Font Maker", true);
                    isDone = true;
                }
            }
            else if (mType == typeof(UIAtlas))
            {
                if (GUILayout.Button("Open the Atlas Maker", GUILayout.Width(150f)))
                {
                    EditorWindow.GetWindow <UIAtlasMaker>(false, "Atlas Maker", true);
                    isDone = true;
                }
            }

            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();
            if (isDone)
            {
                Close();
            }
        }
        else
        {
            MonoBehaviour sel = null;

            foreach (MonoBehaviour o in mObjects)
            {
                if (DrawObject(o))
                {
                    sel = o;
                }
            }

            if (sel != null)
            {
                mCallback(sel);
                Close();
            }
        }
    }
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        Transform trans = target as Transform;

        RebasedEditorGUIUtility.LookLikeControls(15f);

        Vector3 pos;
        Vector3 rot;
        Vector3 scale;

        // Position
        EditorGUILayout.BeginHorizontal();
        {
            if (DrawButton("P", "Reset Position", IsResetPositionValid(trans), 20f))
            {
                NGUIEditorTools.RegisterUndo("Reset Position", trans);
                trans.localPosition = Vector3.zero;
            }
            pos = DrawVector3(trans.localPosition);
        }
        EditorGUILayout.EndHorizontal();

        // Rotation
        EditorGUILayout.BeginHorizontal();
        {
            if (DrawButton("R", "Reset Rotation", IsResetRotationValid(trans), 20f))
            {
                NGUIEditorTools.RegisterUndo("Reset Rotation", trans);
                trans.localEulerAngles = Vector3.zero;
            }
            rot = DrawVector3(trans.localEulerAngles);
        }
        EditorGUILayout.EndHorizontal();

        // Scale
        EditorGUILayout.BeginHorizontal();
        {
            if (DrawButton("S", "Reset Scale", IsResetScaleValid(trans), 20f))
            {
                NGUIEditorTools.RegisterUndo("Reset Scale", trans);
                trans.localScale = Vector3.one;
            }
            scale = DrawVector3(trans.localScale);
        }
        EditorGUILayout.EndHorizontal();

        // If something changes, set the transform values
        if (GUI.changed)
        {
            NGUIEditorTools.RegisterUndo("Transform Change", trans);
            trans.localPosition    = Validate(pos);
            trans.localEulerAngles = Validate(rot);
            trans.localScale       = Validate(scale);
        }
    }
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        RebasedEditorGUIUtility.LookLikeControls(80f);
        EditorGUILayout.Space();

        // Check to see if we can draw the widget's default properties to begin with
        if (DrawProperties())
        {
            // Draw all common properties next
            DrawCommonProperties();
            DrawExtraProperties();
        }
    }
Example #8
0
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

    void OnGUI()
    {
        RebasedEditorGUIUtility.LookLikeControls(80f);
        string newItemName = EditorGUILayout.TextField("Search for:", mItemName);

        NGUIEditorTools.DrawSeparator();

        if (GUI.changed || newItemName != mItemName)
        {
            mItemName = newItemName;

            if (string.IsNullOrEmpty(mItemName))
            {
                mResults.Clear();
            }
            else
            {
                FindAllByName(mItemName);
            }
        }

        if (mResults.Count == 0)
        {
            if (!string.IsNullOrEmpty(mItemName))
            {
                GUILayout.Label("No matches found");
            }
        }
        else
        {
            Print3("Item ID", "Item Name", "Path", false);
            NGUIEditorTools.DrawSeparator();

            foreach (FindResult fr in mResults)
            {
                if (Print3(InvDatabase.FindItemID(fr.item).ToString(),
                           fr.item.name, NGUITools.GetHierarchy(fr.db.gameObject), true))
                {
                    InvDatabaseInspector.SelectIndex(fr.db, fr.item);
                    Selection.activeGameObject = fr.db.gameObject;
                    EditorUtility.SetDirty(Selection.activeGameObject);
                }
            }
        }
    }
Example #9
0
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

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

        List <Camera> list = NGUIEditorTools.FindInScene <Camera>();

        if (list.Count > 0)
        {
            DrawRow(null);
            NGUIEditorTools.DrawSeparator();
            mScroll = GUILayout.BeginScrollView(mScroll);
            foreach (Camera cam in list)
            {
                DrawRow(cam);
            }
            GUILayout.EndScrollView();
        }
        else
        {
            GUILayout.Label("No cameras found in the scene");
        }
    }
    public override void OnInspectorGUI()
    {
        RebasedEditorGUIUtility.LookLikeControls(80f);
        UIScrollBar sb = target as UIScrollBar;

        NGUIEditorTools.DrawSeparator();

        float val   = EditorGUILayout.Slider("Value", sb.scrollValue, 0f, 1f);
        float size  = EditorGUILayout.Slider("Size", sb.barSize, 0f, 1f);
        float alpha = EditorGUILayout.Slider("Alpha", sb.alpha, 0f, 1f);

        NGUIEditorTools.DrawSeparator();

        UISprite bg = (UISprite)EditorGUILayout.ObjectField("Background", sb.background, typeof(UISprite), true);
        UISprite fg = (UISprite)EditorGUILayout.ObjectField("Foreground", sb.foreground, typeof(UISprite), true);

        UIScrollBar.Direction dir = (UIScrollBar.Direction)EditorGUILayout.EnumPopup("Direction", sb.direction);
        bool inv = EditorGUILayout.Toggle("Inverted", sb.inverted);

        if (sb.scrollValue != val ||
            sb.barSize != size ||
            sb.background != bg ||
            sb.foreground != fg ||
            sb.direction != dir ||
            sb.inverted != inv ||
            sb.alpha != alpha)
        {
            NGUIEditorTools.RegisterUndo("Scroll Bar Change", sb);
            sb.scrollValue = val;
            sb.barSize     = size;
            sb.inverted    = inv;
            sb.background  = bg;
            sb.foreground  = fg;
            sb.direction   = dir;
            sb.alpha       = alpha;
            UnityEditor.EditorUtility.SetDirty(sb);
        }
    }
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        RebasedEditorGUIUtility.LookLikeControls(80f);
        mAtlas = target as UIAtlas;

        UIAtlas.Sprite sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null;

        NGUIEditorTools.DrawSeparator();

        if (mAtlas.replacement != null)
        {
            mType        = AtlasType.Reference;
            mReplacement = mAtlas.replacement;
        }

        AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType);

        if (mType != after)
        {
            if (after == AtlasType.Normal)
            {
                OnSelectAtlas(null);
            }
            else
            {
                mType = AtlasType.Reference;
            }
        }

        if (mType == AtlasType.Reference)
        {
            ComponentSelector.Draw <UIAtlas>(mAtlas.replacement, OnSelectAtlas);

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

            if (mReplacement != mAtlas && mAtlas.replacement != mReplacement)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.replacement = mReplacement;
                UnityEditor.EditorUtility.SetDirty(mAtlas);
            }
            return;
        }

        if (!mConfirmDelete)
        {
            NGUIEditorTools.DrawSeparator();
            Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material;

            if (mAtlas.spriteMaterial != mat)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.spriteMaterial = mat;

                // Ensure that this atlas has valid import settings
                if (mAtlas.texture != null)
                {
                    NGUIEditorTools.ImportTexture(mAtlas.texture, false, false);
                }

                mAtlas.MarkAsDirty();
                mConfirmDelete = false;
            }

            if (mat != null)
            {
                TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset;

                if (ta != null)
                {
                    // Ensure that this atlas has valid import settings
                    if (mAtlas.texture != null)
                    {
                        NGUIEditorTools.ImportTexture(mAtlas.texture, false, false);
                    }

                    NGUIEditorTools.RegisterUndo("Import Sprites", mAtlas);
                    NGUIJson.LoadSpriteData(mAtlas, ta);
                    if (sprite != null)
                    {
                        sprite = mAtlas.GetSprite(sprite.name);
                    }
                    mAtlas.MarkAsDirty();
                }

                UIAtlas.Coordinates coords = (UIAtlas.Coordinates)EditorGUILayout.EnumPopup("Coordinates", mAtlas.coordinates);

                if (coords != mAtlas.coordinates)
                {
                    NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                    mAtlas.coordinates = coords;
                    mConfirmDelete     = false;
                }

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

                if (pixelSize != mAtlas.pixelSize)
                {
                    NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                    mAtlas.pixelSize = pixelSize;
                    mConfirmDelete   = false;
                }
            }
        }

        if (mAtlas.spriteMaterial != null)
        {
            Color blue  = new Color(0f, 0.7f, 1f, 1f);
            Color green = new Color(0.4f, 1f, 0f, 1f);

            if (mConfirmDelete)
            {
                if (sprite != null)
                {
                    // Show the confirmation dialog
                    NGUIEditorTools.DrawSeparator();
                    GUILayout.Label("Are you sure you want to delete '" + sprite.name + "'?");
                    NGUIEditorTools.DrawSeparator();

                    GUILayout.BeginHorizontal();
                    {
                        GUI.backgroundColor = Color.green;
                        if (GUILayout.Button("Cancel"))
                        {
                            mConfirmDelete = false;
                        }
                        GUI.backgroundColor = Color.red;

                        if (GUILayout.Button("Delete"))
                        {
                            NGUIEditorTools.RegisterUndo("Delete Sprite", mAtlas);
                            mAtlas.spriteList.Remove(sprite);
                            mConfirmDelete = false;
                        }
                        GUI.backgroundColor = Color.white;
                    }
                    GUILayout.EndHorizontal();
                }
                else
                {
                    mConfirmDelete = false;
                }
            }
            else
            {
                if (sprite == null && mAtlas.spriteList.Count > 0)
                {
                    string spriteName = NGUISettings.selectedSprite;
                    if (!string.IsNullOrEmpty(spriteName))
                    {
                        sprite = mAtlas.GetSprite(spriteName);
                    }
                    if (sprite == null)
                    {
                        sprite = mAtlas.spriteList[0];
                    }
                }

                if (!mConfirmDelete && sprite != null)
                {
                    NGUIEditorTools.DrawSeparator();
                    NGUIEditorTools.AdvancedSpriteField(mAtlas, sprite.name, SelectSprite, true);

                    if (sprite == null)
                    {
                        return;
                    }

                    Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D;

                    if (tex != null)
                    {
                        Rect inner = sprite.inner;
                        Rect outer = sprite.outer;

                        if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                        {
                            GUI.backgroundColor = green;
                            outer = NGUIEditorTools.IntRect("Dimensions", sprite.outer);

                            Vector4 border = new Vector4(
                                sprite.inner.xMin - sprite.outer.xMin,
                                sprite.inner.yMin - sprite.outer.yMin,
                                sprite.outer.xMax - sprite.inner.xMax,
                                sprite.outer.yMax - sprite.inner.yMax);

                            GUI.backgroundColor = blue;
                            border = NGUIEditorTools.IntPadding("Border", border);
                            GUI.backgroundColor = Color.white;

                            inner.xMin = sprite.outer.xMin + border.x;
                            inner.yMin = sprite.outer.yMin + border.y;
                            inner.xMax = sprite.outer.xMax - border.z;
                            inner.yMax = sprite.outer.yMax - border.w;
                        }
                        else
                        {
                            // Draw the inner and outer rectangle dimensions
                            GUI.backgroundColor = green;
                            outer = EditorGUILayout.RectField("Outer Rect", sprite.outer);
                            GUI.backgroundColor = blue;
                            inner = EditorGUILayout.RectField("Inner Rect", sprite.inner);
                            GUI.backgroundColor = Color.white;
                        }

                        if (outer.xMax < outer.xMin)
                        {
                            outer.xMax = outer.xMin;
                        }
                        if (outer.yMax < outer.yMin)
                        {
                            outer.yMax = outer.yMin;
                        }

                        if (outer != sprite.outer)
                        {
                            float x = outer.xMin - sprite.outer.xMin;
                            float y = outer.yMin - sprite.outer.yMin;

                            inner.x += x;
                            inner.y += y;
                        }

                        // Sanity checks to ensure that the inner rect is always inside the outer
                        inner.xMin = Mathf.Clamp(inner.xMin, outer.xMin, outer.xMax);
                        inner.xMax = Mathf.Clamp(inner.xMax, outer.xMin, outer.xMax);
                        inner.yMin = Mathf.Clamp(inner.yMin, outer.yMin, outer.yMax);
                        inner.yMax = Mathf.Clamp(inner.yMax, outer.yMin, outer.yMax);

                        bool changed = false;

                        if (sprite.inner != inner || sprite.outer != outer)
                        {
                            NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                            sprite.inner = inner;
                            sprite.outer = outer;
                            MarkSpriteAsDirty();
                            changed = true;
                        }

                        EditorGUILayout.Separator();

                        if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                        {
                            int left   = Mathf.RoundToInt(sprite.paddingLeft * sprite.outer.width);
                            int right  = Mathf.RoundToInt(sprite.paddingRight * sprite.outer.width);
                            int top    = Mathf.RoundToInt(sprite.paddingTop * sprite.outer.height);
                            int bottom = Mathf.RoundToInt(sprite.paddingBottom * sprite.outer.height);

                            NGUIEditorTools.IntVector a = NGUIEditorTools.IntPair("Padding", "Left", "Top", left, top);
                            NGUIEditorTools.IntVector b = NGUIEditorTools.IntPair(null, "Right", "Bottom", right, bottom);

                            if (changed || a.x != left || a.y != top || b.x != right || b.y != bottom)
                            {
                                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                                sprite.paddingLeft   = a.x / sprite.outer.width;
                                sprite.paddingTop    = a.y / sprite.outer.height;
                                sprite.paddingRight  = b.x / sprite.outer.width;
                                sprite.paddingBottom = b.y / sprite.outer.height;
                                MarkSpriteAsDirty();
                            }
                        }
                        else
                        {
                            // Create a button that can make the coordinates pixel-perfect on click
                            GUILayout.BeginHorizontal();
                            {
                                GUILayout.Label("Correction", GUILayout.Width(75f));

                                Rect corrected0 = outer;
                                Rect corrected1 = inner;

                                if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                                {
                                    corrected0 = NGUIMath.MakePixelPerfect(corrected0);
                                    corrected1 = NGUIMath.MakePixelPerfect(corrected1);
                                }
                                else
                                {
                                    corrected0 = NGUIMath.MakePixelPerfect(corrected0, tex.width, tex.height);
                                    corrected1 = NGUIMath.MakePixelPerfect(corrected1, tex.width, tex.height);
                                }

                                if (corrected0 == sprite.outer && corrected1 == sprite.inner)
                                {
                                    GUI.color = Color.grey;
                                    GUILayout.Button("Make Pixel-Perfect");
                                    GUI.color = Color.white;
                                }
                                else if (GUILayout.Button("Make Pixel-Perfect"))
                                {
                                    outer       = corrected0;
                                    inner       = corrected1;
                                    GUI.changed = true;
                                }
                            }
                            GUILayout.EndHorizontal();
                        }
                    }

                    // This functionality is no longer used. It became obsolete when the Atlas Maker was added.

                    /*NGUIEditorTools.DrawSeparator();
                     *
                     * GUILayout.BeginHorizontal();
                     * {
                     *      EditorGUILayout.PrefixLabel("Add/Delete");
                     *
                     *      if (GUILayout.Button("Clone Sprite"))
                     *      {
                     *              NGUIEditorTools.RegisterUndo("Add Sprite", mAtlas);
                     *              UIAtlas.Sprite newSprite = new UIAtlas.Sprite();
                     *
                     *              if (sprite != null)
                     *              {
                     *                      newSprite.name = "Copy of " + sprite.name;
                     *                      newSprite.outer = sprite.outer;
                     *                      newSprite.inner = sprite.inner;
                     *              }
                     *              else
                     *              {
                     *                      newSprite.name = "New Sprite";
                     *              }
                     *
                     *              mAtlas.spriteList.Add(newSprite);
                     *              sprite = newSprite;
                     *      }
                     *
                     *      // Show the delete button
                     *      GUI.backgroundColor = Color.red;
                     *
                     *      if (sprite != null && GUILayout.Button("Delete", GUILayout.Width(55f)))
                     *      {
                     *              mConfirmDelete = true;
                     *      }
                     *      GUI.backgroundColor = Color.white;
                     * }
                     * GUILayout.EndHorizontal();*/

                    if (NGUIEditorTools.previousSelection != null)
                    {
                        NGUIEditorTools.DrawSeparator();

                        GUI.backgroundColor = Color.green;

                        if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name))
                        {
                            NGUIEditorTools.SelectPrevious();
                        }
                        GUI.backgroundColor = Color.white;
                    }
                }
            }
        }
    }
Example #12
0
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        UIPanel panel = target as UIPanel;
        BetterList <UIDrawCall> drawcalls = panel.drawCalls;

        drawcalls.Sort(delegate(UIDrawCall w1, UIDrawCall w2) { return(w1.depth.CompareTo(w2.depth)); });
        RebasedEditorGUIUtility.LookLikeControls(80f);

        //NGUIEditorTools.DrawSeparator();
        EditorGUILayout.Space();

        float alpha = EditorGUILayout.Slider("Alpha", panel.alpha, 0f, 1f);

        if (alpha != panel.alpha)
        {
            NGUIEditorTools.RegisterUndo("Panel Alpha", panel);
            panel.alpha = alpha;
        }

        GUILayout.BeginHorizontal();
        bool norms = EditorGUILayout.Toggle("Normals", panel.generateNormals, GUILayout.Width(100f));

        GUILayout.Label("Needed for lit shaders");
        GUILayout.EndHorizontal();

        if (panel.generateNormals != norms)
        {
            panel.generateNormals = norms;
            panel.UpdateDrawcalls();
            EditorUtility.SetDirty(panel);
        }

        // No one seems to know how to use this properly correctly. Solution? Removing it.
        // If you know wtf you're doing, you're welcome to uncomment it.

        //GUILayout.BeginHorizontal();
        //bool depth = EditorGUILayout.Toggle("Depth Pass", panel.depthPass, GUILayout.Width(100f));
        //GUILayout.Label("Doubles draw calls, saves fillrate");
        //GUILayout.EndHorizontal();

        //if (panel.depthPass != depth)
        //{
        //    panel.depthPass = depth;
        //    panel.UpdateDrawcalls();
        //    EditorUtility.SetDirty(panel);
        //}

        //if (depth)
        //{
        //    UICamera cam = UICamera.FindCameraForLayer(panel.gameObject.layer);

        //    if (cam == null || cam.camera.isOrthoGraphic)
        //    {
        //        EditorGUILayout.HelpBox("Please note that depth pass will only save fillrate when used with 3D UIs, and only UIs drawn by the game camera. If you are using a separate camera for the UI, you will not see any benefit!", MessageType.Warning);
        //    }
        //}

        GUILayout.BeginHorizontal();
        bool sort = EditorGUILayout.Toggle("Depth Sort", panel.sortByDepth, GUILayout.Width(100f));

        GUILayout.Label("Sort widgets by depth (ignore Z)");
        GUILayout.EndHorizontal();

        if (panel.sortByDepth != sort)
        {
            panel.sortByDepth = sort;
            panel.UpdateDrawcalls();
            EditorUtility.SetDirty(panel);
        }

        if (!sort)
        {
            EditorGUILayout.HelpBox("Keep the 'Depth Sort' flag turned on, unless you are working with a UI created prior to NGUI 2.7.0 and want to sort by Z in addition to Depth.", MessageType.Warning);
        }

        GUILayout.BeginHorizontal();
        bool cull = EditorGUILayout.Toggle("Cull", panel.cullWhileDragging, GUILayout.Width(100f));

        GUILayout.Label("Cull widgets while dragging them");
        GUILayout.EndHorizontal();

        if (panel.cullWhileDragging != cull)
        {
            panel.cullWhileDragging = cull;
            panel.UpdateDrawcalls();
            EditorUtility.SetDirty(panel);
        }

        GUILayout.BeginHorizontal();
        bool stat = EditorGUILayout.Toggle("Static", panel.widgetsAreStatic, GUILayout.Width(100f));

        GUILayout.Label("Check if widgets won't move");
        GUILayout.EndHorizontal();

        if (panel.widgetsAreStatic != stat)
        {
            panel.widgetsAreStatic = stat;
            panel.UpdateDrawcalls();
            EditorUtility.SetDirty(panel);
        }

        if (stat)
        {
            EditorGUILayout.HelpBox("Only mark the panel as 'static' if you know FOR CERTAIN that the widgets underneath will not move, rotate, or scale. Doing this improves performance, but moving widgets around will have no effect.", MessageType.Warning);
        }

        if (panel.showInPanelTool != EditorGUILayout.Toggle("Panel Tool", panel.showInPanelTool))
        {
            panel.showInPanelTool = !panel.showInPanelTool;
            EditorUtility.SetDirty(panel);
            EditorWindow.FocusWindowIfItsOpen <UIPanelTool>();
        }

        UIPanel.DebugInfo di = (UIPanel.DebugInfo)EditorGUILayout.EnumPopup("Debug Info", panel.debugInfo);

        if (panel.debugInfo != di)
        {
            panel.debugInfo = di;
            EditorUtility.SetDirty(panel);
        }

        UIDrawCall.Clipping clipping = (UIDrawCall.Clipping)EditorGUILayout.EnumPopup("Clipping", panel.clipping);

        if (panel.clipping != clipping)
        {
            panel.clipping = clipping;
            EditorUtility.SetDirty(panel);
        }

        if (panel.clipping != UIDrawCall.Clipping.None)
        {
            Vector4 range = panel.clipRange;

            GUILayout.BeginHorizontal();
            GUILayout.Space(80f);
            Vector2 pos = EditorGUILayout.Vector2Field("Center", new Vector2(range.x, range.y));
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Space(80f);
            Vector2 size = EditorGUILayout.Vector2Field("Size", new Vector2(range.z, range.w));
            GUILayout.EndHorizontal();

            if (size.x < 0f)
            {
                size.x = 0f;
            }
            if (size.y < 0f)
            {
                size.y = 0f;
            }

            range.x = pos.x;
            range.y = pos.y;
            range.z = size.x;
            range.w = size.y;

            if (panel.clipRange != range)
            {
                NGUIEditorTools.RegisterUndo("Clipping Change", panel);
                panel.clipRange = range;
                EditorUtility.SetDirty(panel);
            }

            if (panel.clipping == UIDrawCall.Clipping.SoftClip)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Space(80f);
                Vector2 soft = EditorGUILayout.Vector2Field("Softness", panel.clipSoftness);
                GUILayout.EndHorizontal();

                if (soft.x < 1f)
                {
                    soft.x = 1f;
                }
                if (soft.y < 1f)
                {
                    soft.y = 1f;
                }

                if (panel.clipSoftness != soft)
                {
                    NGUIEditorTools.RegisterUndo("Clipping Change", panel);
                    panel.clipSoftness = soft;
                    EditorUtility.SetDirty(panel);
                }
            }

#if !UNITY_3_5 && !UNITY_4_0 && (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8 || UNITY_BLACKBERRY)
            if (PlayerSettings.targetGlesGraphics == TargetGlesGraphics.OpenGLES_1_x)
            {
                EditorGUILayout.HelpBox("Clipping requires shader support!\n\nOpen File -> Build Settings -> Player Settings -> Other Settings, then set:\n\n- Graphics Level: OpenGL ES 2.0.", MessageType.Error);
            }
#endif
        }

        if (clipping == UIDrawCall.Clipping.HardClip)
        {
            EditorGUILayout.HelpBox("Hard clipping has been removed due to major performance issues on certain Android devices. Alpha clipping will be used instead.", MessageType.Warning);
        }

        if (clipping != UIDrawCall.Clipping.None && !NGUIEditorTools.IsUniform(panel.transform.lossyScale))
        {
            EditorGUILayout.HelpBox("Clipped panels must have a uniform scale, or clipping won't work properly!", MessageType.Error);

            if (GUILayout.Button("Auto-fix"))
            {
                NGUIEditorTools.FixUniform(panel.gameObject);
            }
        }

        if (panel.drawCalls.size > 0 && NGUIEditorTools.DrawHeader(panel.drawCalls.size + " draw calls from " + panel.widgets.size + " widgets", "DrawCalls"))
        {
            NGUIEditorTools.BeginContents();

            foreach (UIDrawCall dc in drawcalls)
            {
                EditorGUILayout.ObjectField("Material", dc.material, typeof(Material), false);
                EditorGUILayout.LabelField("Triangles", dc.triangles.ToString());

                if (clipping != UIDrawCall.Clipping.None && !dc.isClipped)
                {
                    EditorGUILayout.HelpBox("You must switch this material's shader to Unlit/Transparent Colored or Unlit/Premultiplied Colored in order for clipping to work.",
                                            MessageType.Warning);
                }
            }
            NGUIEditorTools.EndContents();
        }
    }
Example #13
0
    public override void OnInspectorGUI()
    {
        RebasedEditorGUIUtility.LookLikeControls(80f);
        UISlider slider = target as UISlider;

        NGUIEditorTools.DrawSeparator();

        float sliderValue = EditorGUILayout.Slider("Value", slider.sliderValue, 0f, 1f);

        if (slider.sliderValue != sliderValue)
        {
            NGUIEditorTools.RegisterUndo("Slider Change", slider);
            slider.sliderValue = sliderValue;
            UnityEditor.EditorUtility.SetDirty(slider);
        }

        int steps = EditorGUILayout.IntSlider("Steps", slider.numberOfSteps, 0, 11);

        if (slider.numberOfSteps != steps)
        {
            NGUIEditorTools.RegisterUndo("Slider Change", slider);
            slider.numberOfSteps = steps;
            slider.ForceUpdate();
            UnityEditor.EditorUtility.SetDirty(slider);
        }

        NGUIEditorTools.DrawSeparator();

        Transform fg = EditorGUILayout.ObjectField("Foreground", slider.foreground, typeof(Transform), true) as Transform;
        Transform tb = EditorGUILayout.ObjectField("Thumb", slider.thumb, typeof(Transform), true) as Transform;

        UISlider.Direction dir = (UISlider.Direction)EditorGUILayout.EnumPopup("Direction", slider.direction);

        // If we're using a sprite for the foreground, ensure it's using a proper pivot.
        ValidatePivot(fg, "Foreground sprite", dir);

        NGUIEditorTools.DrawSeparator();

        GameObject er = EditorGUILayout.ObjectField("Event Recv.", slider.eventReceiver, typeof(GameObject), true) as GameObject;

        GUILayout.BeginHorizontal();
        string fn = EditorGUILayout.TextField("Function", slider.functionName);

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

        if (slider.foreground != fg ||
            slider.thumb != tb ||
            slider.direction != dir ||
            slider.eventReceiver != er ||
            slider.functionName != fn)
        {
            NGUIEditorTools.RegisterUndo("Slider Change", slider);
            slider.foreground    = fg;
            slider.thumb         = tb;
            slider.direction     = dir;
            slider.eventReceiver = er;
            slider.functionName  = fn;

            if (slider.thumb != null)
            {
                slider.thumb.localPosition = Vector3.zero;
                slider.sliderValue         = -1f;
                slider.sliderValue         = sliderValue;
            }
            else
            {
                slider.ForceUpdate();
            }

            UnityEditor.EditorUtility.SetDirty(slider);
        }
    }
Example #14
0
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

    void OnGUI()
    {
        List <UIPanel> panels = GetListOfPanels();

        if (panels != null && panels.Count > 0)
        {
            UIPanel selectedPanel = NGUITools.FindInParents <UIPanel>(Selection.activeGameObject);

            // First, collect a list of panels with their associated widgets
            List <Entry> entries       = new List <Entry>();
            Entry        selectedEntry = null;
            bool         allEnabled    = true;

            foreach (UIPanel panel in panels)
            {
                Entry ent = new Entry();
                ent.panel          = panel;
                ent.widgets        = GetWidgets(panel);
                ent.isEnabled      = panel.enabled && NGUITools.GetActive(panel.gameObject);
                ent.widgetsEnabled = ent.isEnabled;

                if (ent.widgetsEnabled)
                {
                    foreach (UIWidget w in ent.widgets)
                    {
                        if (!NGUITools.GetActive(w.gameObject))
                        {
                            allEnabled         = false;
                            ent.widgetsEnabled = false;
                            break;
                        }
                    }
                }
                else
                {
                    allEnabled = false;
                }
                entries.Add(ent);
            }

            // Sort the list alphabetically
            entries.Sort(Compare);

            RebasedEditorGUIUtility.LookLikeControls(80f);
            bool showAll = DrawRow(null, null, allEnabled);
            NGUIEditorTools.DrawSeparator();

            mScroll = GUILayout.BeginScrollView(mScroll);

            foreach (Entry ent in entries)
            {
                if (DrawRow(ent, selectedPanel, ent.widgetsEnabled))
                {
                    selectedEntry = ent;
                }
            }
            GUILayout.EndScrollView();

            if (showAll)
            {
                foreach (Entry ent in entries)
                {
                    SetActiveState(ent.panel, !allEnabled);
                }
            }
            else if (selectedEntry != null)
            {
                SetActiveState(selectedEntry.panel, !selectedEntry.widgetsEnabled);
            }
        }
        else
        {
            GUILayout.Label("No UI Panels found in the scene");
        }
    }
Example #15
0
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

    void OnGUI()
    {
        // Load the saved preferences
        if (!mLoaded)
        {
            mLoaded = true; Load();
        }

        RebasedEditorGUIUtility.LookLikeControls(80f);
        GameObject go = NGUIEditorTools.SelectedRoot();

        if (go == null)
        {
            GUILayout.Label("You must create a UI first.");

            if (GUILayout.Button("Open the New UI Wizard"))
            {
                EditorWindow.GetWindow <UICreateNewUIWizard>(false, "New UI", true);
            }
        }
        else
        {
            GUILayout.Space(4f);

            GUILayout.BeginHorizontal();
            ComponentSelector.Draw <UIAtlas>(NGUISettings.atlas, OnSelectAtlas, GUILayout.Width(140f));
            GUILayout.Label("Texture atlas used by widgets", GUILayout.MinWidth(10000f));
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            ComponentSelector.Draw <UIFont>(NGUISettings.font, OnSelectFont, GUILayout.Width(140f));
            GUILayout.Label("Font used by labels", GUILayout.MinWidth(10000f));
            GUILayout.EndHorizontal();

            GUILayout.Space(-2f);
            NGUIEditorTools.DrawSeparator();

            GUILayout.BeginHorizontal();
            WidgetType wt = (WidgetType)EditorGUILayout.EnumPopup("Template", mType, GUILayout.Width(200f));
            GUILayout.Space(20f);
            GUILayout.Label("Select a widget template to use");
            GUILayout.EndHorizontal();

            if (mType != wt)
            {
                mType = wt; Save();
            }

            switch (mType)
            {
            case WidgetType.Label:                  CreateLabel(go); break;

            case WidgetType.Sprite:                 CreateSprite(go, mSprite); break;

            case WidgetType.Texture:                CreateSimpleTexture(go); break;

            case WidgetType.Button:                 CreateButton(go); break;

            case WidgetType.ImageButton:    CreateImageButton(go); break;

            case WidgetType.Checkbox:               CreateCheckbox(go); break;

            case WidgetType.ProgressBar:    CreateSlider(go, false); break;

            case WidgetType.Slider:                 CreateSlider(go, true); break;

            case WidgetType.Input:                  CreateInput(go); break;

            case WidgetType.PopupList:              CreatePopup(go, true); break;

            case WidgetType.PopupMenu:              CreatePopup(go, false); break;

            case WidgetType.ScrollBar:              CreateScrollBar(go); break;
            }
        }
    }
    public override void OnInspectorGUI()
    {
        mFont = target as UIFont;
        RebasedEditorGUIUtility.LookLikeControls(80f);

        NGUIEditorTools.DrawSeparator();

        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);

            NGUIEditorTools.DrawSeparator();
            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
            NGUIEditorTools.DrawSeparator();
            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;
            }

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

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

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

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

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

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

            if (mFont.atlas != null)
            {
                if (mFont.bmFont.isValid)
                {
                    NGUIEditorTools.AdvancedSpriteField(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();
            {
                RebasedEditorGUIUtility.LookLikeControls(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);
                RebasedEditorGUIUtility.LookLikeControls(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)
            {
                NGUIEditorTools.DrawHeader("Symbols and Emoticons");

                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.SimpleSpriteField(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)
                {
                    NGUIEditorTools.DrawSeparator();
                }

                GUILayout.BeginHorizontal();
                mSymbolSequence = EditorGUILayout.TextField(mSymbolSequence, GUILayout.Width(40f));
                NGUIEditorTools.SimpleSpriteField(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);
                }
            }
        }
    }
Example #17
0
    /// <summary>
    /// Draw the UI for this tool.
    /// </summary>

    void OnGUI()
    {
        bool create  = false;
        bool update  = false;
        bool replace = false;

        string prefabPath = "";
        string matPath    = "";

        // If we have an atlas to work with, see if we can figure out the path for it and its material
        if (NGUISettings.atlas != null && NGUISettings.atlas.name == NGUISettings.atlasName)
        {
            prefabPath = AssetDatabase.GetAssetPath(NGUISettings.atlas.gameObject.GetInstanceID());
            if (NGUISettings.atlas.spriteMaterial != null)
            {
                matPath = AssetDatabase.GetAssetPath(NGUISettings.atlas.spriteMaterial.GetInstanceID());
            }
        }

        // Assume default values if needed
        if (string.IsNullOrEmpty(NGUISettings.atlasName))
        {
            NGUISettings.atlasName = "New Atlas";
        }
        if (string.IsNullOrEmpty(prefabPath))
        {
            prefabPath = NGUIEditorTools.GetSelectionFolder() + NGUISettings.atlasName + ".prefab";
        }
        if (string.IsNullOrEmpty(matPath))
        {
            matPath = NGUIEditorTools.GetSelectionFolder() + NGUISettings.atlasName + ".mat";
        }

        // Try to load the prefab
        GameObject go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;

        if (NGUISettings.atlas == null && go != null)
        {
            NGUISettings.atlas = go.GetComponent <UIAtlas>();
        }

        RebasedEditorGUIUtility.LookLikeControls(80f);

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

        if (go == null)
        {
            GUI.backgroundColor = Color.green;
            create = GUILayout.Button("Create", GUILayout.Width(76f));
        }
        else
        {
            GUI.backgroundColor = Color.red;
            create = GUILayout.Button("Replace", GUILayout.Width(76f));
        }

        GUI.backgroundColor    = Color.white;
        NGUISettings.atlasName = GUILayout.TextField(NGUISettings.atlasName);
        GUILayout.EndHorizontal();

        if (create)
        {
            // If the prefab already exists, confirm that we want to overwrite it
            if (go == null || EditorUtility.DisplayDialog("Are you sure?", "Are you sure you want to replace the contents of the " +
                                                          NGUISettings.atlasName + " atlas with the textures currently selected in the Project View? All other sprites will be deleted.", "Yes", "No"))
            {
                replace = true;

                // Try to load the material
                Material mat = AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)) as Material;

                // If the material doesn't exist, create it
                if (mat == null)
                {
                    Shader shader = Shader.Find(NGUISettings.atlasPMA ? "Unlit/Premultiplied Colored" : "Unlit/Transparent Colored");
                    mat = new Material(shader);

                    // Save the material
                    AssetDatabase.CreateAsset(mat, matPath);
                    AssetDatabase.Refresh();

                    // Load the material so it's usable
                    mat = AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)) as Material;
                }

                if (NGUISettings.atlas == null || NGUISettings.atlas.name != NGUISettings.atlasName)
                {
                    // Create a new prefab for the atlas
                    Object prefab = (go != null) ? go : PrefabUtility.CreateEmptyPrefab(prefabPath);

                    // Create a new game object for the atlas
                    go = new GameObject(NGUISettings.atlasName);
                    go.AddComponent <UIAtlas>().spriteMaterial = mat;

                    // Update the prefab
                    PrefabUtility.ReplacePrefab(go, prefab);
                    DestroyImmediate(go);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();

                    // Select the atlas
                    go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;
                    NGUISettings.atlas = go.GetComponent <UIAtlas>();
                }
            }
        }

        ComponentSelector.Draw <UIAtlas>("Select", NGUISettings.atlas, OnSelectAtlas);

        List <Texture> textures = GetSelectedTextures();

        if (NGUISettings.atlas != null && NGUISettings.atlas.name == NGUISettings.atlasName)
        {
            Material mat = NGUISettings.atlas.spriteMaterial;
            Texture  tex = NGUISettings.atlas.texture;

            // Material information
            GUILayout.BeginHorizontal();
            {
                if (mat != null)
                {
                    if (GUILayout.Button("Material", GUILayout.Width(76f)))
                    {
                        Selection.activeObject = mat;
                    }
                    GUILayout.Label(" " + mat.name);
                }
                else
                {
                    GUI.color = Color.grey;
                    GUILayout.Button("Material", GUILayout.Width(76f));
                    GUI.color = Color.white;
                    GUILayout.Label(" N/A");
                }
            }
            GUILayout.EndHorizontal();

            // Texture atlas information
            GUILayout.BeginHorizontal();
            {
                if (tex != null)
                {
                    if (GUILayout.Button("Texture", GUILayout.Width(76f)))
                    {
                        Selection.activeObject = tex;
                    }
                    GUILayout.Label(" " + tex.width + "x" + tex.height);
                }
                else
                {
                    GUI.color = Color.grey;
                    GUILayout.Button("Texture", GUILayout.Width(76f));
                    GUI.color = Color.white;
                    GUILayout.Label(" N/A");
                }
            }
            GUILayout.EndHorizontal();
        }

        GUILayout.BeginHorizontal();
        NGUISettings.atlasPadding = Mathf.Clamp(EditorGUILayout.IntField("Padding", NGUISettings.atlasPadding, GUILayout.Width(100f)), 0, 8);
        GUILayout.Label((NGUISettings.atlasPadding == 1 ? "pixel" : "pixels") + " in-between of sprites");
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        NGUISettings.atlasTrimming = EditorGUILayout.Toggle("Trim Alpha", NGUISettings.atlasTrimming, GUILayout.Width(100f));
        GUILayout.Label("Remove empty space");
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        NGUISettings.atlasPMA = EditorGUILayout.Toggle("PMA Shader", NGUISettings.atlasPMA, GUILayout.Width(100f));
        GUILayout.Label("Pre-multiply color by alpha");
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        NGUISettings.unityPacking = EditorGUILayout.Toggle("Unity Packer", NGUISettings.unityPacking, GUILayout.Width(100f));
        GUILayout.Label("if off, use a custom packer");
        GUILayout.EndHorizontal();

        if (!NGUISettings.unityPacking)
        {
            GUILayout.BeginHorizontal();
            NGUISettings.forceSquareAtlas = EditorGUILayout.Toggle("Force Square", NGUISettings.forceSquareAtlas, GUILayout.Width(100f));
            GUILayout.Label("if on, forces a square atlas texture");
            GUILayout.EndHorizontal();
        }

#if UNITY_IPHONE || UNITY_ANDROID
        GUILayout.BeginHorizontal();
        NGUISettings.allow4096 = EditorGUILayout.Toggle("4096x4096", NGUISettings.allow4096, GUILayout.Width(100f));
        GUILayout.Label("if off, limit atlases to 2048x2048");
        GUILayout.EndHorizontal();
#endif
        if (NGUISettings.atlas != null && NGUISettings.atlas.name == NGUISettings.atlasName)
        {
            if (textures.Count > 0)
            {
                GUI.backgroundColor = Color.green;
                update = GUILayout.Button("Add/Update All");
                GUI.backgroundColor = Color.white;
            }
            else
            {
                EditorGUILayout.HelpBox("You can reveal more options by selecting one or more textures in the Project View window.", MessageType.Info);
            }
        }
        else
        {
            EditorGUILayout.HelpBox("You can create a new atlas by selecting one or more textures in the Project View window, then clicking \"Create\".", MessageType.Info);
        }

        string selection = null;
        Dictionary <string, int> spriteList = GetSpriteList(textures);

        if (spriteList.Count > 0)
        {
            NGUIEditorTools.DrawHeader("Sprites", true);
            {
                GUILayout.BeginHorizontal();
                GUILayout.Space(3f);
                GUILayout.BeginVertical();

                mScroll = GUILayout.BeginScrollView(mScroll);

                bool delete = false;
                int  index  = 0;
                foreach (KeyValuePair <string, int> iter in spriteList)
                {
                    ++index;

                    GUILayout.Space(-1f);
                    bool highlight = (UIAtlasInspector.instance != null) && (NGUISettings.selectedSprite == iter.Key);
                    GUI.backgroundColor = highlight ? Color.white : new Color(0.8f, 0.8f, 0.8f);
                    GUILayout.BeginHorizontal("AS TextArea", GUILayout.MinHeight(20f));
                    GUI.backgroundColor = Color.white;
                    GUILayout.Label(index.ToString(), GUILayout.Width(24f));

                    if (GUILayout.Button(iter.Key, "OL TextField", GUILayout.Height(20f)))
                    {
                        selection = iter.Key;
                    }

                    if (iter.Value == 2)
                    {
                        GUI.color = Color.green;
                        GUILayout.Label("Add", GUILayout.Width(27f));
                        GUI.color = Color.white;
                    }
                    else if (iter.Value == 1)
                    {
                        GUI.color = Color.cyan;
                        GUILayout.Label("Update", GUILayout.Width(45f));
                        GUI.color = Color.white;
                    }
                    else
                    {
                        if (mDelNames.Contains(iter.Key))
                        {
                            GUI.backgroundColor = Color.red;

                            if (GUILayout.Button("Delete", GUILayout.Width(60f)))
                            {
                                delete = true;
                            }
                            GUI.backgroundColor = Color.green;
                            if (GUILayout.Button("X", GUILayout.Width(22f)))
                            {
                                mDelNames.Remove(iter.Key);
                                delete = false;
                            }
                            GUI.backgroundColor = Color.white;
                        }
                        else
                        {
                            // If we have not yet selected a sprite for deletion, show a small "X" button
                            if (GUILayout.Button("X", GUILayout.Width(22f)))
                            {
                                mDelNames.Add(iter.Key);
                            }
                        }
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndScrollView();
                GUILayout.EndVertical();
                GUILayout.Space(3f);
                GUILayout.EndHorizontal();

                // If this sprite was marked for deletion, remove it from the atlas
                if (delete)
                {
                    List <SpriteEntry> sprites = new List <SpriteEntry>();
                    ExtractSprites(NGUISettings.atlas, sprites);

                    for (int i = sprites.Count; i > 0;)
                    {
                        SpriteEntry ent = sprites[--i];
                        if (mDelNames.Contains(ent.name))
                        {
                            sprites.RemoveAt(i);
                        }
                    }
                    UpdateAtlas(NGUISettings.atlas, sprites);
                    mDelNames.Clear();
                }
                else if (update)
                {
                    UpdateAtlas(textures, true);
                }
                else if (replace)
                {
                    UpdateAtlas(textures, false);
                }

                if (NGUISettings.atlas != null && !string.IsNullOrEmpty(selection))
                {
                    NGUISettings.selectedSprite = selection;
                    Selection.activeGameObject  = NGUISettings.atlas.gameObject;

                    if (UIAtlasInspector.instance != null)
                    {
                        UIAtlasInspector.instance.Repaint();
                    }
                }
            }
        }
    }
Example #18
0
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

    void OnGUI()
    {
        RebasedEditorGUIUtility.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)
                    {
                        UIAtlas.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);
                                }

                                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 == UIAtlas.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();
            }
        }
    }
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        RebasedEditorGUIUtility.LookLikeControls(80f);
        InvDatabase db = target as InvDatabase;

        NGUIEditorTools.DrawSeparator();

        InvBaseItem item = null;

        if (db.items == null || db.items.Count == 0)
        {
            mIndex = 0;
        }
        else
        {
            mIndex = Mathf.Clamp(mIndex, 0, db.items.Count - 1);
            item   = db.items[mIndex];
        }

        if (mConfirmDelete)
        {
            // Show the confirmation dialog
            GUILayout.Label("Are you sure you want to delete '" + item.name + "'?");
            NGUIEditorTools.DrawSeparator();

            GUILayout.BeginHorizontal();
            {
                GUI.backgroundColor = Color.green;
                if (GUILayout.Button("Cancel"))
                {
                    mConfirmDelete = false;
                }
                GUI.backgroundColor = Color.red;

                if (GUILayout.Button("Delete"))
                {
                    NGUIEditorTools.RegisterUndo("Delete Inventory Item", db);
                    db.items.RemoveAt(mIndex);
                    mConfirmDelete = false;
                }
                GUI.backgroundColor = Color.white;
            }
            GUILayout.EndHorizontal();
        }
        else
        {
            // Database icon atlas
            UIAtlas atlas = EditorGUILayout.ObjectField("Icon Atlas", db.iconAtlas, typeof(UIAtlas), false) as UIAtlas;

            if (atlas != db.iconAtlas)
            {
                NGUIEditorTools.RegisterUndo("Databse Atlas change", db);
                db.iconAtlas = atlas;
                foreach (InvBaseItem i in db.items)
                {
                    i.iconAtlas = atlas;
                }
            }

            // Database ID
            int dbID = EditorGUILayout.IntField("Database ID", db.databaseID);

            if (dbID != db.databaseID)
            {
                NGUIEditorTools.RegisterUndo("Database ID change", db);
                db.databaseID = dbID;
            }

            // "New" button
            GUI.backgroundColor = Color.green;

            if (GUILayout.Button("New Item"))
            {
                NGUIEditorTools.RegisterUndo("Add Inventory Item", db);

                InvBaseItem bi = new InvBaseItem();
                bi.iconAtlas = db.iconAtlas;
                bi.id16      = (db.items.Count > 0) ? db.items[db.items.Count - 1].id16 + 1 : 0;
                db.items.Add(bi);
                mIndex = db.items.Count - 1;

                if (item != null)
                {
                    bi.name         = "Copy of " + item.name;
                    bi.description  = item.description;
                    bi.slot         = item.slot;
                    bi.color        = item.color;
                    bi.iconName     = item.iconName;
                    bi.attachment   = item.attachment;
                    bi.minItemLevel = item.minItemLevel;
                    bi.maxItemLevel = item.maxItemLevel;

                    foreach (InvStat stat in item.stats)
                    {
                        InvStat copy = new InvStat();
                        copy.id       = stat.id;
                        copy.amount   = stat.amount;
                        copy.modifier = stat.modifier;
                        bi.stats.Add(copy);
                    }
                }
                else
                {
                    bi.name        = "New Item";
                    bi.description = "Item Description";
                }

                item = bi;
            }
            GUI.backgroundColor = Color.white;

            if (item != null)
            {
                NGUIEditorTools.DrawSeparator();

                // Navigation section
                GUILayout.BeginHorizontal();
                {
                    if (mIndex == 0)
                    {
                        GUI.color = Color.grey;
                    }
                    if (GUILayout.Button("<<"))
                    {
                        mConfirmDelete = false; --mIndex;
                    }
                    GUI.color = Color.white;
                    mIndex    = EditorGUILayout.IntField(mIndex + 1, GUILayout.Width(40f)) - 1;
                    GUILayout.Label("/ " + db.items.Count, GUILayout.Width(40f));
                    if (mIndex + 1 == db.items.Count)
                    {
                        GUI.color = Color.grey;
                    }
                    if (GUILayout.Button(">>"))
                    {
                        mConfirmDelete = false; ++mIndex;
                    }
                    GUI.color = Color.white;
                }
                GUILayout.EndHorizontal();

                NGUIEditorTools.DrawSeparator();

                // Item name and delete item button
                GUILayout.BeginHorizontal();
                {
                    string itemName = EditorGUILayout.TextField("Item Name", item.name);

                    GUI.backgroundColor = Color.red;

                    if (GUILayout.Button("Delete", GUILayout.Width(55f)))
                    {
                        mConfirmDelete = true;
                    }
                    GUI.backgroundColor = Color.white;

                    if (!itemName.Equals(item.name))
                    {
                        NGUIEditorTools.RegisterUndo("Rename Item", db);
                        item.name = itemName;
                    }
                }
                GUILayout.EndHorizontal();

                string           itemDesc   = GUILayout.TextArea(item.description, 200, GUILayout.Height(100f));
                InvBaseItem.Slot slot       = (InvBaseItem.Slot)EditorGUILayout.EnumPopup("Slot", item.slot);
                string           iconName   = "";
                float            iconSize   = 64f;
                bool             drawIcon   = false;
                float            extraSpace = 0f;

                if (item.iconAtlas != null)
                {
                    BetterList <string> sprites = item.iconAtlas.GetListOfSprites();
                    sprites.Insert(0, "<None>");

                    int    index      = 0;
                    string spriteName = (item.iconName != null) ? item.iconName : sprites[0];

                    // We need to find the sprite in order to have it selected
                    if (!string.IsNullOrEmpty(spriteName))
                    {
                        for (int i = 1; i < sprites.size; ++i)
                        {
                            if (spriteName.Equals(sprites[i], System.StringComparison.OrdinalIgnoreCase))
                            {
                                index = i;
                                break;
                            }
                        }
                    }

                    // Draw the sprite selection popup
                    index = EditorGUILayout.Popup("Icon", index, sprites.ToArray());
                    UIAtlas.Sprite sprite = (index > 0) ? item.iconAtlas.GetSprite(sprites[index]) : null;

                    if (sprite != null)
                    {
                        iconName = sprite.name;

                        Material mat = item.iconAtlas.spriteMaterial;

                        if (mat != null)
                        {
                            Texture2D tex = mat.mainTexture as Texture2D;

                            if (tex != null)
                            {
                                drawIcon = true;
                                Rect rect = sprite.outer;

                                if (item.iconAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                                {
                                    rect = NGUIMath.ConvertToTexCoords(rect, tex.width, tex.height);
                                }

                                GUILayout.Space(4f);
                                GUILayout.BeginHorizontal();
                                {
                                    GUILayout.Space(Screen.width - iconSize);
                                    DrawSprite(tex, rect, null, false);
                                }
                                GUILayout.EndHorizontal();

                                extraSpace = iconSize * (float)sprite.outer.height / sprite.outer.width;
                            }
                        }
                    }
                }

                // Item level range
                GUILayout.BeginHorizontal();
                GUILayout.Label("Level Range", GUILayout.Width(77f));
                int min = EditorGUILayout.IntField(item.minItemLevel, GUILayout.MinWidth(40f));
                int max = EditorGUILayout.IntField(item.maxItemLevel, GUILayout.MinWidth(40f));
                if (drawIcon)
                {
                    GUILayout.Space(iconSize);
                }
                GUILayout.EndHorizontal();

                // Game Object attachment field, left of the icon
                GUILayout.BeginHorizontal();
                GameObject go = (GameObject)EditorGUILayout.ObjectField("Attachment", item.attachment, typeof(GameObject), false);
                if (drawIcon)
                {
                    GUILayout.Space(iconSize);
                }
                GUILayout.EndHorizontal();

                // Color tint field, left of the icon
                GUILayout.BeginHorizontal();
                Color color = EditorGUILayout.ColorField("Color", item.color);
                if (drawIcon)
                {
                    GUILayout.Space(iconSize);
                }
                GUILayout.EndHorizontal();

                // Calculate the extra spacing necessary for the icon to show up properly and not overlap anything
                if (drawIcon)
                {
                    extraSpace = Mathf.Max(0f, extraSpace - 60f);
                    GUILayout.Space(extraSpace);
                }

                // Item stats
                NGUIEditorTools.DrawSeparator();

                if (item.stats != null)
                {
                    for (int i = 0; i < item.stats.Count; ++i)
                    {
                        InvStat stat = item.stats[i];

                        GUILayout.BeginHorizontal();
                        {
                            InvStat.Identifier iden = (InvStat.Identifier)EditorGUILayout.EnumPopup(stat.id, GUILayout.Width(80f));

                            // Color the field red if it's negative, green if it's positive
                            if (stat.amount > 0)
                            {
                                GUI.backgroundColor = Color.green;
                            }
                            else if (stat.amount < 0)
                            {
                                GUI.backgroundColor = Color.red;
                            }
                            int amount = EditorGUILayout.IntField(stat.amount, GUILayout.Width(40f));
                            GUI.backgroundColor = Color.white;

                            InvStat.Modifier mod = (InvStat.Modifier)EditorGUILayout.EnumPopup(stat.modifier);

                            GUI.backgroundColor = Color.red;
                            if (GUILayout.Button("X", GUILayout.Width(20f)))
                            {
                                NGUIEditorTools.RegisterUndo("Delete Item Stat", db);
                                item.stats.RemoveAt(i);
                                --i;
                            }
                            else if (iden != stat.id || amount != stat.amount || mod != stat.modifier)
                            {
                                NGUIEditorTools.RegisterUndo("Item Stats", db);
                                stat.id       = iden;
                                stat.amount   = amount;
                                stat.modifier = mod;
                            }
                            GUI.backgroundColor = Color.white;
                        }
                        GUILayout.EndHorizontal();
                    }
                }

                if (GUILayout.Button("Add Stat", GUILayout.Width(80f)))
                {
                    NGUIEditorTools.RegisterUndo("Add Item Stat", db);
                    InvStat stat = new InvStat();
                    stat.id = InvStat.Identifier.Armor;
                    item.stats.Add(stat);
                }

                // Save all values
                if (!itemDesc.Equals(item.description) ||
                    slot != item.slot ||
                    go != item.attachment ||
                    color != item.color ||
                    min != item.minItemLevel ||
                    max != item.maxItemLevel ||
                    !iconName.Equals(item.iconName))
                {
                    NGUIEditorTools.RegisterUndo("Item Properties", db);
                    item.description  = itemDesc;
                    item.slot         = slot;
                    item.attachment   = go;
                    item.color        = color;
                    item.iconName     = iconName;
                    item.minItemLevel = min;
                    item.maxItemLevel = max;
                }
            }
        }
    }
Example #20
0
    public override void OnInspectorGUI()
    {
        RebasedEditorGUIUtility.LookLikeControls(80f);
        mList = target as UIPopupList;

        ComponentSelector.Draw <UIAtlas>(mList.atlas, OnSelectAtlas);
        ComponentSelector.Draw <UIFont>(mList.font, OnSelectFont);

        GUILayout.BeginHorizontal();
        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;
            }
        }
        GUILayout.Space(44f);
        GUILayout.EndHorizontal();

        if (mList.atlas != null)
        {
            NGUIEditorTools.SpriteField("Background", mList.atlas, mList.backgroundSprite, OnBackground);
            NGUIEditorTools.SpriteField("Highlight", mList.atlas, mList.highlightSprite, OnHighlight);

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

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

            GUILayout.Space(-14f);
            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;
            }

            UIPopupList.Position pos = (UIPopupList.Position)EditorGUILayout.EnumPopup("Position", mList.position);

            if (mList.position != pos)
            {
                RegisterUndo();
                mList.position = pos;
            }

            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;
            }

            RebasedEditorGUIUtility.LookLikeControls(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;
            }
        }
    }
Example #21
0
    /// <summary>
    /// Draw the UI for this tool.
    /// </summary>

    void OnGUI()
    {
        string prefabPath = "";
        string matPath    = "";

        if (NGUISettings.font != null && NGUISettings.font.name == NGUISettings.fontName)
        {
            prefabPath = AssetDatabase.GetAssetPath(NGUISettings.font.gameObject.GetInstanceID());
            if (NGUISettings.font.material != null)
            {
                matPath = AssetDatabase.GetAssetPath(NGUISettings.font.material.GetInstanceID());
            }
        }

        // Assume default values if needed
        if (string.IsNullOrEmpty(NGUISettings.fontName))
        {
            NGUISettings.fontName = "New Font";
        }
        if (string.IsNullOrEmpty(prefabPath))
        {
            prefabPath = NGUIEditorTools.GetSelectionFolder() + NGUISettings.fontName + ".prefab";
        }
        if (string.IsNullOrEmpty(matPath))
        {
            matPath = NGUIEditorTools.GetSelectionFolder() + NGUISettings.fontName + ".mat";
        }

        RebasedEditorGUIUtility.LookLikeControls(80f);
        NGUIEditorTools.DrawHeader("Input");

        GUILayout.BeginHorizontal();
        mType = (FontType)EditorGUILayout.EnumPopup("Type", mType);
        GUILayout.Space(18f);
        GUILayout.EndHorizontal();
        int create = 0;

        if (mType == FontType.Dynamic)
        {
            NGUISettings.dynamicFont = EditorGUILayout.ObjectField("Font TTF", NGUISettings.dynamicFont, typeof(Font), false) as Font;

            GUILayout.BeginHorizontal();
            NGUISettings.dynamicFontSize  = EditorGUILayout.IntField("Font Size", NGUISettings.dynamicFontSize, GUILayout.Width(120f));
            NGUISettings.dynamicFontStyle = (FontStyle)EditorGUILayout.EnumPopup(NGUISettings.dynamicFontStyle);
            GUILayout.Space(18f);
            GUILayout.EndHorizontal();

            if (NGUISettings.dynamicFont != null)
            {
                NGUIEditorTools.DrawHeader("Output");

                GUILayout.BeginHorizontal();
                GUILayout.Label("Font Name", GUILayout.Width(76f));
                GUI.backgroundColor   = Color.white;
                NGUISettings.fontName = GUILayout.TextField(NGUISettings.fontName);
                GUILayout.EndHorizontal();
            }
            NGUIEditorTools.DrawSeparator();

#if UNITY_3_5
            EditorGUILayout.HelpBox("Dynamic fonts require Unity 4.0 or higher.", MessageType.Error);
#else
            // Helpful info
            if (NGUISettings.dynamicFont == null)
            {
                EditorGUILayout.HelpBox("Dynamic font creation happens right in Unity. Simply specify the TrueType font to be used as source.", MessageType.Info);
            }
            EditorGUILayout.HelpBox("Please note that dynamic fonts can't be made a part of an atlas, and they will always be drawn in a separate draw call. You WILL need to adjust transform position's Z rather than depth!", MessageType.Warning);

            if (NGUISettings.dynamicFont != null)
            {
                NGUIEditorTools.DrawSeparator();

                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                GUI.backgroundColor = Color.green;

                GameObject go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;

                if (go != null)
                {
                    if (go.GetComponent <UIFont>() != null)
                    {
                        GUI.backgroundColor = Color.red;
                        if (GUILayout.Button("Replace the Font", GUILayout.Width(140f)))
                        {
                            create = 1;
                        }
                    }
                    else
                    {
                        GUI.backgroundColor = Color.grey;
                        GUILayout.Button("Rename Your Font", GUILayout.Width(140f));
                    }
                }
                else
                {
                    GUI.backgroundColor = Color.green;
                    if (GUILayout.Button("Create the Font", GUILayout.Width(140f)))
                    {
                        create = 1;
                    }
                }

                GUI.backgroundColor = Color.white;
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();
            }
#endif
        }
        else
        {
            NGUISettings.fontData    = EditorGUILayout.ObjectField("Font Data", NGUISettings.fontData, typeof(TextAsset), false) as TextAsset;
            NGUISettings.fontTexture = EditorGUILayout.ObjectField("Texture", NGUISettings.fontTexture, typeof(Texture2D), false) as Texture2D;

            // Draw the atlas selection only if we have the font data and texture specified, just to make it easier
            if (NGUISettings.fontData != null && NGUISettings.fontTexture != null)
            {
                NGUIEditorTools.DrawHeader("Output");

                GUILayout.BeginHorizontal();
                GUILayout.Label("Font Name", GUILayout.Width(76f));
                GUI.backgroundColor   = Color.white;
                NGUISettings.fontName = GUILayout.TextField(NGUISettings.fontName);
                GUILayout.EndHorizontal();

                ComponentSelector.Draw <UIFont>("Select", NGUISettings.font, OnSelectFont);
                ComponentSelector.Draw <UIAtlas>(NGUISettings.atlas, OnSelectAtlas);
            }
            NGUIEditorTools.DrawSeparator();

            // Helpful info
            if (NGUISettings.fontData == null)
            {
                EditorGUILayout.HelpBox("The bitmap font creation mostly takes place outside of Unity. You can use BMFont on " +
                                        "Windows or your choice of Glyph Designer or the less expensive bmGlyph on the Mac.\n\n" +
                                        "Either of these tools will create a FNT file for you that you will drag & drop into the field above.", MessageType.Info);
            }
            else if (NGUISettings.fontTexture == null)
            {
                EditorGUILayout.HelpBox("When exporting your font, you should get two files: the TXT, and the texture. Only one texture can be used per font.", MessageType.Info);
            }
            else if (NGUISettings.atlas == null)
            {
                EditorGUILayout.HelpBox("You can create a font that doesn't use a texture atlas. This will mean that the text " +
                                        "labels using this font will generate an extra draw call, and will need to be sorted by " +
                                        "adjusting the Z instead of the Depth.\n\nIf you do specify an atlas, the font's texture will be added to it automatically.", MessageType.Info);

                NGUIEditorTools.DrawSeparator();

                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                GUI.backgroundColor = Color.red;
                if (GUILayout.Button("Create a Font without an Atlas", GUILayout.Width(200f)))
                {
                    create = 2;
                }
                GUI.backgroundColor = Color.white;
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();
            }
            else
            {
                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();

                GameObject go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;

                if (go != null)
                {
                    if (go.GetComponent <UIFont>() != null)
                    {
                        GUI.backgroundColor = Color.red;
                        if (GUILayout.Button("Replace the Font", GUILayout.Width(140f)))
                        {
                            create = 3;
                        }
                    }
                    else
                    {
                        GUI.backgroundColor = Color.grey;
                        GUILayout.Button("Rename Your Font", GUILayout.Width(140f));
                    }
                }
                else
                {
                    GUI.backgroundColor = Color.green;
                    if (GUILayout.Button("Create the Font", GUILayout.Width(140f)))
                    {
                        create = 3;
                    }
                }
                GUI.backgroundColor = Color.white;
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();
            }
        }

        if (create != 0)
        {
            GameObject go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;

            if (go == null || EditorUtility.DisplayDialog("Are you sure?", "Are you sure you want to replace the contents of the " +
                                                          NGUISettings.fontName + " font with the currently selected values? This action can't be undone.", "Yes", "No"))
            {
                // Try to load the material
                Material mat = null;

                // Non-atlased font
                if (create == 2)
                {
                    mat = AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)) as Material;

                    // If the material doesn't exist, create it
                    if (mat == null)
                    {
                        Shader shader = Shader.Find("Unlit/Transparent Colored");
                        mat = new Material(shader);

                        // Save the material
                        AssetDatabase.CreateAsset(mat, matPath);
                        AssetDatabase.Refresh();

                        // Load the material so it's usable
                        mat = AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)) as Material;
                    }
                    mat.mainTexture = NGUISettings.fontTexture;
                }
                else if (create != 1)
                {
                    UIAtlasMaker.AddOrUpdate(NGUISettings.atlas, NGUISettings.fontTexture);
                }

                // Font doesn't exist yet
                if (go == null || go.GetComponent <UIFont>() == null)
                {
                    // Create a new prefab for the atlas
                    Object prefab = PrefabUtility.CreateEmptyPrefab(prefabPath);

                    // Create a new game object for the font
                    go = new GameObject(NGUISettings.fontName);
                    NGUISettings.font = go.AddComponent <UIFont>();
                    CreateFont(NGUISettings.font, create, mat);

                    // Update the prefab
                    PrefabUtility.ReplacePrefab(go, prefab);
                    DestroyImmediate(go);
                    AssetDatabase.Refresh();

                    // Select the atlas
                    go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;
                    NGUISettings.font = go.GetComponent <UIFont>();
                }
                else
                {
                    NGUISettings.font = go.GetComponent <UIFont>();
                    CreateFont(NGUISettings.font, create, mat);
                }
                MarkAsChanged();
            }
        }
    }