static SpriteMetaData[] CreateSpriteSheetForTextureBogdan(Texture2D texture, SpriteSlicingOptions slicingOptions)
        {
            Rect[] gridRects = InternalSpriteUtility.GenerateGridSpriteRectangles(texture, Vector2.zero,
                slicingOptions.CellSize, Vector2.zero);

            string path = AssetDatabase.GetAssetPath(texture);
            var importer = AssetImporter.GetAtPath(path) as TextureImporter;

            var spriteSheet = importer.spritesheet ?? new SpriteMetaData[gridRects.Length];

            // Add new sprite meta data to the end for all the newly parsed grid rects?
            if (importer.spritesheet != null)
                spriteSheet = spriteSheet.Concat(new SpriteMetaData[Mathf.Max(0, gridRects.Length - importer.spritesheet.Length)]).ToArray();

            for (var i = 0; i < spriteSheet.Length; i++)
            {
                bool sliceExists = importer.spritesheet != null && i < importer.spritesheet.Length;
                bool changePivot = !sliceExists || slicingOptions.OverridePivot;
                spriteSheet[i] = new SpriteMetaData
                {
                    alignment = changePivot ? (int) slicingOptions.Pivot: spriteSheet[i].alignment,
                    pivot = changePivot ? slicingOptions.CustomPivot : spriteSheet[i].pivot,
                    name = sliceExists ? spriteSheet[i].name : texture.name + "_" + i,
                    rect = gridRects[i]
                };
            }

            if (slicingOptions.Frames > 0)
                spriteSheet = spriteSheet.Take((int) slicingOptions.Frames).ToArray();

            return spriteSheet;
        }
Пример #2
0
        public static SpriteSlicingOptions FromString(string serialized)
        {
            var options = new SpriteSlicingOptions();

            string[] entries = serialized.Split(delimeterChar);

            // Default ImportMode to Multiple for versioned options
            options.ImportMode = UnityEditor.SpriteImportMode.Multiple;
            // Default GridSlicing to Bogdan method
            options.SlicingMethod = GridSlicingMethod.Bogdan;

            options.CellSize = new Vector2(int.Parse(entries[0]), int.Parse(entries[1]));
            if (entries.Length >= 3)
            {
                int.TryParse(entries[2], out options.Frames);
                if (entries.Length >= 9)
                {
                    options.ImportMode    = (SpriteImportMode)int.Parse(entries[3]);
                    options.SlicingMethod = (GridSlicingMethod)int.Parse(entries[4]);
                    options.OverridePivot = bool.Parse(entries[5]);
                    options.Pivot         = (SpriteAlignment)int.Parse(entries[6]);
                    options.CustomPivot   = new Vector2(float.Parse(entries[7]), float.Parse(entries[8]));
                }
            }

            return(options);
        }
        public static SpriteSlicingOptions FromString(string serialized)
        {
            var options = new SpriteSlicingOptions ();
            string[] entries = serialized.Split (delimeterChar);

            // Default ImportMode to Multiple for versioned options
            options.ImportMode = UnityEditor.SpriteImportMode.Multiple;
            // Default GridSlicing to Bogdan method
            options.SlicingMethod = GridSlicingMethod.Bogdan;

            options.CellSize = new Vector2 (int.Parse (entries[0]), int.Parse (entries[1]));
            if (entries.Length >= 3)
            {
                int.TryParse (entries[2], out options.Frames);
                if (entries.Length >= 9)
                {
                    options.ImportMode = (SpriteImportMode) int.Parse (entries[3]);
                    options.SlicingMethod = (GridSlicingMethod) int.Parse (entries[4]);
                    options.OverridePivot = bool.Parse (entries[5]);
                    options.Pivot = (SpriteAlignment) int.Parse (entries[6]);
                    options.CustomPivot = new Vector2 (float.Parse (entries[7]), float.Parse (entries[8]));
                }
            }

            return options;
        }
 public static SpriteMetaData[] CreateSpriteSheetForTexture(Texture2D texture, SpriteSlicingOptions slicingOptions)
 {
     switch (slicingOptions.SlicingMethod)
     {
         case SpriteSlicingOptions.GridSlicingMethod.SliceAll :
             return CreateSpriteSheetForTextureSliceAll (texture, slicingOptions);
         case SpriteSlicingOptions.GridSlicingMethod.Bogdan :
             return CreateSpriteSheetForTextureBogdan (texture, slicingOptions);
         default :
             Debug.LogError ("Trying to create spritesheet with unknown slicing method: " + slicingOptions.SlicingMethod);
             return null;
     }
 }
    public void SlicesTopLeftToBottomRight()
    {
        var expectedImporter = AssetImporter.GetAtPath(pathToExpectedNormal) as TextureImporter;
        var expectedSpritesheet = expectedImporter.spritesheet;

        var slicingOptions = new SpriteSlicingOptions ();
        slicingOptions.CellSize = new Vector2 (86, 86);
        slicingOptions.ImportMode = UnityEditor.SpriteImportMode.Multiple;
        slicingOptions.Pivot = SpriteAlignment.Center;
        slicingOptions.SlicingMethod = SpriteSlicingOptions.GridSlicingMethod.SliceAll;
        SpriteMetaData[] createdSpritesheet = SpriteSlicer.CreateSpriteSheetForTexture (SampleTexture, slicingOptions);

        CompareFirstAndLastRects (expectedSpritesheet, createdSpritesheet);
    }
 static SpriteMetaData[] CreateSpriteSheetForTextureSliceAll(Texture2D texture, SpriteSlicingOptions slicingOptions)
 {
     List<SpriteMetaData> sprites = new List<SpriteMetaData> ();
     Rect[] gridRects = GetAllSliceRectsForTexture (texture, slicingOptions.CellSize);
     for (int i = 0; i < gridRects.Length; i++) {
         SpriteMetaData spriteMetaData = new SpriteMetaData ();
         spriteMetaData.rect = gridRects[i];
         spriteMetaData.alignment = (int) slicingOptions.Pivot;
         spriteMetaData.pivot = slicingOptions.CustomPivot;
         spriteMetaData.name = texture.name + "_" + i;
         sprites.Add (spriteMetaData);
     }
     return sprites.ToArray ();
 }
    public void CorrectlySlices()
    {
        var expectedImporter = AssetImporter.GetAtPath(pathToExpectedOffset) as TextureImporter;
        var expectedSpritesheet = expectedImporter.spritesheet;

        var slicingOptions = new SpriteSlicingOptions ();
        slicingOptions.CellSize = new Vector2 (86, 86);
        slicingOptions.ImportMode = UnityEditor.SpriteImportMode.Multiple;
        slicingOptions.Pivot = SpriteAlignment.Center;
        slicingOptions.SlicingMethod = SpriteSlicingOptions.GridSlicingMethod.SliceAll;
        SpriteMetaData[] createdSpritesheet = SpriteSlicer.CreateSpriteSheetForTexture (SampleTexture, slicingOptions);

        Assert.AreEqual (expectedSpritesheet.Length, createdSpritesheet.Length,
            "Number of Slices not equal to NumExpectedSlices");
    }
