UIAtlasMaker.OnGUI C# (CSharp) Method

OnGUI() private method

Draw the UI for this tool.
private OnGUI ( ) : void
return void
    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>("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.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 (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);

            bool delete = false;
            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 (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();

            // 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.tex.name))
                    {
                        sprites.RemoveAt(i);
                    }
                }
                UpdateAtlas(NGUISettings.atlas, sprites);
                mDelNames.Clear();
            }
            else if (update) UpdateAtlas(textures, true);
            else if (replace) UpdateAtlas(textures, false);
            return;
        }
    }