Example #1
0
        public void DoAutomaticSlicing(int minimumSpriteSize, int alignment, Vector2 pivot, AutoSlicingMethod slicingMethod)
        {
            undoSystem.RegisterCompleteObjectUndo(m_RectsCache, "Automatic Slicing");

            if (slicingMethod == AutoSlicingMethod.DeleteAll)
            {
                m_RectsCache.Clear();
            }

            var         textureToUse = GetTextureToSlice();
            List <Rect> frames       = new List <Rect>(InternalSpriteUtility.GenerateAutomaticSpriteRectangles((UnityTexture2D)textureToUse, minimumSpriteSize, 0));

            frames = SortRects(frames);
            int index         = 0;
            int originalCount = m_RectsCache.spriteRects.Count;

            foreach (Rect frame in frames)
            {
                AddSprite(frame, alignment, pivot, slicingMethod, originalCount, ref index);
            }

            selected = null;
            spriteEditor.SetDataModified();
            Repaint();
        }
Example #2
0
    public static void GenerateMultiple()
    {
        string path         = EditorUtility.OpenFolderPanel("Sprite Folder", "", "");
        string relativePath = "Assets" + path.Substring(Application.dataPath.Length);

        if (!AssetDatabase.IsValidFolder(relativePath))
        {
            return;
        }

        string[] folders  = new string[] { relativePath };
        string[] textures = AssetDatabase.FindAssets("t:Texture2D", folders);

        for (int i = 0; i < textures.Length; i++)
        {
            TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(AssetDatabase.GUIDToAssetPath(textures[i]));
            Texture2D       texture  = (Texture2D)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(textures[i]), typeof(Texture2D));
            importer.textureType        = TextureImporterType.Sprite;
            importer.spriteImportMode   = SpriteImportMode.Multiple;
            importer.isReadable         = true;
            importer.maxTextureSize     = 8192;
            importer.filterMode         = FilterMode.Point;
            importer.textureCompression = TextureImporterCompression.Uncompressed;

            AssetDatabase.Refresh();

            int    minimumSpriteSize = 16;
            int    extrudeSize       = 0;
            Rect[] rects             = InternalSpriteUtility.GenerateAutomaticSpriteRectangles(texture, minimumSpriteSize, extrudeSize);

            string p            = AssetDatabase.GUIDToAssetPath(textures[i]);
            string relative     = p.Replace("Assets/", "");
            string absolutePath = Application.dataPath + "/" + p;

            string filenameNoExtension  = Path.GetFileNameWithoutExtension(absolutePath);
            List <SpriteMetaData> metas = new List <SpriteMetaData>();
            int rectNum = 0;

            foreach (Rect rect in rects)
            {
                SpriteMetaData meta = new SpriteMetaData();
                meta.rect = rect;
                meta.name = filenameNoExtension + "_" + rectNum++;
                Debug.Log(meta.name);
                metas.Add(meta);
            }

            importer.spritesheet = metas.ToArray();
            importer.SaveAndReimport();
            AssetDatabase.Refresh();
        }
    }
Example #3
0
    static void ProcessTexture(string path, bool isAutomatics)
    {
        Rect[]    rects   = null;
        Texture2D texture = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D;

        if (isAutomatics)
        {
            int minimumSpriteSize = 16;
            int extrudeSize       = 0;
            rects = InternalSpriteUtility.GenerateAutomaticSpriteRectangles(texture, minimumSpriteSize, extrudeSize);
        }
        else
        {
            string[] str       = texture.name.Split('_');
            int      widthNum  = int.Parse(str[str.Length - 2]);
            int      hrightNum = int.Parse(str[str.Length - 1]);
            Vector2  size      = new Vector2();
            size.x = (float)texture.width / widthNum;
            size.y = (float)texture.height / hrightNum;
            rects  = InternalSpriteUtility.GenerateGridSpriteRectangles(texture, Vector2.zero, size, Vector2.zero);
        }

        var rectsList = new List <Rect>(rects);

        rectsList = SortRects(rectsList, texture.width);

        string filenameNoExtension = Path.GetFileNameWithoutExtension(path);
        var    metas   = new List <SpriteMetaData>();
        int    rectNum = 0;

        foreach (Rect rect in rectsList)
        {
            var meta = new SpriteMetaData();
            meta.pivot     = Vector2.down;
            meta.alignment = (int)SpriteAlignment.BottomCenter;
            meta.rect      = rect;
            meta.name      = filenameNoExtension + "_" + rectNum++;
            metas.Add(meta);
        }

        var importer = AssetImporter.GetAtPath(path) as TextureImporter;

        importer.spritesheet = metas.ToArray();
        EditorUtility.SetDirty(importer);
        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
        AssetDatabase.Refresh();
    }