Пример #8
0
        static SpriteMetaData[] CreateSpriteSheetForTextureBogdan(Texture2D texture, SpriteSlicingOptions slicingOptions)
        {
            Rect[] gridRects = InternalSpriteUtility.GenerateGridSpriteRectangles(texture, Vector2.zero,
                                                                                  slicingOptions.CellSize, Vector2.zero);

            string path     = AssetDatabase.GetAssetPath(texture);
            var    importer = AssetImporter.GetAtPath(path) as TextureImporter;

            var spriteSheet = importer.spritesheet ?? new SpriteMetaData[gridRects.Length];

            // Add new sprite meta data to the end for all the newly parsed grid rects?
            if (importer.spritesheet != null)
            {
                spriteSheet = spriteSheet.Concat(new SpriteMetaData[Mathf.Max(0, gridRects.Length - importer.spritesheet.Length)]).ToArray();
            }

            for (var i = 0; i < spriteSheet.Length; i++)
            {
                bool sliceExists = importer.spritesheet != null && i < importer.spritesheet.Length;
                bool changePivot = !sliceExists || slicingOptions.OverridePivot;
                spriteSheet[i] = new SpriteMetaData
                {
                    alignment = changePivot ? (int)slicingOptions.Pivot: spriteSheet[i].alignment,
                    pivot     = changePivot ? slicingOptions.CustomPivot : spriteSheet[i].pivot,
                    name      = sliceExists ? spriteSheet[i].name : texture.name + "_" + i,
                    rect      = gridRects[i]
                };
            }

            if (slicingOptions.Frames > 0)
            {
                spriteSheet = spriteSheet.Take((int)slicingOptions.Frames).ToArray();
            }

            return(spriteSheet);
        }
        public static void ApplySpriteSettings(Texture2D texture,
                                               SpriteSettings prefs, SpriteFileSettings fileSettings)
        {
            if (prefs == null)
            {
                return;
            }

            string path     = AssetDatabase.GetAssetPath(texture);
            var    importer = AssetImporter.GetAtPath(path) as TextureImporter;

            // When we have text file data
            SpriteSlicingOptions slicingOptions = fileSettings.SlicingOptions;

            if (slicingOptions.ImportMode == SpriteImportMode.Multiple)
            {
                // Clamp cellSize to texture width and height
                slicingOptions.CellSize.x = Mathf.Min(texture.width, slicingOptions.CellSize.x);
                slicingOptions.CellSize.y = Mathf.Min(texture.height, slicingOptions.CellSize.y);

                SpriteMetaData[] spriteSheet;
                spriteSheet = SpriteSlicer.CreateSpriteSheetForTexture(AssetDatabase.LoadAssetAtPath(path,
                                                                                                     typeof(Texture2D)) as Texture2D, slicingOptions);

                // If we don't do this it won't update the new sprite meta data
                importer.spriteImportMode = SpriteImportMode.Single;
                importer.spriteImportMode = SpriteImportMode.Multiple;

                importer.spritesheet = spriteSheet;
            }
            else if (slicingOptions.ImportMode == SpriteImportMode.Single)
            {
                importer.spriteImportMode = SpriteImportMode.Single;
            }
            else if (slicingOptions.ImportMode == SpriteImportMode.None)
            {
                // Do nothing for None mode for now.
            }
            else
            {
                throw new System.NotSupportedException("Encountered unsupported SpriteImportMode:"
                                                       + slicingOptions.ImportMode);
            }

            TextureImporterSettings settings = new TextureImporterSettings();

            importer.ReadTextureSettings(settings);
            importer.textureType = TextureImporterType.Advanced;

            settings.filterMode     = prefs.FilterMode;
            settings.wrapMode       = prefs.WrapMode;
            settings.mipmapEnabled  = prefs.GenerateMipMaps;
            settings.textureFormat  = prefs.TextureFormat;
            settings.maxTextureSize = prefs.MaxSize;

            settings.spritePixelsPerUnit = prefs.PixelsPerUnit;

            settings.spriteExtrude  = (uint)Mathf.Clamp(prefs.ExtrudeEdges, 0, 32);
            settings.spriteMeshType = prefs.SpriteMeshType;

            // Settings also store Sprite Alignment for Single spritemode
            settings.spriteAlignment = (int)slicingOptions.Pivot;
            if (slicingOptions.Pivot == SpriteAlignment.Custom)
            {
                settings.spritePivot = slicingOptions.CustomPivot;
            }

            importer.spritePackingTag = fileSettings.PackingTag;

            importer.SetTextureSettings(settings);
#if UNITY_5_0
            importer.SaveAndReimport();
#else
            AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
#endif
            EditorUtility.SetDirty(texture);
            WriteSpriteFileSettings(path, prefs.SpritesheetDataFile, texture.name + ".png", fileSettings);
        }
