public static List <SpriteMetaData> ProcessToSprites(string text) { Hashtable hashtable = text.hashtableFromJson(); TexturePacker.MetaData metaData = new TexturePacker.MetaData((Hashtable)hashtable["meta"]); List <TexturePacker.PackedFrame> list = new List <TexturePacker.PackedFrame>(); Hashtable hashtable2 = (Hashtable)hashtable["frames"]; foreach (object obj in hashtable2) { DictionaryEntry dictionaryEntry = (DictionaryEntry)obj; list.Add(new TexturePacker.PackedFrame((string)dictionaryEntry.Key, metaData.size, (Hashtable)dictionaryEntry.Value)); } TexturePacker.SortFrames(list); List <SpriteMetaData> list2 = new List <SpriteMetaData>(); for (int i = 0; i < list.Count; i++) { SpriteMetaData spriteMetaData = list[i].BuildBasicSprite(0.01f, new Color32(128, 128, 128, 128)); bool flag = !spriteMetaData.name.Equals("IGNORE_SPRITE"); if (flag) { list2.Add(spriteMetaData); } } return(list2); }
//处理一个文件 private static void ProcessOneFile(string txtPath) { txtPath = txtPath.Replace("\\", "/"); Debug.Log("开始处理图集" + txtPath.Substring(0, txtPath.LastIndexOf("_cfg")) + ".png"); string rootPath = txtPath; rootPath = rootPath.Substring(rootPath.IndexOf("Assets")); TextAsset txt = AssetDatabase.LoadAssetAtPath <TextAsset>(rootPath); string[] p = rootPath.Split(new string[] { "/" }, StringSplitOptions.None); string folder = p[p.Length - 2]; TexturePacker.MetaData meta = TexturePacker.GetMetaData(txt.text); List <SpriteMetaData> sprites = TexturePacker.ProcessToSprites(txt.text, folder); rootPath = rootPath.Substring(0, rootPath.LastIndexOf("/")); string path = rootPath + "/" + meta.image; TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter; texImp.spritesheet = sprites.ToArray(); texImp.textureType = TextureImporterType.Sprite; texImp.spriteImportMode = SpriteImportMode.Multiple; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); }
//[MenuItem("Assets/TexturePacker/Process to Sprites")] static void ProcessToSprite(int pivot) { TextAsset txt = (TextAsset)Selection.activeObject; string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(txt)); TexturePacker.MetaData meta = TexturePacker.GetMetaData(txt.text); List <SpriteMetaData> sprites = TexturePacker.ProcessToSprites(txt.text, pivot); string imageName = meta.image; //validate image name, if it's null or something, then try to load the sprite with the same name as text file with png extension if ((imageName == null) || (imageName.Length <= 0)) { imageName = Selection.activeObject.name; imageName += ".png"; } string path = rootPath + "/" + imageName; Debug.Log("Loading spritesheet at: " + path); TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter; texImp.spritesheet = sprites.ToArray(); texImp.textureType = TextureImporterType.Sprite; texImp.spriteImportMode = SpriteImportMode.Multiple; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); }
// Token: 0x06000044 RID: 68 RVA: 0x00004424 File Offset: 0x00002624 public static List <TexturePacker.PackedFrame> ProcessToFrames(string text) { Hashtable hashtable = text.hashtableFromJson(); TexturePacker.MetaData metaData = new TexturePacker.MetaData((Hashtable)hashtable["meta"]); List <TexturePacker.PackedFrame> list = new List <TexturePacker.PackedFrame>(); Hashtable hashtable2 = (Hashtable)hashtable["frames"]; foreach (object obj in hashtable2) { DictionaryEntry dictionaryEntry = (DictionaryEntry)obj; list.Add(new TexturePacker.PackedFrame((string)dictionaryEntry.Key, metaData.size, (Hashtable)dictionaryEntry.Value)); } TexturePacker.SortFrames(list); return(list); }
public static void ProcessToSprite(TextAsset txt) { string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(txt)); TexturePacker.MetaData meta = TexturePacker.GetMetaData(txt.text); List <SpriteMetaData> newSprites = TexturePacker.ProcessToSprites(txt.text); string path = rootPath + "/" + meta.image; TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter; if (texImp.spriteImportMode == SpriteImportMode.Multiple) { List <SpriteMetaData> oldSprites = new List <SpriteMetaData>(texImp.spritesheet); for (int i = 0; i < newSprites.Count; i++) { SpriteMetaData newSprite = newSprites[i]; for (int j = 0; j < oldSprites.Count; j++) { SpriteMetaData oldSprite = oldSprites[j]; if (newSprite.name == oldSprite.name) { if (newSprite.rect.width == oldSprite.rect.width && newSprite.rect.height == oldSprite.rect.height) { newSprite.alignment = oldSprite.alignment; newSprite.border = oldSprite.border; newSprites[i] = newSprite; } newSprite.pivot = oldSprite.pivot; break; } } } } texImp.textureType = TextureImporterType.Sprite; texImp.spriteImportMode = SpriteImportMode.Multiple; texImp.spritesheet = newSprites.ToArray(); //MoleMole.UIPostprocessor.CreateSpriteSheetScriptObject(texImp); AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); }
static void ProcessToSprite() { TextAsset txt = (TextAsset)Selection.activeObject; string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(txt)); TexturePacker.MetaData meta = TexturePacker.GetMetaData(txt.text); List <SpriteMetaData> sprites = TexturePacker.ProcessToSprites(txt.text); string path = rootPath + "/" + meta.image; TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter; texImp.spritesheet = sprites.ToArray(); texImp.textureType = TextureImporterType.Sprite; texImp.spriteImportMode = SpriteImportMode.Multiple; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); }
public static void ProcessOneFile(string txtPath, string spriteFolder) { txtPath = txtPath.Replace("\\", "/"); string rootPath = txtPath; rootPath = rootPath.Substring(rootPath.IndexOf("Assets")); string cfgPath = System.IO.Path.GetDirectoryName(rootPath); string fileName = System.IO.Path.GetFileNameWithoutExtension(rootPath); cfgPath = cfgPath + "/" + fileName + "_cfg"; string cfgTxt = System.IO.File.ReadAllText(cfgPath + ".json"); TexturePacker.MetaData meta = TexturePacker.GetMetaData(cfgTxt); List <SpriteMetaData> sprites = TexturePacker.ProcessToSprites(cfgTxt, spriteFolder); rootPath = rootPath.Substring(0, rootPath.LastIndexOf("/")); string path = rootPath + "/" + meta.image; TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter; if (texImp == null) { Debug.Log("textureImporter is null:" + path); return; } if (sprites == null) { Debug.Log("sprites is null :" + txtPath); return; } texImp.spritesheet = sprites.ToArray(); texImp.textureType = TextureImporterType.Sprite; texImp.spriteImportMode = SpriteImportMode.Multiple; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); }
static void ProcessToPrefabs() { Mesh[] meshes = ProcessToMeshes(); TextAsset txt = (TextAsset)Selection.activeObject; string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(txt)); string prefabPath = rootPath.Substring(7, rootPath.Length - 7) + "/Prefabs"; Directory.CreateDirectory(Application.dataPath + "/" + prefabPath); prefabPath = "Assets/" + prefabPath; //make material TexturePacker.MetaData meta = TexturePacker.GetMetaData(txt.text); string matPath = rootPath + "/" + (Path.GetFileNameWithoutExtension(meta.image) + ".mat"); string texturePath = rootPath + "/" + meta.image; Material mat = (Material)AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)); if (mat == null) { mat = new Material(Shader.Find("Sprites/Transparent Unlit")); Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)); if (tex == null) { EditorUtility.DisplayDialog("Error!", "Texture " + meta.image + " not found!", "Ok"); } mat.mainTexture = tex; AssetDatabase.CreateAsset(mat, matPath); } AssetDatabase.Refresh(); for (int i = 0; i < meshes.Length; i++) { string prefabFilePath = prefabPath + "/" + meshes[i].name + ".prefab"; bool createdNewPrefab = false; Object prefab = AssetDatabase.LoadAssetAtPath(prefabFilePath, typeof(Object)); if (prefab == null) { prefab = PrefabUtility.CreateEmptyPrefab(prefabFilePath); createdNewPrefab = true; } if (createdNewPrefab) { GameObject go = new GameObject(meshes[i].name, typeof(MeshRenderer), typeof(MeshFilter)); go.GetComponent <MeshFilter>().sharedMesh = meshes[i]; go.GetComponent <Renderer>().sharedMaterial = mat; PrefabUtility.ReplacePrefab(go, prefab, ReplacePrefabOptions.ConnectToPrefab); GameObject.DestroyImmediate(go); } else { GameObject pgo = (GameObject)prefab; pgo.GetComponent <Renderer>().sharedMaterial = mat; pgo.GetComponent <MeshFilter>().sharedMesh = meshes[i]; EditorUtility.SetDirty(pgo); } } }