Skip to content

Commit

Permalink
#5 update existing animations and animator
Browse files Browse the repository at this point in the history
  • Loading branch information
nicloay committed Feb 12, 2014
1 parent f1d43c9 commit d2c7845
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class SpineImporterWizard :ScriptableWizard {
public int pixelsPerUnit = 100;
public bool buildAvatarMask = true;
public AnimationImportType animationImportType = AnimationImportType.MECANIM;
public bool updateResources = true;
[HideInInspector]
public string path;

Expand All @@ -33,7 +34,8 @@ public static bool validateContext(){

void OnWizardUpdate() {
helpString = "Be carefull, don't use small amout of pixels per unit (e.g. 1 or 10) \n" +
"if you are going to use result model with unity 2d physics and gravity";
"if you are going to use result model with unity 2d physics and gravity\n" +
"update resources means - instead of create new animator and new animations update them";
if (pixelsPerUnit <=0)
errorString = "PixelsPerUnit must be greater than zero";
else
Expand Down Expand Up @@ -70,11 +72,10 @@ void OnWizardCreate(){
}

ModelImporterAnimationType modelImporterAnimationType = getModelImporterAnimationType();
SpineUtil.addAnimation(rootGO, directory, spineData, boneGOByName, attachmentGOByNameBySlot, pixelsPerUnit, modelImporterAnimationType);
SpineUtil.addAnimation(rootGO, directory, spineData, boneGOByName, attachmentGOByNameBySlot,
pixelsPerUnit, modelImporterAnimationType, updateResources);
sk.showDefaulSlots();

SpineUtil.buildPrefab(rootGO, directory, name);

GameObject.DestroyImmediate(rootGO);

} catch (SpineMultiatlasCreationException e){
Expand Down
43 changes: 31 additions & 12 deletions Assets/UnitySpineImporter/Scripts/Editor/Util/SpineUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,28 @@ public static void addAnimation(GameObject rootGO,
SpineData spineData,
Dictionary<string, GameObject> boneGOByName,
AttachmentGOByNameBySlot attachmentGOByNameBySlot,
int pixelsPerUnit,
ModelImporterAnimationType modelImporterAnimationType)
int pixelsPerUnit,
ModelImporterAnimationType modelImporterAnimationType,
bool updateResources)
{
float ratio = 1.0f / (float)pixelsPerUnit;
foreach(KeyValuePair<string,SpineAnimation> kvp in spineData.animations){
string animationName = kvp.Key;
string animationFolder = rootDirectory+"/"+ANIMATION_FOLDER;
string assetPath = animationFolder + "/" + animationName+".anim";

SpineAnimation spineAnimation = kvp.Value;
AnimationClip animationClip = new AnimationClip();
bool updateCurve = false;
if (File.Exists(assetPath)){
AnimationClip oldClip = AssetDatabase.LoadAssetAtPath(assetPath, typeof(AnimationClip)) as AnimationClip;
if (oldClip != null){
animationClip = oldClip;
animationClip.ClearCurves();
updateCurve = true;
}
}

AnimationUtility.SetAnimationType(animationClip, modelImporterAnimationType);
if (spineAnimation.bones!=null)
addBoneAnimationToClip(animationClip,spineAnimation.bones, spineData, boneGOByName, ratio);
Expand All @@ -402,16 +416,22 @@ public static void addAnimation(GameObject rootGO,
if (spineAnimation.draworder!=null)
Debug.LogWarning("draworder animation implemented yet");

animationClip.frameRate = 30;
string animationFolder = rootDirectory+"/"+ANIMATION_FOLDER;
createFolderIfNoExists(rootDirectory, ANIMATION_FOLDER);

AssetDatabase.CreateAsset(animationClip, animationFolder + "/" + animationName+".anim");
AssetDatabase.SaveAssets();
if (modelImporterAnimationType == ModelImporterAnimationType.Generic)
AddClipToAnimatorComponent(rootGO,animationClip);
else
AddClipToLegacyAnimationComponent(rootGO, animationClip);
if (updateCurve){
EditorUtility.SetDirty(animationClip);
AssetDatabase.SaveAssets();
} else {
animationClip.frameRate = 30;
createFolderIfNoExists(rootDirectory, ANIMATION_FOLDER);
AssetDatabase.CreateAsset(animationClip, assetPath);
AssetDatabase.SaveAssets();

if (modelImporterAnimationType == ModelImporterAnimationType.Generic)
AddClipToAnimatorComponent(rootGO,animationClip);
else
AddClipToLegacyAnimationComponent(rootGO, animationClip);
}

}
}

Expand Down Expand Up @@ -479,7 +499,6 @@ public static void addSlotAnimationToClip(AnimationClip
string attachmentName = kvp2.Key;
AnimationCurve animationCurve = kvp2.Value;
string attachmentPath = spineData.slotPathByName[slotName] + "/" + attachmentName.Replace("/",SLASH_REPLACEMENT);
Debug.Log(attachmentPath);
clip.SetCurve(attachmentPath, typeof(GameObject),"m_IsActive", animationCurve);
}

Expand Down

0 comments on commit d2c7845

Please sign in to comment.