Пример #1
0
    /// <summary>
    /// Helper function used to print things in columns.
    /// </summary>

    bool DrawRow(Entry ent, UIPanel selected, bool isChecked)
    {
        bool   retVal = false;
        string panelName, layer, widgetCount, drawCalls, clipping;

        if (ent != null)
        {
            panelName   = ent.panel.name;
            layer       = LayerMask.LayerToName(ent.panel.gameObject.layer);
            widgetCount = ent.widgets.Count.ToString();
            drawCalls   = ent.panel.drawCalls.Count.ToString();
            clipping    = (ent.panel.clipping != UIDrawCall.Clipping.None) ? "Yes" : "";
        }
        else
        {
            panelName   = "Panel's Name";
            layer       = "Layer";
            widgetCount = "WG";
            drawCalls   = "DC";
            clipping    = "Clip";
        }

        if (ent != null)
        {
            NGUIEditorTools.HighlightLine(ent.isEnabled ? new Color(0.6f, 0.6f, 0.6f) : Color.black);
        }

        GUILayout.BeginHorizontal();
        {
            GUI.color = Color.white;

            if (isChecked != EditorGUILayout.Toggle(isChecked, GUILayout.Width(20f)))
            {
                retVal = true;
            }

            if (ent == null)
            {
                GUI.contentColor = Color.white;
            }
            else if (ent.isEnabled)
            {
                GUI.contentColor = (ent.panel == selected) ? new Color(0f, 0.8f, 1f) : Color.white;
            }
            else
            {
                GUI.contentColor = (ent.panel == selected) ? new Color(0f, 0.5f, 0.8f) : Color.grey;
            }

#if UNITY_3_4
            if (GUILayout.Button(panelName, EditorStyles.structHeadingLabel, GUILayout.MinWidth(100f)))
#else
            if (GUILayout.Button(panelName, EditorStyles.label, GUILayout.MinWidth(100f)))
#endif
            {
                if (ent != null)
                {
                    Selection.activeGameObject = ent.panel.gameObject;
                    EditorUtility.SetDirty(ent.panel.gameObject);
                }
            }

            GUILayout.Label(layer, GUILayout.Width(ent == null ? 65f : 70f));
            GUILayout.Label(widgetCount, GUILayout.Width(30f));
            GUILayout.Label(drawCalls, GUILayout.Width(30f));
            GUILayout.Label(clipping, GUILayout.Width(30f));

            if (ent == null)
            {
                GUILayout.Label("Giz", GUILayout.Width(24f));
            }
            else
            {
                GUI.contentColor = ent.isEnabled ? Color.white : new Color(0.7f, 0.7f, 0.7f);
                bool debug = (ent.panel.debugInfo == UIPanel.DebugInfo.Gizmos);

                if (debug != EditorGUILayout.Toggle(debug, GUILayout.Width(20f)))
                {
                    // debug != value, so it's currently inverse
                    ent.panel.debugInfo = debug ? UIPanel.DebugInfo.None : UIPanel.DebugInfo.Gizmos;
                    EditorUtility.SetDirty(ent.panel.gameObject);
                }
            }
        }
        GUILayout.EndHorizontal();
        return(retVal);
    }