Example #4
0
    private void GenerateMultiple(Texture2D tex)
    {
        Debug.Log("Generate Multiple: " + tex.name);

        TextureImporter            importer    = (TextureImporter)TextureImporter.GetAtPath(AssetDatabase.GetAssetPath(tex));
        TextureImporterCompression compression = importer.textureCompression;
        bool readable = importer.isReadable;

        importer.isReadable         = true;
        importer.textureCompression = TextureImporterCompression.Uncompressed;
        importer.spriteImportMode   = SpriteImportMode.Multiple;

        importer.SaveAndReimport();
        AssetDatabase.Refresh();

        int minimumSpriteSize = 16;
        int extrudeSize       = 0;

        Rect[] rects = InternalSpriteUtility.GenerateAutomaticSpriteRectangles(tex, minimumSpriteSize, extrudeSize);

        List <SpriteMetaData> metas = new List <SpriteMetaData>();
        int rectNum = 0;

        foreach (Rect r in rects)
        {
            SpriteMetaData meta = new SpriteMetaData();
            meta.rect = r;
            meta.name = tex.name + "_" + rectNum++;
            Debug.Log(meta.name);
            metas.Add(meta);
        }

        importer.spritesheet = metas.ToArray();
        //importer.SaveAndReimport();
        //AssetDatabase.Refresh();

        importer.textureCompression = compression;
        importer.isReadable         = readable;
        importer.SaveAndReimport();
        AssetDatabase.Refresh();
    }
        public void DoAutomaticSlicing(int minimumSpriteSize, int alignment, Vector2 pivot, SpriteFrameModule.AutoSlicingMethod slicingMethod)
        {
            base.undoSystem.RegisterCompleteObjectUndo(this.m_RectsCache, "Automatic Slicing");
            if (slicingMethod == SpriteFrameModule.AutoSlicingMethod.DeleteAll)
            {
                this.m_RectsCache.spriteRects.Clear();
            }
            UnityEngine.Texture2D readableTexture2D = this.m_TextureDataProvider.GetReadableTexture2D();
            List <Rect>           list = new List <Rect>(InternalSpriteUtility.GenerateAutomaticSpriteRectangles(readableTexture2D, minimumSpriteSize, 0));

            list = this.SortRects(list);
            int num = 0;

            foreach (Rect current in list)
            {
                this.AddSprite(current, alignment, pivot, slicingMethod, ref num);
            }
            base.selected = null;
            base.spriteEditor.SetDataModified();
            base.Repaint();
        }
        private void ImportSpriteSheetIntoAnimation(Texture2D spriteSheet, int animIdx = -1)
        {
            string assetPath = AssetDatabase.GetAssetPath(spriteSheet);

            if (string.IsNullOrEmpty(assetPath))
            {
                return;
            }
            TextureImporter spriteSheetImporter = (TextureImporter)TextureImporter.GetAtPath(assetPath);

            int characterNb;
            int charRowLength;
            int charColumnLength;
            int charFramesCount = m_target.DirectionsPerAnim * m_target.FramesPerAnim;
            int columns = 0, rows = 0;

            if (spriteSheetImporter.textureType != TextureImporterType.Sprite ||
                spriteSheetImporter.spriteImportMode != SpriteImportMode.Multiple ||
                spriteSheetImporter.spritesheet.Length == 0 ||
                spriteSheetImporter.spritesheet.Length % charFramesCount != 0)
            {
                Rect[] rects = InternalSpriteUtility.GenerateAutomaticSpriteRectangles(spriteSheet, 4, 0);
                if (rects.Length > 0 && rects.Length % charFramesCount == 0)
                {
                    for (; columns < rects.Length; ++columns)
                    {
                        //NOTE: the order of slicing in GenerateAutomaticSpriteRectangles is from bottom to top, not from top to bottom like Sprite Editor Slicing
                        if (rects[columns].yMin >= rects[0].yMax)
                        {
                            rows = rects.Length / columns;
                            break;
                        }
                    }
                }
                else
                {
                    columns = m_target.FramesPerAnim;
                    rows    = m_target.DirectionsPerAnim;
                }

                charRowLength    = Mathf.Max(1, columns / m_target.FramesPerAnim);
                charColumnLength = Mathf.Max(1, rows / m_target.FramesPerAnim);
                characterNb      = charRowLength * charColumnLength;

                int spriteCount = charFramesCount * characterNb;
                SpriteMetaData[] aSpriteMetaData = spriteSheetImporter.spritesheet;
                if (spriteSheetImporter.spritesheet.Length != spriteCount || spriteSheetImporter.spriteImportMode != SpriteImportMode.Multiple)
                {
                    aSpriteMetaData = new SpriteMetaData[spriteCount];
                    spriteSheetImporter.textureType      = TextureImporterType.Sprite;
                    spriteSheetImporter.spriteImportMode = SpriteImportMode.Multiple;
                    spriteSheetImporter.filterMode       = FilterMode.Point;
                    spriteSheetImporter.mipmapEnabled    = false;
                    spriteSheetImporter.textureFormat    = TextureImporterFormat.AutomaticTruecolor;
                    Rect spriteRect = new Rect(0, 0, spriteSheet.width / (m_target.FramesPerAnim * charRowLength), spriteSheet.height / (m_target.DirectionsPerAnim * charColumnLength));
                    for (int gy = 0, spriteIdx = 0; gy < rows; ++gy)
                    {
                        for (int gx = 0; gx < columns; ++gx, ++spriteIdx)
                        {
                            spriteRect.position = new Vector2(gx * spriteRect.width, spriteSheet.height - (1 + gy) * spriteRect.height);
                            SpriteMetaData spriteMetaData = new SpriteMetaData();
                            //int characterIdx = (gy / m_target.DirectionNb) * charRowLength + (gx / m_target.FramesPerAnim);

                            //NOTE: the sprites are sorted alphabetically, so spriteIdx should be in the first place after the name and nothing else
                            spriteMetaData.name        = spriteSheet.name + "_" + spriteIdx; // + (characterNb > 1 ? ("_" + characterIdx) : "") + "_" + gy + "_" + gx;
                            spriteMetaData.rect        = spriteRect;
                            aSpriteMetaData[spriteIdx] = spriteMetaData;
                        }
                    }
                    spriteSheetImporter.spritesheet = aSpriteMetaData;
                    AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
                }
            }

            UpdateAligmentAndPivot(spriteSheet);

            List <Sprite> sprites = new List <Sprite>(AssetDatabase.LoadAllAssetsAtPath(assetPath).OfType <Sprite>().ToArray());

            //sort them properly using the last number
            sprites = sprites.OrderBy(s => int.Parse(s.name.Substring(s.name.LastIndexOf("_") + 1))).ToList();
            for (; columns < sprites.Count; ++columns)
            {
                if (sprites[columns].rect.yMax <= sprites[0].rect.yMin)
                {
                    rows = sprites.Count / columns;
                    break;
                }
            }

            if (columns * rows != sprites.Count || columns % m_target.FramesPerAnim != 0 || rows % m_target.DirectionsPerAnim != 0)
            {
                Debug.LogError("Something was wrong with the sprite sheet. Try slicing it again using the Sprite Editor using grid settings or set the Sprite Mode to single and try again.");
                return;
            }

            List <Sprite> sortedSprites = new List <Sprite>();

            charRowLength    = Mathf.Max(1, columns / m_target.FramesPerAnim);
            charColumnLength = Mathf.Max(1, rows / m_target.FramesPerAnim);
            for (int charY = 0; charY < charColumnLength; ++charY)
            {
                for (int charX = 0; charX < charRowLength; ++charX)
                {
                    for (int c = 0; c < m_target.DirectionsPerAnim; ++c)
                    {
                        for (int r = 0; r < m_target.FramesPerAnim; ++r)
                        {
                            int gx = charX * m_target.FramesPerAnim + r;
                            int gy = charY * m_target.DirectionsPerAnim + c;
                            sortedSprites.Add(sprites[gy * columns + gx]);
                        }
                    }
                }
            }
            characterNb = sortedSprites.Count / charFramesCount;

            if (animIdx >= 0)
            {
                ImportSpriteSheetIntoAnimation(spriteSheet, sortedSprites.Take(charFramesCount).ToArray(), animIdx, spriteSheet.name);
            }
            else
            {
                for (int characterIdx = 0; characterIdx < characterNb; ++characterIdx)
                {
                    Sprite[] characterSprites = sortedSprites.Skip(characterIdx * charFramesCount).Take(charFramesCount).ToArray();
                    string   charName         = spriteSheet.name + (characterNb > 1? ("_" + characterIdx) : "");
                    ImportSpriteSheetIntoAnimation(spriteSheet, characterSprites, m_target.GetAnimList().FindIndex(x => x.name == charName), charName);
                }
            }
        }
