Created
March 2, 2018 07:39
-
-
Save hsienwei/1b76d6dfb43e281394e2cab07e331a7b to your computer and use it in GitHub Desktop.
將NGUI UIAtlas的spriteList 轉成 UGUI可用的spritesheet SpriteMetaData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_EDITOR | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
#endif | |
public class NGUIAtlasToStringConverter { | |
#if UNITY_EDITOR | |
[MenuItem("Tools/NGUI Atlas to Sprites Tool/Convert")] | |
private static void Convert() | |
{ | |
Debug.Log("Selected Object" + Selection.activeObject); | |
GameObject AtlasObj = Selection.activeObject as GameObject; | |
if (AtlasObj == null) | |
{ | |
Debug.Log("Target is not a GameObject."); | |
return; | |
} | |
UIAtlas AtlasComponent = AtlasObj.GetComponent<UIAtlas>(); | |
if (AtlasComponent == null) | |
{ | |
Debug.Log("Target does not have UIAtlas."); | |
return; | |
} | |
if(AtlasComponent.texture == null) | |
{ | |
Debug.Log("Target's UIAtlas texture is null."); | |
return; | |
} | |
string path = AssetDatabase.GetAssetPath(AtlasComponent.texture); | |
Debug.Log("Texture Path: " + path); | |
TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(path); | |
List<SpriteMetaData> SpriteLists = new List<SpriteMetaData>(); | |
foreach (UISpriteData Data in AtlasComponent.spriteList) | |
{ | |
PrintData(Data); | |
SpriteMetaData SpriteData = new SpriteMetaData(); | |
SpriteData.name = Data.name; | |
SpriteData.rect.x = Data.x; | |
SpriteData.rect.y = AtlasComponent.texture.height - Data.y - Data.height; // y value reverse. | |
SpriteData.rect.width = Data.width; | |
SpriteData.rect.height = Data.height; | |
SpriteData.pivot = new Vector2(0.5f, 0.5f); | |
SpriteLists.Add(SpriteData); | |
} | |
importer.isReadable = true; | |
importer.textureType = TextureImporterType.Sprite; | |
importer.spriteImportMode = SpriteImportMode.Multiple; | |
importer.spritesheet = SpriteLists.ToArray(); | |
importer.SaveAndReimport(); | |
Debug.Log("Success"); | |
} | |
static void PrintData(UISpriteData Data) | |
{ | |
Debug.Log(Data.name + "," + Data.x + "," + Data.y + "," + Data.width + "," + Data.height); | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment