コード例 #1
0
        private void CalcPerformance(DicingTexture dicingTexture)
        {
            calcPerformance = false;

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

                var fileInfo = new FileInfo(fullPath);

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

            var textures = dicingTexture.GetAllDicingSource()
                           .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 (dicingTexture.Texture != null)
            {
                var mem = Mathf.NextPowerOfTwo(dicingTexture.Texture.width) * Mathf.NextPowerOfTwo(dicingTexture.Texture.height);
                mem *= !dicingTexture.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 (dicingTexture.Texture != null)
            {
                var assetPath = AssetDatabase.GetAssetPath(dicingTexture.Texture);
                var fullPath  = UnityPathUtility.ConvertAssetPathToFullPath(assetPath);

                var fileInfo = new FileInfo(fullPath);

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

            calcPerformance = true;
        }
コード例 #2
0
        public static void Show(DicingTexture dicingTexture, string selection, Action <string> onSelectAction, Action onCloseAction)
        {
            if (instance != null)
            {
                instance.Close();
                instance = null;
            }

            var comp = DisplayWizard <DicingSpriteSelector>("Select DicingSprite");

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

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

            comp.sourceTextures = dicingTexture.GetAllDicingSource()
                                  .Select(x =>
            {
                var path = AssetDatabase.GUIDToAssetPath(x.guid);
                return(AssetDatabase.LoadMainAssetAtPath(path) as Texture2D);
            })
                                  .Where(x => x != null)
                                  .ToArray();
        }
コード例 #3
0
        private void GenerateDicingTexture(DicingTexture target)
        {
            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 (target == null)
            {
                var path = string.Empty;

                if (string.IsNullOrEmpty(Prefs.exportPath) || !Directory.Exists(path))
                {
                    path = UnityPathUtility.AssetsFolder;
                }
                else
                {
                    path = Prefs.exportPath;
                }

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

            if (!string.IsNullOrEmpty(exportPath))
            {
                var dicingData = generator.Generate(exportPath, blockSize, padding, textures, hasAlphaMap);

                target = ScriptableObjectGenerator.Generate <DicingTexture>(exportPath);

                target.Set(dicingData.Texture, blockSize, padding, dicingData.SourceTextures, dicingData.DicingBlocks, hasAlphaMap);
                target.Texture.filterMode = filterMode;

                UnityEditorUtility.SaveAsset(target);

                Prefs.exportPath = exportPath;

                selectDicingTexture = target;

                BuildTextureInfos();

                deleteNames.Clear();

                Repaint();
            }
        }
コード例 #4
0
        void OnGUI()
        {
            var selectionTextures = Selection.objects != null?Selection.objects.OfType <Texture2D>().ToArray() : null;

            GUILayout.Space(2f);

            EditorGUI.BeginChangeCheck();

            selectDicingTexture = EditorLayoutTools.ObjectField(selectDicingTexture, false);

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

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

                deleteNames.Clear();
            }

            GUILayout.Space(5f);

            DrawSettingsGUI();

            GUILayout.Space(5f);

            if (selectDicingTexture == null)
            {
                DrawCreateGUI(selectionTextures);
            }
            else
            {
                DrawUpdateGUI(selectDicingTexture);
            }
        }
コード例 #5
0
        private void DrawUpdateGUI(DicingTexture dicingTexture)
        {
            var labelStyle = new GUIStyle(EditorStyles.label);

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

            var delete = false;

            if (textureInfos.Any())
            {
                EditorLayoutTools.DrawLabelWithBackground("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(EditorLayoutTools.TextAreaStyle, 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.DrawLabelWithBackground("Result", EditorLayoutTools.BackgroundColor, EditorLayoutTools.LabelColor);

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

                    var dicingMemSize = infoFileSize + totalAtlasMemSize;

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

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

                    var dicingFileSize = infoFileSize + atlasFileSize;

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

            GUILayout.Space(15f);

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

                GUILayout.FlexibleSpace();

                if (GUILayout.Button("Apply", GUILayout.Width(150f)))
                {
                    GenerateDicingTexture(dicingTexture);
                }

                GUI.backgroundColor = defaultBackgroundColor;

                GUILayout.Space(25f);

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

                    DicingSpriteSelector.Show(dicingTexture, selectionTextureName, onSelection, null);
                }

                GUILayout.FlexibleSpace();
            }

            if (delete)
            {
                GenerateDicingTexture(dicingTexture);
            }

            GUILayout.Space(5f);
        }