Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- public class TileSplitter : MonoBehaviour
- {
- public Texture2D tileset;
- public int tileWidth = 32;
- public int tileHeight = 32;
- public void SplitTiles()
- {
- int width = tileset.width / 32;
- int height = tileset.height / 32;
- string name = tileset.name;
- int totalTiles = width * height;
- string path = AssetDatabase.GetAssetPath(tileset) + ".meta";
- List<string> lines = new List<string>();
- string line = null;
- using (StreamReader file = new StreamReader(path))
- {
- lines.Add(file.ReadLine());
- file.ReadLine();
- lines.Add("guid: " + System.Guid.NewGuid());
- while ((line = file.ReadLine()) != null)
- {
- lines.Add(line);
- if (line.Contains("sprites:"))
- break;
- }
- }
- int xCount = 0;
- int yCount = height;
- for (int i = 0; i < totalTiles; ++i)
- {
- int x = xCount * tileWidth;
- int y = yCount * tileHeight;
- lines.Add(" - name: " + name + "_" + i);
- lines.Add(" rect:");
- lines.Add(" serializedVersion: 2");
- lines.Add(" x: " + x);
- lines.Add(" y: " + y);
- lines.Add(" width: " + tileWidth);
- lines.Add(" height: " + tileHeight);
- lines.Add(" alignment: 0");
- lines.Add(" pivot: {x: 0, y: 0}");
- lines.Add(" border: {x: 0, y: 0, z: 0, w: 0}");
- ++xCount;
- if(xCount >= width)
- {
- xCount = 0;
- --yCount;
- }
- }
- lines.Add(" spritePackingTag: ");
- lines.Add(" userData: ");
- File.Delete(path);
- File.WriteAllLines(path, lines.ToArray());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement