Beispiel #1
0
    public void OnGUI()
    {
        if (_needsLoadData)
        {
            _needsLoadData = false;
            LoadData();
        }

        int linkToMoveIndex = -1;
        int moveDelta       = 0;

        _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos);

        if (_activeGenerator != null)
        {
            GUILayout.Label("Building " + Path.GetFileNameWithoutExtension(_activeGenerator.link.atlasFilePath), EditorStyles.boldLabel);
            //GUILayout.BeginHorizontal();
            GUILayout.Label(_progressMessage);

            GUILayout.Space(10.0f);
        }
        else
        {
            GUILayout.BeginHorizontal();
            GUILayout.Label("Packrat", EditorStyles.boldLabel);
            GUILayout.FlexibleSpace();
            GUI.backgroundColor = new Color(0.8f, 1.0f, 0.8f);

            if (_atlasLinks.Count > 0)
            {
                GUILayout.BeginVertical();

                GUILayout.Space(5.0f);

                if (GUILayout.Button("Generate Selected"))
                {
                    for (int n = 0; n < _atlasLinks.Count; n++)
                    {
                        PRAtlasLink link = _atlasLinks[n];
                        if (link.shouldGenerate)
                        {
                            if (!_atlasLinksToGenerate.Contains(link))
                            {
                                _atlasLinksToGenerate.Add(link);
                            }
                        }
                    }
                }
                GUILayout.EndVertical();
            }

            GUI.backgroundColor = Color.white;
            GUILayout.EndHorizontal();
            GUILayout.Space(15.0f);
        }


        GUI.color = Color.white.CloneWithNewAlpha(0.6f);
        GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
        GUI.color = Color.white;

        for (int n = 0; n < _atlasLinks.Count; n++)
        {
            PRAtlasLink link     = _atlasLinks[n];
            string      linkName = Path.GetFileNameWithoutExtension(link.atlasFilePath);

            EditorGUILayout.BeginHorizontal();
            link.shouldFoldout = EditorGUILayout.Foldout(link.shouldFoldout, linkName);
            GUILayout.FlexibleSpace();
            if (link.shouldFoldout)
            {
                GUI.backgroundColor = new Color(0.8f, 0.8f, 0.8f);
                EditorGUI.BeginDisabledGroup(n == 0);
                if (GUILayout.Button("\u25B3"))                //up (u25B2 is bigger)
                {
                    linkToMoveIndex = n;
                    moveDelta       = -1;
                }
                EditorGUI.EndDisabledGroup();

                EditorGUI.BeginDisabledGroup(n == _atlasLinks.Count - 1);
                if (GUILayout.Button("\u25BD"))                //down (u25BC is bigger)
                {
                    linkToMoveIndex = n;
                    moveDelta       = 1;
                }
                EditorGUI.EndDisabledGroup();
                GUI.backgroundColor = Color.white;
            }

            GUI.backgroundColor = new Color(0.6f, 0.8f, 0.6f);
            bool shouldGenerate = GUILayout.Toggle(link.shouldGenerate, "");

            if (link.shouldGenerate != shouldGenerate)
            {
                link.shouldGenerate = shouldGenerate;
                _hasDataChanged     = true;
            }

            GUILayout.Space(-5.0f);
            GUI.backgroundColor = Color.white;

            GUI.backgroundColor = new Color(0.8f, 1.0f, 0.8f);
            if (GUILayout.Button("Generate"))
            {
                if (!_atlasLinksToGenerate.Contains(link))
                {
                    _atlasLinksToGenerate.Add(link);
                }
            }
            GUI.backgroundColor = Color.white;

            EditorGUILayout.EndHorizontal();
            if (!link.shouldFoldout)
            {
                GUI.color = Color.white.CloneWithNewAlpha(0.6f);
                GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));                 //divider
                GUI.color = Color.white;
                continue;
            }

            EditorGUILayout.BeginVertical();

            GUILayout.Space(10.0f);

            GUILayout.BeginHorizontal();
            EditorGUILayout.SelectableLabel("" + Path.GetFileName(link.sourceFolderPath));
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Change Source"))
            {
                string sourceFolderPath = EditorUtility.OpenFolderPanel("Find source images folder", "Assets", "");

                if (sourceFolderPath.Length != 0)
                {
                    link.sourceFolderPath = PRUtils.GetTrueProjectRelativePath(sourceFolderPath);

                    _hasDataChanged = true;
                }
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            EditorGUILayout.SelectableLabel("" + Path.GetFileName(link.atlasFilePath));
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Change Atlas"))
            {
                string atlasFilePath = EditorUtility.SaveFilePanel("Choose output atlas location (in /Resources)", link.atlasFilePath, Path.GetFileName(link.atlasFilePath) + ".txt", "txt");

                if (atlasFilePath.Length != 0)
                {
                    atlasFilePath = PRUtils.GetTrueProjectRelativePath(atlasFilePath);

                    link.atlasFilePath = Path.GetDirectoryName(atlasFilePath) + "/" + Path.GetFileNameWithoutExtension(atlasFilePath);

                    _hasDataChanged = true;
                }
            }
            GUILayout.EndHorizontal();

//			bool shouldAutoGenerate = EditorGUILayout.Toggle("Auto Generate", link.shouldAutoGenerate);
//			if(link.shouldAutoGenerate != shouldAutoGenerate)
//			{
//				link.shouldAutoGenerate = shouldAutoGenerate;
//				_hasDataChanged = true;
//			}

            EditorGUI.BeginChangeCheck();

            link.shouldAddSubfolders = EditorGUILayout.Toggle(new GUIContent("Include subfolders?", "Note:\nSubfolder names will be used in element names"), link.shouldAddSubfolders);

            link.shouldUseBytes = EditorGUILayout.Toggle(new GUIContent("Export as .bytes?", "Stores the image as png bytedata for reduced file size"), link.shouldUseBytes);

            link.shouldTrim = EditorGUILayout.Toggle(new GUIContent("Trim sprites?", "Remove alpha from edges of the sprite"), link.shouldTrim);

            if (link.shouldTrim)
            {
                bool shouldPadTrim = EditorGUILayout.Toggle(new GUIContent("\tShould pad trim?", "Prevent trimming to the very edge"), link.trimPadding != 0);

                if (shouldPadTrim)
                {
                    if (link.trimPadding == 0)
                    {
                        link.trimPadding = 1;
                    }
                }
                else
                {
                    link.trimPadding = 0;
                }

                //link.trimPadding = EditorGUILayout.IntSlider("Trim padding",link.trimPadding,0,8);
            }

            //link.scale = Mathf.Max(0.0f, Mathf.Min(10.0f,EditorGUILayout.FloatField("Scale",link.scale)));
            float precision = 0.025f;
            link.scale = Mathf.Round(EditorGUILayout.Slider(new GUIContent("Scale", "Shrink the contents of the atlas"), link.scale, precision, 1.0f) / precision) * precision;


            //link.padding = Mathf.Max(0, Mathf.Min(16,EditorGUILayout.IntField("Padding",link.padding)));

            link.padding = EditorGUILayout.IntSlider(new GUIContent("Padding", "Add spacing between elements"), link.padding, 0, 8);

            link.extrude = EditorGUILayout.IntSlider(new GUIContent("Extrude", "Duplicate pixels on the edges"), link.extrude, 0, 8);

            if (EditorGUI.EndChangeCheck())
            {
                _hasDataChanged = true;
            }

            GUILayout.Space(15.0f);

            GUILayout.BeginHorizontal();

            GUI.backgroundColor = new Color(1.0f, 1.0f, 0.9f);
            if (GUILayout.Button("View Atlas"))
            {
                PRViewAtlasWindow.Show(link);
            }
            GUI.backgroundColor = Color.white;

            GUILayout.FlexibleSpace();
            GUI.backgroundColor = new Color(0.8f, 0.8f, 1.0f);
            if (GUILayout.Button("Duplicate Atlas"))
            {
                _atlasLinks.Insert(n + 1, link.GetDuplicate());
            }
            GUI.backgroundColor = Color.white;

            GUI.backgroundColor = new Color(1.0f, 0.8f, 0.8f);
            if (GUILayout.Button("Delete Atlas"))
            {
                bool didConfirmDelete = EditorUtility.DisplayDialog("Delete atlas?", "Are you sure you want to delete " + linkName + "?", "Delete", "Cancel");
                if (didConfirmDelete)
                {
                    _atlasLinks.RemoveAt(n);
                    _hasDataChanged = true;
                    n--;
                }
            }
            GUI.backgroundColor = Color.white;
            GUILayout.EndHorizontal();


            GUILayout.Space(20.0f);

            EditorGUILayout.EndVertical();

            GUI.color = Color.white.CloneWithNewAlpha(0.6f);
            GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
            GUI.color = Color.white;
        }

        GUILayout.Space(20.0f);

        if (GUILayout.Button("Add Atlas"))
        {
            string sourceFolderPath = EditorUtility.OpenFolderPanel("Find source images folder", "/Assets", "");

            if (sourceFolderPath.Length != 0)
            {
                string sourceFolderName = Path.GetFileName(sourceFolderPath);

                sourceFolderPath = PRUtils.GetTrueProjectRelativePath(sourceFolderPath);

                string atlasFilePath = EditorUtility.SaveFilePanel("Choose result atlas location (in /Resources)", sourceFolderName + ".txt", sourceFolderName, "txt");

                if (atlasFilePath.Length != 0)
                {
                    atlasFilePath = PRUtils.GetTrueProjectRelativePath(atlasFilePath);

                    //remove the extension
                    atlasFilePath = Path.GetDirectoryName(atlasFilePath) + "/" + Path.GetFileNameWithoutExtension(atlasFilePath);

                    _atlasLinks.Add(new PRAtlasLink(sourceFolderPath, atlasFilePath));
                    _hasDataChanged = true;
                }
            }
        }

        GUILayout.Space(20.0f);

        GUI.contentColor = new Color(0.6f, 0.6f, 0.6f);
