Skip to content

Commit

Permalink
added user option for different sprite naming schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
talecrafter committed Mar 22, 2017
1 parent 9698d7d commit 041f489
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 39 deletions.
12 changes: 5 additions & 7 deletions Assets/AnimationImporter/Editor/AnimationImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ private ImportedAnimationSheet ImportJob(AnimationImportJob job)
animationSheet.assetDirectory = job.assetDirectory;
animationSheet.name = job.name;

animationSheet.ApplySpriteNamingScheme(sharedData.spriteNamingScheme);

CreateSprites(animationSheet);

job.SetProgress(0.6f);
Expand Down Expand Up @@ -478,6 +480,8 @@ private void CreateSprites(ImportedAnimationSheet animationSheet)
private static Sprite[] GetAllSpritesFromAssetFile(string imageFilename)
{
var assets = AssetDatabase.LoadAllAssetsAtPath(imageFilename);

// make sure we only grab valid sprites here
List<Sprite> sprites = new List<Sprite>();
foreach (var item in assets)
{
Expand All @@ -487,13 +491,7 @@ private static Sprite[] GetAllSpritesFromAssetFile(string imageFilename)
}
}

// we order the sprites by name here because the LoadAllAssets above does not necessarily return the sprites in correct order
// the OrderBy is fed with the last word of the name, which is an int from 0 upwards
Sprite[] orderedSprites = sprites
.OrderBy(x => int.Parse(x.name.Substring(x.name.LastIndexOf(' ')).TrimStart()))
.ToArray();

return orderedSprites;
return sprites.ToArray();
}

// ================================================================================
Expand Down
6 changes: 6 additions & 0 deletions Assets/AnimationImporter/Editor/AnimationImporterWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ sprite values
ShowTargetLocationOptions("Animations", importer.sharedData.animationsTargetLocation);
ShowTargetLocationOptions("AnimationController", importer.sharedData.animationControllersTargetLocation);

GUILayout.Space(5f);

importer.sharedData.spriteNamingScheme = (SpriteNamingScheme)EditorGUILayout.IntPopup("Sprite Naming Scheme",
(int)importer.sharedData.spriteNamingScheme,
SpriteNaming.namingSchemesDisplayValues, SpriteNaming.namingSchemesValues);

GUILayout.Space(25f);