Example #7
0
    static void ConvertTextureToSprite(Texture2D texture)
    {
        Debug.Log("Processing Textures -> " + texture.name.ToString() + " " + texture.width + "," + texture.height);


        // Get Texture2D

        // get asset at path
        string path = AssetDatabase.GetAssetPath(texture);
        // create TextureImporter
        var importer = AssetImporter.GetAtPath(path) as TextureImporter;


        // Texture2D / Sprite options

        // setting to false saves memory
        importer.isReadable = true;
        // change to sprite
        importer.textureType = TextureImporterType.Sprite;
        // change to spritesheets
        importer.spriteImportMode = SpriteImportMode.Multiple;
        // point in the Sprite object's coordinate space where the graphic is located
        importer.spritePivot = Vector2.down;


        // performance / visual

        // allow to increase visual quality at small sizes
        importer.mipmapEnabled = false;
        // filter mode, performance (high to low) = point, bi, tri
        // https://forum.unity.com/threads/filter-mode-point-bilinear-trilinear-which-is-the-cheapest.147523/
        importer.filterMode = FilterMode.Point;
        // image compression https://docs.unity3d.com/ScriptReference/TextureImporterCompression.html
        importer.textureCompression = TextureImporterCompression.Uncompressed;


        // Texture2D mesh options

        // spriteExtrude and spriteMeshType aren't exposed on TextureImporter
        // https://docs.unity3d.com/ScriptReference/TextureImporterSettings.html
        var textureImporterSettings = new TextureImporterSettings();

        importer.ReadTextureSettings(textureImporterSettings);
        // set mesh type to full rectangle (original size)
        textureImporterSettings.spriteMeshType = SpriteMeshType.FullRect;
        // number of blank pixels to leave between the edge of the graphic and the mesh.
        textureImporterSettings.spriteExtrude = 0;
        // add texture-specific settings to importer
        importer.SetTextureSettings(textureImporterSettings);



        // Sprite Slice options

        // number animation slices
        int slices = 3;
        // ?
        int extrudeSize = 0;
        // width / height of sprite slices
        // monsters-400h: 500w x 400h
        int sliceW = texture.width / slices;
        int sliceH = texture.height;


        // Create slices

        // generate sprite rects on texture,
        Rect[] rects = InternalSpriteUtility.GenerateAutomaticSpriteRectangles(texture, slices, extrudeSize);
        // get and sort list of rects added
        var rectsList = new List <Rect>(rects);

        rectsList = SortRects(rectsList, texture.width);


        // Save slices

        // get filename
        string filenameNoExtension = Path.GetFileNameWithoutExtension(path);
        // create sprite metadata
        var metas = new List <SpriteMetaData>();
        // the current rect we are adding
        int rectNum = 0;

        // loop through the rects in the sprite
        foreach (Rect rect in rectsList)
        {
            // create meta for individual sprite
            var meta = new SpriteMetaData();
            // set sprite pivot
            meta.pivot = Vector2.down;
            // set sprite alignment
            meta.alignment = (int)SpriteAlignment.BottomCenter;
            // set rect coordinates: X1,Y1 X2,Y2
            meta.rect = new Rect((rectNum * sliceW), 0, sliceW, sliceH);
            // update sprite name
            meta.name = filenameNoExtension + "_" + rectNum++;
            // add to metadata
            metas.Add(meta);

            Debug.Log("Adding sprite slice -> " + meta.name + " -> " + meta);
        }

        Debug.Log("Saving slices -> " + texture.name + " " + metas.ToString());

        importer.spritesheet = metas.ToArray();

        // save changes to AssetDatabase
        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
    }