Пример #10
0
        static SpriteMetaData[] CreateSpriteSheetForTextureSliceAll(Texture2D texture, SpriteSlicingOptions slicingOptions)
        {
            List <SpriteMetaData> sprites = new List <SpriteMetaData> ();

            Rect[] gridRects = GetAllSliceRectsForTexture(texture, slicingOptions.CellSize);
            for (int i = 0; i < gridRects.Length; i++)
            {
                SpriteMetaData spriteMetaData = new SpriteMetaData();
                spriteMetaData.rect      = gridRects[i];
                spriteMetaData.alignment = (int)slicingOptions.Pivot;
                spriteMetaData.pivot     = slicingOptions.CustomPivot;
                spriteMetaData.name      = texture.name + "_" + i;
                sprites.Add(spriteMetaData);
            }
            return(sprites.ToArray());
        }
Пример #11
0
        public static SpriteMetaData[] CreateSpriteSheetForTexture(Texture2D texture, SpriteSlicingOptions slicingOptions)
        {
            switch (slicingOptions.SlicingMethod)
            {
            case SpriteSlicingOptions.GridSlicingMethod.SliceAll:
                return(CreateSpriteSheetForTextureSliceAll(texture, slicingOptions));

            case SpriteSlicingOptions.GridSlicingMethod.Bogdan:
                return(CreateSpriteSheetForTextureBogdan(texture, slicingOptions));

            default:
                Debug.LogError("Trying to create spritesheet with unknown slicing method: " + slicingOptions.SlicingMethod);
                return(null);
            }
        }