ShowHeadline("Automatic Import");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ private static bool GetFramesFromJSON(ImportedAnimationSheet animationSheet, JSO
foreach (var item in list)
{
ImportedAnimationFrame frame = new ImportedAnimationFrame();
frame.name = Path.GetFileNameWithoutExtension(item.Obj["filename"].Str);

var frameValues = item.Obj["frame"].Obj;
frame.width = (int)frameValues["w"].Number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ public AssetTargetLocation animationControllersTargetLocation
set { _animationControllersTargetLocation = value; }
}

private SpriteNamingScheme _spriteNamingScheme = SpriteNamingScheme.Classic;
public SpriteNamingScheme spriteNamingScheme
{
get { return _spriteNamingScheme; }
set { _spriteNamingScheme = value; }
}

public void RemoveAnimationThatDoesNotLoop(int index)
{
animationNamesThatDoNotLoop.RemoveAt(index);
Expand Down
79 changes: 79 additions & 0 deletions Assets/AnimationImporter/Editor/Config/SpriteNaming.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using UnityEngine;

namespace AnimationImporter
{
public enum SpriteNamingScheme : int
{
Classic, // hero 0
FileAnimationZero, // hero_idle_0, ...
FileAnimationOne, // hero_idle_1, ...
AnimationZero, // idle_0, ...
AnimationOne // idle_1, ...
}

public static class SpriteNaming
{
private static int[] _namingSchemesValues = null;
public static int[] namingSchemesValues
{
get
{
if (_namingSchemesValues == null)
{
InitNamingLists();
}

return _namingSchemesValues;
}
}

private static string[] _namingSchemesDisplayValues = null;
public static string[] namingSchemesDisplayValues
{
get
{
if (_namingSchemesDisplayValues == null)
{
InitNamingLists();
}

return _namingSchemesDisplayValues;
}
}

private static void InitNamingLists()
{
var allNamingSchemes = Enum.GetValues(typeof(SpriteNamingScheme));

_namingSchemesValues = new int[allNamingSchemes.Length];
_namingSchemesDisplayValues = new string[allNamingSchemes.Length];

for (int i = 0; i < allNamingSchemes.Length; i++)
{
SpriteNamingScheme namingScheme = (SpriteNamingScheme)allNamingSchemes.GetValue(i);
_namingSchemesValues[i] = (int)namingScheme;
_namingSchemesDisplayValues[i] = namingScheme.ToDisplayString();
}
}

private static string ToDisplayString(this SpriteNamingScheme namingScheme)
{
switch (namingScheme)
{
case SpriteNamingScheme.Classic:
return "hero 0, hero 1, ... (Default)";
case SpriteNamingScheme.FileAnimationZero:
return "hero_idle_0, hero_idle_1, ...";
case SpriteNamingScheme.FileAnimationOne:
return "hero_idle_1, hero_idle_2, ...";
case SpriteNamingScheme.AnimationZero:
return "idle_0, idle_1, ...";
case SpriteNamingScheme.AnimationOne:
return "idle_1, idle_2, ...";
}

return "";
}
}
}
12 changes: 12 additions & 0 deletions Assets/AnimationImporter/Editor/Config/SpriteNaming.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -259,29 +259,73 @@ public SpriteMetaData[] GetSpriteSheet(SpriteAlignment spriteAlignment, float cu
return metaData;
}

public void ApplySpriteNamingScheme(SpriteNamingScheme namingScheme)
{
const string NAME_DELIMITER = "_";

if (namingScheme == SpriteNamingScheme.Classic)
{
for (int i = 0; i < frames.Count; i++)
{
frames[i].name = name + " " + i.ToString();
}
}
else
{
foreach (var anim in animations)
{
for (int i = 0; i < anim.frames.Length; i++)
{
var animFrame = anim.frames[i];

switch (namingScheme)
{
case SpriteNamingScheme.FileAnimationZero:
animFrame.name = name + NAME_DELIMITER + anim.name + NAME_DELIMITER + i.ToString();
break;
case SpriteNamingScheme.FileAnimationOne:
animFrame.name = name + NAME_DELIMITER + anim.name + NAME_DELIMITER + (i + 1).ToString();
break;
case SpriteNamingScheme.AnimationZero:
animFrame.name = anim.name + NAME_DELIMITER + i.ToString();
break;
case SpriteNamingScheme.AnimationOne:
animFrame.name = anim.name + NAME_DELIMITER + (i + 1).ToString();
break;
}
}
}
}

// remove unused frames from the list so they don't get created for the sprite sheet
for (int i = frames.Count - 1; i >= 0; i--)
{
if (string.IsNullOrEmpty(frames[i].name))
{
frames.RemoveAt(i);
}
}
}

public void ApplyCreatedSprites(Sprite[] sprites)
{
if (sprites == null)
{
return;
}

// apply to general list
// add final Sprites to frames by comparing names
// as we can't be sure about the right order of the sprites
for (int i = 0; i < sprites.Length; i++)
{
frames[i].sprite = sprites[i];
}
Sprite sprite = sprites[i];

// apply to frames in animations (might be different classes than in general list)
foreach (var anim in animations)
{
for (int i = 0; i < anim.Count; i++)
for (int k = 0; k < frames.Count; k++)
{
int index = anim.firstSpriteIndex + i;

if (index < sprites.Length)
if (frames[k].name == sprite.name)
{
anim.frames[i].sprite = sprites[index];
frames[k].sprite = sprite;
break;
}
}
}
Expand Down
27 changes: 7 additions & 20 deletions Assets/AnimationImporter/Editor/PyxelEdit/PyxelEditImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private static ImportedAnimationSheet GetAnimationInfo(PyxelEditData data)
frame.duration *= (int)(animationData.frameDurationMultipliers[i] / 100f);
}

int tileIndex = animationData.baseTile + i;
int tileIndex = animationData.baseTile + frameIndex;

int columnCount = data.canvas.width / tileWidth;

Expand All @@ -154,27 +154,14 @@ private static ImportedAnimationSheet GetAnimationInfo(PyxelEditData data)
animationSheet.animations.Add(importAnimation);
}

// all frames
// gather all frames used by animations for the sprite sheet
animationSheet.frames = new List<ImportedAnimationFrame>();
for (int i = 0; i < maxTileIndex; i++)
foreach (var anim in animationSheet.animations)
{
ImportedAnimationFrame frame = new ImportedAnimationFrame();

int tileIndex = i;

int columnCount = data.canvas.width / tileWidth;

int column = tileIndex % columnCount;
int row = tileIndex / columnCount;

frame.y = row * tileHeight;
frame.x = column * tileWidth;
frame.width = tileWidth;
frame.height = tileHeight;

frame.name = data.name + " " + i.ToString();

animationSheet.frames.Add(frame);
foreach (var frame in anim.frames)
{
animationSheet.frames.Add(frame);
}
}

return animationSheet;
Expand Down

0 comments on commit 041f489

Please sign in to comment.