示例#1
0
        public static void Show(PatternTexture patternTexture, string selection, Action <string> onSelectAction, Action onCloseAction)
        {
            if (instance != null)
            {
                instance.Close();
                instance = null;
            }

            var comp = DisplayWizard <PatternSpriteSelector>("Select Sprite");

            comp.onSelectAction = onSelectAction;
            comp.onCloseAction  = onCloseAction;

            comp.patternTexture       = patternTexture;
            comp.selectionTextureName = selection;
            comp.preViewSize          = 200f;

            comp.sourceTextures = patternTexture.GetAllPatternData()
                                  .Select(x =>
            {
                var path = AssetDatabase.GUIDToAssetPath(x.Guid);
                return(AssetDatabase.LoadMainAssetAtPath(path) as Texture2D);
            })
                                  .Where(x => x != null)
                                  .ToArray();
        }
示例#2
0
        private void CalcPerformance(PatternTexture patternTexture)
        {
            calcPerformance = false;

            if (patternTexture != null)
            {
                var assetPath = AssetDatabase.GetAssetPath(patternTexture);
                var fullPath  = UnityPathUtility.ConvertAssetPathToFullPath(assetPath);

                var fileInfo = new FileInfo(fullPath);

                infoFileSize = (float)fileInfo.Length / MB;
            }
            else
            {
                return;
            }

            var textures = patternTexture.GetAllPatternData()
                           .Select(x => AssetDatabase.GUIDToAssetPath(x.Guid))
                           .Select(x => AssetDatabase.LoadMainAssetAtPath(x) as Texture2D)
                           .Where(x => x != null)
                           .ToArray();

            // 消費メモリサイズを計測.
            totalMemSize = 0;
            textures.ForEach(x =>
            {
                var mem       = Mathf.NextPowerOfTwo(x.width) * Mathf.NextPowerOfTwo(x.height);
                mem          *= !x.alphaIsTransparency ? 3 : 4;
                totalMemSize += mem;
            });
            totalMemSize /= MB;

            if (patternTexture.Texture != null)
            {
                var mem = Mathf.NextPowerOfTwo(patternTexture.Texture.width) * Mathf.NextPowerOfTwo(patternTexture.Texture.height);
                mem *= !patternTexture.Texture.alphaIsTransparency ? 3 : 4;
                totalAtlasMemSize = (float)mem / MB;
            }

            // ファイルサイズ.
            totalFileSize = 0f;
            textures.Select(x => AssetDatabase.GetAssetPath(x))
            .Select(x => UnityPathUtility.ConvertAssetPathToFullPath(x))
            .Select(x => new FileInfo(x))
            .ForEach(x => totalFileSize += (float)x.Length / MB);

            if (patternTexture.Texture != null)
            {
                var assetPath = AssetDatabase.GetAssetPath(patternTexture.Texture);
                var fullPath  = UnityPathUtility.ConvertAssetPathToFullPath(assetPath);

                var fileInfo = new FileInfo(fullPath);

                atlasFileSize = (float)fileInfo.Length / MB;
            }

            calcPerformance = true;
        }
示例#3
0
        void OnGUI()
        {
            var selectionTextures = Selection.objects != null?Selection.objects.OfType <Texture2D>().ToArray() : null;

            GUILayout.Space(2f);

            EditorGUI.BeginChangeCheck();

            selectPatternTexture = EditorLayoutTools.ObjectField(selectPatternTexture, false);

            if (EditorGUI.EndChangeCheck())
            {
                if (selectPatternTexture != null)
                {
                    blockSize = selectPatternTexture.BlockSize;
                    padding   = selectPatternTexture.Padding;

                    Selection.objects = new UnityEngine.Object[0];

                    BuildTextureInfos(null);
                }
                else
                {
                    blockSize = DefaultBlockSize;
                    padding   = DefaultPadding;
                }

                deleteNames.Clear();
            }

            GUILayout.Space(5f);

            DrawSettingsGUI();

            GUILayout.Space(5f);

            if (selectPatternTexture == null)
            {
                DrawCreateGUI(selectionTextures);
            }
            else
            {
                DrawUpdateGUI(selectPatternTexture);
            }
        }
示例#4
0
        private void GeneratePatternTexture(PatternTexture patternTexture)
        {
            if (textureInfos == null)
            {
                Debug.LogError("Require select texture.");
                return;
            }

            var exportPath = string.Empty;

            var textures = textureInfos
                           .Where(x => x.status == TextureStatus.Exist ||
                                  x.status == TextureStatus.Add ||
                                  x.status == TextureStatus.Update)
                           .Where(x => !deleteNames.Contains(x.texture.name))
                           .Select(x => x.texture)
                           .ToArray();

            if (patternTexture == null)
            {
                var path = string.Empty;

                var savedExportPath = Prefs.exportPath;

                if (string.IsNullOrEmpty(savedExportPath) || !File.Exists(savedExportPath))
                {
                    path = UnityPathUtility.AssetsFolder;
                }
                else
                {
                    var directory = Directory.GetParent(savedExportPath);

                    path = directory.FullName;
                }

                exportPath = EditorUtility.SaveFilePanelInProject("Save As", "New PatternTexture.asset", "asset", "Save as...", path);
            }
            else
            {
                exportPath = AssetDatabase.GetAssetPath(patternTexture);
            }

            if (!string.IsNullOrEmpty(exportPath))
            {
                patternTexture = ScriptableObjectGenerator.Generate <PatternTexture>(exportPath);

                var patternData = generator.Generate(exportPath, blockSize, padding, textures, hasAlphaMap);

                patternTexture.Set(patternData.Texture, blockSize, padding, patternData.PatternData, patternData.PatternBlocks, hasAlphaMap);
                patternTexture.Texture.filterMode = filterMode;

                UnityEditorUtility.SaveAsset(patternTexture);

                Prefs.exportPath = exportPath;

                selectPatternTexture = patternTexture;

                var selectionTextures = GetSelectionTextures();

                BuildTextureInfos(selectionTextures);

                deleteNames.Clear();

                Repaint();
            }
        }