Пример #2
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>();
        }

        EditorGUIUtility.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("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
#if UNITY_3_4
                    Object prefab = (go != null) ? go : EditorUtility.CreateEmptyPrefab(prefabPath);
#else
                    Object prefab = (go != null) ? go : PrefabUtility.CreateEmptyPrefab(prefabPath);
#endif
                    // Create a new game object for the atlas
                    go = new GameObject(NGUISettings.atlasName);
                    go.AddComponent <UIAtlas>().spriteMaterial = mat;

                    // Update the prefab
#if UNITY_3_4
                    EditorUtility.ReplacePrefab(go, prefab);
#else
                    PrefabUtility.ReplacePrefab(go, prefab);
#endif
                    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>("...or 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("in 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.unityPacking = EditorGUILayout.Toggle("Unity Packer", NGUISettings.unityPacking, GUILayout.MinWidth(100f));
            GUILayout.Label("if off, use a custom packer");
            GUILayout.EndHorizontal();

            if (textures.Count > 0)
            {
                GUI.backgroundColor = Color.green;
                update = GUILayout.Button("Add/Update All");
                GUI.backgroundColor = Color.white;
            }
            else
            {
                NGUIEditorTools.DrawSeparator();
                GUILayout.Label("You can reveal more options by selecting\none or more textures in the Project View\nwindow.");
            }
        }
        else
        {
            NGUIEditorTools.DrawSeparator();
            GUILayout.Label("You can create a new atlas by selecting\none or more textures in the Project View\nwindow, then clicking \"Create\".");
        }

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

        if (spriteList.Count > 0)
        {
            NGUIEditorTools.DrawHeader("Sprites");
            GUILayout.Space(-7f);

            mScroll = GUILayout.BeginScrollView(mScroll);

            string delSprite = null;
            int    index     = 0;
            foreach (KeyValuePair <string, int> iter in spriteList)
            {
                ++index;
                NGUIEditorTools.HighlightLine(new Color(0.6f, 0.6f, 0.6f));
                GUILayout.BeginHorizontal();
                GUILayout.Label(index.ToString(), GUILayout.Width(24f));
                GUILayout.Label(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 (string.IsNullOrEmpty(mDelName) || mDelName != iter.Key)
                    {
                        // If we have not yet selected a sprite for deletion, show a small "X" button
                        if (GUILayout.Button("X", GUILayout.Width(22f)))
                        {
                            mDelName = iter.Key;
                        }
                    }
                    else
                    {
                        GUI.backgroundColor = Color.red;

                        if (GUILayout.Button("Delete", GUILayout.Width(60f)))
                        {
                            // Confirmation button clicked on -- delete this sprite
                            delSprite = iter.Key;
                            mDelName  = null;
                        }
                        GUI.backgroundColor = Color.white;
                    }
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndScrollView();

            // If this sprite was marked for deletion, remove it from the atlas
            if (!string.IsNullOrEmpty(delSprite))
            {
                List <UIAtlas.Sprite> list = NGUISettings.atlas.spriteList;

                foreach (UIAtlas.Sprite sp in list)
                {
                    if (sp.name == delSprite)
                    {
                        list.Remove(sp);
                        List <SpriteEntry> sprites = new List <SpriteEntry>();
                        ExtractSprites(NGUISettings.atlas, sprites);
                        UpdateAtlas(NGUISettings.atlas, sprites);
                        mDelName = null;
                        return;
                    }
                }
            }
            else if (update)
            {
                UpdateAtlas(textures, true);
            }
            else if (replace)
            {
                UpdateAtlas(textures, false);
            }
            return;
        }
    }
Пример #3
0
    /// <summary>
    /// Helper function used to print things in columns.
    /// </summary>

    void DrawRow(Camera cam)
    {
                #pragma warning disable 0618
        if (cam != null)
        {
            NGUIEditorTools.HighlightLine(new Color(0.6f, 0.6f, 0.6f));
        }

        GUILayout.BeginHorizontal();
        {
            bool enabled = (cam == null || (cam.gameObject.active && cam.enabled));

            GUI.color = Color.white;

            if (cam != null)
            {
                if (enabled != EditorGUILayout.Toggle(enabled, GUILayout.Width(20f)))
                {
                    cam.enabled = !enabled;
                    EditorUtility.SetDirty(cam.gameObject);
                }
            }
            else
            {
                GUILayout.Space(30f);
            }

            bool highlight = (cam == null || Selection.activeGameObject == null) ? false :
                             (0 != (cam.cullingMask & (1 << Selection.activeGameObject.layer)));

            if (enabled)
            {
                GUI.color = highlight ? new Color(0f, 0.8f, 1f) : Color.white;
            }
            else
            {
                GUI.color = highlight ? new Color(0f, 0.5f, 0.8f) : Color.grey;
            }

            string camName, camLayer;

            if (cam == null)
            {
                camName  = "Camera's Name";
                camLayer = "Layer";
            }
            else
            {
                camName  = cam.name + (cam.orthographic ? " (2D)" : " (3D)");
                camLayer = LayerMask.LayerToName(cam.gameObject.layer);
            }

#if UNITY_3_4
            if (GUILayout.Button(camName, EditorStyles.structHeadingLabel, GUILayout.MinWidth(100f)) && cam != null)
#else
            if (GUILayout.Button(camName, EditorStyles.label, GUILayout.MinWidth(100f)) && cam != null)
#endif
            {
                Selection.activeGameObject = cam.gameObject;
                EditorUtility.SetDirty(cam.gameObject);
            }
            GUILayout.Label(camLayer, GUILayout.Width(70f));

            GUI.color = enabled ? Color.white : new Color(0.7f, 0.7f, 0.7f);

            if (cam == null)
            {
                GUILayout.Label("EV", GUILayout.Width(26f));
            }
            else
            {
                UICamera uic = cam.GetComponent <UICamera>();
                bool     ev  = (uic != null && uic.enabled);

                if (ev != EditorGUILayout.Toggle(ev, GUILayout.Width(20f)))
                {
                    if (uic == null)
                    {
                        uic = cam.gameObject.AddComponent <UICamera>();
                    }
                    uic.enabled = !ev;
                }
            }

            if (cam == null)
            {
                GUILayout.Label("Mask", GUILayout.Width(100f));
            }
            else
            {
                int mask = LayerMaskField(cam.cullingMask, GUILayout.Width(105f));

                if (cam.cullingMask != mask)
                {
                    NGUIEditorTools.RegisterUndo("Camera Mask Change", cam);
                    cam.cullingMask = mask;
                }
            }
        }
        GUILayout.EndHorizontal();
                #pragma warning restore 0618
    }