//		GUILayout.Label("Reminder: set the atlas to transparent,");
//		GUILayout.Space(-5.0f);
//		GUILayout.Label("with a max size of 4096");
        GUI.skin.label.wordWrap = true;
        GUILayout.Label("Tip: set the atlas to transparent with a max size of 4096");
        GUI.contentColor = Color.white;

        //TODO: Fix Atlas Image Settings (set transparent and 4096)

        EditorGUILayout.EndScrollView();

        if (linkToMoveIndex != -1)
        {
            if (moveDelta == 1)            //if we're moving down, just move the link below up instead :)
            {
                linkToMoveIndex++;
            }

            PRAtlasLink link = _atlasLinks[linkToMoveIndex];

            _atlasLinks.RemoveAt(linkToMoveIndex);
            _atlasLinks.Insert(linkToMoveIndex - 1, link);

            _hasDataChanged = true;
        }

        if (_hasDataChanged)
        {
            _hasDataChanged = false;
            SaveData();
        }

        GenerateAtlases();
    }
Beispiel #2
0
    //Unity's FileUtil.GetProjectRelativePath() only works for files INSIDE the project,
    //but this method works for ALL files+folders
    public static string GetTrueProjectRelativePath(string inputPath)
    {
        string fullProjectPath = Directory.GetParent(Path.GetFullPath(Application.dataPath)).FullName;

        return(PRUtils.GetRelativePath(fullProjectPath, Path.GetFullPath(inputPath)));
    }