示例#5
0
        private void DrawUpdateGUI(PatternTexture patternTexture)
        {
            var labelStyle = new GUIStyle(EditorStyles.label);

            var defaultColor           = labelStyle.normal.textColor;
            var defaultBackgroundColor = GUI.backgroundColor;

            var delete = false;

            if (textureInfos.Any())
            {
                EditorLayoutTools.Title("Sprites", EditorLayoutTools.BackgroundColor, EditorLayoutTools.LabelColor);

                EditorGUILayout.Separator();

                using (new EditorGUILayout.VerticalScope())
                {
                    using (var scrollViewScope = new EditorGUILayout.ScrollViewScope(scrollPosition))
                    {
                        int index = 0;

                        for (var i = 0; i < textureInfos.Length; i++)
                        {
                            ++index;

                            GUILayout.Space(-1f);

                            var textureName = textureInfos[i].texture != null ? textureInfos[i].texture.name : null;

                            var highlight = selectionTextureName == textureName;

                            GUI.backgroundColor = highlight ? Color.white : new Color(0.8f, 0.8f, 0.8f);

                            using (new EditorGUILayout.HorizontalScope(EditorStyles.textArea, GUILayout.MinHeight(20f)))
                            {
                                GUI.backgroundColor = Color.white;
                                GUILayout.Label(index.ToString(), GUILayout.Width(24f));

                                if (GUILayout.Button(textureName, EditorStyles.label, GUILayout.Height(20f)))
                                {
                                    selectionTextureName = textureName;
                                }

                                switch (textureInfos[i].status)
                                {
                                case TextureStatus.Add:
                                    labelStyle.normal.textColor = Color.green;
                                    GUILayout.Label("Add", labelStyle, GUILayout.Width(27f));
                                    break;

                                case TextureStatus.Update:
                                    labelStyle.normal.textColor = Color.cyan;
                                    GUILayout.Label("Update", labelStyle, GUILayout.Width(45f));
                                    break;

                                case TextureStatus.Missing:
                                    labelStyle.normal.textColor = Color.yellow;
                                    GUILayout.Label("Missing", labelStyle, GUILayout.Width(45f));
                                    break;
                                }

                                labelStyle.normal.textColor = defaultColor;

                                if (deleteNames.Contains(textureName))
                                {
                                    GUI.backgroundColor = Color.red;

                                    if (GUILayout.Button("Delete", GUILayout.Width(60f)))
                                    {
                                        delete = true;
                                    }

                                    GUI.backgroundColor = Color.green;

                                    if (GUILayout.Button("X", GUILayout.Width(22f)))
                                    {
                                        deleteNames.Remove(textureName);
                                    }
                                }
                                else
                                {
                                    if (GUILayout.Button("X", GUILayout.Width(22f)))
                                    {
                                        if (!deleteNames.Contains(textureName))
                                        {
                                            deleteNames.Add(textureName);
                                        }
                                    }
                                }

                                GUILayout.Space(5f);
                            }
                        }

                        scrollPosition = scrollViewScope.scrollPosition;
                    }
                }
            }

            if (calcPerformance)
            {
                GUILayout.Space(5f);

                EditorLayoutTools.Title("Result", EditorLayoutTools.BackgroundColor, EditorLayoutTools.LabelColor);

                using (new EditorGUILayout.HorizontalScope())
                {
                    GUILayout.Label("MemorySize", GUILayout.Width(75f));

                    var memSize = infoFileSize + totalAtlasMemSize;

                    labelStyle.normal.textColor = totalMemSize < memSize ? Color.red : defaultColor;
                    GUILayout.Label(string.Format("{0:F1} MB >>> {1:F1} MB : {2:F1}% ", totalMemSize, memSize, 100.0f * memSize / totalMemSize), labelStyle);
                    labelStyle.normal.textColor = defaultColor;
                }

                using (new EditorGUILayout.HorizontalScope())
                {
                    GUILayout.Label("FileSize", GUILayout.Width(75f));

                    var fileSize = infoFileSize + atlasFileSize;

                    labelStyle.normal.textColor = totalFileSize < atlasFileSize ? Color.red : defaultColor;
                    GUILayout.Label(string.Format("{0:F1} MB >>> {1:F1} MB : {2:F1}% ", totalFileSize, fileSize, 100.0f * fileSize / totalFileSize), labelStyle);
                    labelStyle.normal.textColor = defaultColor;
                }
            }

            GUILayout.Space(15f);

            using (new EditorGUILayout.HorizontalScope())
            {
                GUI.backgroundColor = Color.cyan;

                GUILayout.FlexibleSpace();

                if (GUILayout.Button("Generate", GUILayout.Width(150f)))
                {
                    GeneratePatternTexture(patternTexture);
                }

                GUI.backgroundColor = defaultBackgroundColor;

                GUILayout.Space(25f);

                if (GUILayout.Button("View Textures", GUILayout.Width(150f)))
                {
                    Action <string> onSelection = x =>
                    {
                        selectionTextureName = x;
                        Repaint();
                    };

                    PatternSpriteSelector.Show(patternTexture, selectionTextureName, onSelection, null);
                }

                GUILayout.FlexibleSpace();
            }

            if (delete)
            {
                GeneratePatternTexture(patternTexture);
            }

            GUILayout.Space(5f);
        }