Advertisement
Guest User

Untitled

a guest
Sep 8th, 2014
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.IO;
  7.  
  8. public class TileSplitter : MonoBehaviour
  9. {
  10.     public Texture2D tileset;
  11.     public int tileWidth = 32;
  12.     public int tileHeight = 32;
  13.  
  14.     public void SplitTiles()
  15.     {
  16.         int width = tileset.width / 32;
  17.         int height = tileset.height / 32;
  18.         string name = tileset.name;
  19.         int totalTiles = width * height;
  20.  
  21.         string path = AssetDatabase.GetAssetPath(tileset) + ".meta";
  22.         List<string> lines = new List<string>();
  23.         string line = null;
  24.         using (StreamReader file =  new StreamReader(path))
  25.         {
  26.             lines.Add(file.ReadLine());
  27.             file.ReadLine();
  28.             lines.Add("guid: " + System.Guid.NewGuid());
  29.  
  30.             while ((line = file.ReadLine()) != null)
  31.             {
  32.                 lines.Add(line);
  33.                 if (line.Contains("sprites:"))
  34.                     break;  
  35.             }
  36.         }
  37.  
  38.         int xCount = 0;
  39.         int yCount = height;
  40.         for (int i = 0; i < totalTiles; ++i)
  41.         {
  42.             int x = xCount * tileWidth;
  43.             int y = yCount * tileHeight;
  44.  
  45.             lines.Add("    - name: " + name + "_" + i);
  46.             lines.Add("      rect:");
  47.             lines.Add("        serializedVersion: 2");
  48.             lines.Add("        x: " + x);
  49.             lines.Add("        y: " + y);
  50.             lines.Add("        width: " + tileWidth);
  51.             lines.Add("        height: " + tileHeight);
  52.             lines.Add("      alignment: 0");
  53.             lines.Add("      pivot: {x: 0, y: 0}");
  54.             lines.Add("      border: {x: 0, y: 0, z: 0, w: 0}");
  55.  
  56.             ++xCount;
  57.             if(xCount >= width)
  58.             {
  59.                 xCount = 0;
  60.                 --yCount;
  61.             }
  62.         }
  63.         lines.Add("  spritePackingTag: ");
  64.         lines.Add("  userData: ");
  65.            
  66.         File.Delete(path);
  67.         File.WriteAllLines(path, lines.ToArray());
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement