/// <summary> /// Name of the selected sprite. /// </summary> //public string spriteName { get { return (mSprite != null) ? mSprite.spriteName : mName; } } /// <summary> /// Show the selection wizard. /// </summary> public static void Show(NJGAtlas atlas, string selected, Callback callback) { IconSelector comp = ScriptableWizard.DisplayWizard <IconSelector>("Select a Sprite"); comp.mAtlas = atlas; comp.mSprite = selected; comp.mCallback = callback; }
/// <summary> /// Figures out the saveable filename for the texture of the specified atlas. /// </summary> static public string GetSaveableTexturePath(NJGAtlas atlas) { // Path where the texture atlas will be saved string path = ""; // If the atlas already has a texture, overwrite its texture if (atlas.texture != null) { path = AssetDatabase.GetAssetPath(atlas.texture.GetInstanceID()); if (!string.IsNullOrEmpty(path)) { int dot = path.LastIndexOf('.'); return(path.Substring(0, dot) + ".png"); } } // No texture to use -- figure out a name using the atlas path = AssetDatabase.GetAssetPath(atlas.GetInstanceID()); path = string.IsNullOrEmpty(path) ? "Assets/" + atlas.name + ".png" : path.Replace(".prefab", ".png"); return(path); }
static public void UpdateAtlas(NJGAtlas atlas, string path) { //Object[] objects = AssetDatabase.LoadAllAssetsAtPath(path); List <Texture2D> mTextures = new List <Texture2D>(); string[] fn = System.IO.Directory.GetFiles(path); int count = 0; foreach (string s in fn) { foreach (string e in extensions) { string ext = s.Substring(s.LastIndexOf('.')); if (ext.Equals(e, System.StringComparison.OrdinalIgnoreCase)) { bool ignore = false; Texture2D t = (Texture2D)AssetDatabase.LoadAssetAtPath(s, typeof(Texture2D)); if (atlas.texture != null) { ignore = atlas.texture == t; } if (!ignore) { ImportTexture(s, true, true); if (t != null) { mTextures.Add(t); } count++; } } } } if (mTextures.Count > 0) { // Get the texture for the atlas Texture2D tex = atlas.texture as Texture2D; string oldPath = (tex != null) ? AssetDatabase.GetAssetPath(tex.GetInstanceID()) : ""; string newPath = GetSaveableTexturePath(atlas); bool newTexture = (tex == null || oldPath != newPath); if (newTexture) { // Create a new texture for the atlas tex = new Texture2D(1, 1, TextureFormat.ARGB32, false); } else { // Make the atlas readable so we can save it tex = ImportTexture(oldPath, true, false); } Rect[] rects = tex.PackTextures(mTextures.ToArray(), atlas.padding, atlas.size); atlas.sprites = new NJGAtlas.Sprite[rects.Length]; for (int i = 0, imax = rects.Length; i < imax; i++) { atlas.sprites[i] = new NJGAtlas.Sprite(); atlas.sprites[i].id = i; atlas.sprites[i].name = mTextures[i].name; atlas.sprites[i].uvs = rects[i]; } byte[] bytes = tex.EncodeToPNG(); System.IO.File.WriteAllBytes(newPath, bytes); bytes = null; // Load the texture we just saved as a Texture2D AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); tex = ImportTexture(newPath, false, true); // Update the atlas texture if (newTexture) { if (tex == null) { Debug.LogError("Failed to load the created atlas saved as " + newPath); } else { atlas.spriteMaterial.mainTexture = atlas.texture = tex; } EditorGUIUtility.PingObject(tex); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } }